[英]sql bulk copy from data table will not upload
Hello I have the code below that for some reason is not working: 您好,我下面的代码由于某种原因无法正常工作:
I use oledb to fill a dataset from a pipe delimited file that is uploaded through a fileupload control on an asp.net web application. 我使用oledb从管道分隔文件中填充数据集,该文件是通过asp.net Web应用程序上的fileupload控件上传的。 I then take the data table and then use sql bulk copy to copy the data to a table i have setup in sql. 然后,我取数据表,然后使用sql批量复制将数据复制到在sql中已设置的表中。
protected void btnUpload_Click(object sender, EventArgs e)
{
string filepath = fileUpload1.PostedFile.FileName;
PerformBulkCopy(GencoUpload(filepath));
}
public static DataTable GencoUpload(string path)
{
string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=\"" + dir +
"\\\";" + "Extended Properties=\"text;HDR=Yes;Format=Delimited(|)\";";
string query = "SELECT * FROM " + file;
DataTable dt = new DataTable();
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
try
{
dAdapter.Fill(dt);
}
catch
{
// catch code
}
dAdapter.Dispose();
return dt;
}
private void PerformBulkCopy(DataTable GencoInfo)
{
string conStr = ConfigurationManager.ConnectionStrings["EDI"].ConnectionString;
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conStr))
{
bulkcopy.DestinationTableName = "dbo.GencoUploadTempTable";
bulkcopy.WriteToServer(GencoInfo);
}
}
Can someone help me with why this isnt loading into my sql database? 有人可以帮我解决为什么不将其加载到我的sql数据库中吗? Thank you. 谢谢。
This should do the work............... 这应该做的工作......
public static DataTable columnNamessheet1(String excelFile)
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties='Excel 12.0;HDR=yes'";
string connectionstring ="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS";
using (OleDbConnection conn = new OleDbConnection(connString))
{
SqlConnection SqlConn1 = new SqlConnection(connectionstring);
SqlConn1.Open();
OleDbCommand odc = new OleDbCommand(string.Format("Select LoanNumber,CustomerName,DateofBirth,MobileNo,SanctionDate,EmployerName FROM [BAJAJ DUMP$]"), conn);
conn.Open();
OleDbDataReader reader = odc.ExecuteReader();
DataTable sheetSchema = reader.GetSchemaTable();
SqlBulkCopy sqlbulk = new SqlBulkCopy(SqlConn1);
sqlbulk.DestinationTableName = "CUSTOMER_DETAILS";
sqlbulk.ColumnMappings.Add("LoanNumber", "CUSTOMER_LOAN_NO");
sqlbulk.ColumnMappings.Add("CustomerName", "CUSTOMER_ NAME");
sqlbulk.ColumnMappings.Add("DateofBirth", "CUSTOMER_DOB");
sqlbulk.ColumnMappings.Add("MobileNo", "CUSTOMER_MOBILE NUMBER");
sqlbulk.ColumnMappings.Add("SanctionDate", "CUSTOMER_SANCTION DATE");
sqlbulk.ColumnMappings.Add("EmployerName", "CUSTOMER_COMPANY NAME");
sqlbulk.WriteToServer(reader);
conn.Close();
return sheetSchema;
}
}
try this and put a breakpoint in a catch: 试试这个,在断点处放一个断点:
string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;DataSource={0};Extended Properties=\"text;HDR=Yes;Format=Delimited(|)\";", dir);
try
{
dAdapter.Fill(dt);
}
catch (Exception exc)
{
string myErrorMsg = exc.Message;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.