sqlBUlkCopy用法4
来源:互联网 发布:js rows 编辑:程序博客网 时间:2024/05/18 06:20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data .Odbc ;
using System.Data.OleDb ;
using System.Data .SqlClient ;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
private void copy()
{
string nam = this.FileUpload1.PostedFile.ToString();
if (nam.Length != 0)
{
int i = nam.LastIndexOf("//");
string newnm = nam.Substring(i);
int j = newnm.LastIndexOf(".");
string filename = newnm.Substring(1, j - 1);
string vsFilePath = nam.Substring(0, i);
string wjlx = newnm.Substring(j, newnm.Length - j);
string constr = "";
string sqlstr = "";
string connectionString = "Data Source=(local); Initial Catalog=Text;User ID=sa;Password=sa; Persist Security Info=False";
if (wjlx.ToLower() == ".dbf")//dbf文件
{
constr = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + vsFilePath;
sqlstr = "select * from " + filename;
using (OdbcConnection sourceConnection = new OdbcConnection(constr))
{
sourceConnection.Open();
OdbcCommand ocomm = new OdbcCommand(sqlstr, sourceConnection);
OdbcDataReader reader = ocomm.ExecuteReader();
using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
{
//文件filename中的列与表DPM1中的列进行对应
//第一个参数为filename中的字段
//第二个参数为DPM1中的字段
bulkCopy.ColumnMappings.Add("odate", "odate1");
bulkCopy.ColumnMappings.Add("currencyid", "currencyid1");
bulkCopy.ColumnMappings.Add("branchid", "branchid1");
bulkCopy.ColumnMappings.Add("costid", "costid1");
//DPM1为目标表
bulkCopy.DestinationTableName = "DPM1";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
return;
}
finally
{
reader.Close();
}
}
}
}
}
else
{
if (wjlx.ToLower() == ".xls" || wjlx.ToLower() == ".txt")
{
if (wjlx.ToLower() == ".xls")//excel文件
{
constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + vsFilePath + newnm + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
sqlstr = "select * from [sheet1$]";
}
else//txt文件
{
constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + vsFilePath + ";Extended Properties='text;HDR=Yes;FMT=Delimited;IMEX=1;'";
sqlstr = "select * from " + filename + "#txt";
}
using (OleDbConnection sourceConnection = new OleDbConnection(constr))
{
sourceConnection.Open();
OleDbCommand ocomm = new OleDbCommand(sqlstr, sourceConnection);
OleDbDataReader reader = ocomm.ExecuteReader();
using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
{
//文件filename中的列与表DPM1中的列进行对应
//第一个参数为filename中的字段
//第二个参数为DPM1中的字段
bulkCopy.ColumnMappings.Add("odate", "odate1");
bulkCopy.ColumnMappings.Add("currencyid", "currencyid1");
bulkCopy.ColumnMappings.Add("branchid", "branchid1");
bulkCopy.ColumnMappings.Add("costid", "costid1");
//DPM1为目标表
bulkCopy.DestinationTableName = "DPM1";
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
return;
}
finally
{
reader.Close();
}
}
}
}
}
}
}
}
}
- sqlBUlkCopy用法4
- sqlBUlkCopy用法4
- SqlBulkCopy 简单的用法
- ASP.NET的SqlBulkCopy用法
- SqlBulkCopy
- SqlBulkCopy
- SqlBulkCopy
- SqlBulkCopy
- SqlbulkCopy
- c#大量数量插入 SqlbulkCopy详细用法
- SqlBulkCopy从记事本批量复制数据4
- SqlBulkCopy从记事本批量复制数据4
- SQLBulkCopy使用
- SqlBulkCopy Class
- 使用SqlBulkCopy
- SQLBulkCopy使用
- SQLBulkCopy使用
- SqlBulkCopy 小试牛刀
- MTK socket 操作之域名解析
- Log4j使用总结
- windbg常用命令
- Base64加密解密算法的C/C++代码实现
- Base64加密解密算法的C/C++代码实现
- sqlBUlkCopy用法4
- const、volatile、restrict
- 简易的mp3分拣程序
- WordPress中index.php代码详解
- 试图模仿 Jbuilder拖拽生成代码功能
- IT人生 需要指引
- RFID & FAQs
- UIImage有关的几个方法
- java线程安全-通俗易懂的讲解