使用 C# 应用程序将 excel 数据批量复制到 sql
Bulkcopy excel data to sql using c# application
我正在尝试将 excel 数据批量复制到 SQL,但数据没有复制,程序也没有抛出错误。
帮我指出哪里做错了。
这是我的代码片段
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
openFileDialog.ShowDialog();
if (!string.IsNullOrEmpty(openFileDialog.FileName))
{
string ssqltable = "Student";
string myexceldataquery = "select Name,DOB,Email from [Sheet1$]";
try
{
//Excel connection string
string Econn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName + ";Extended Properties=Excel 12.0;";
//Sql connection string
string Sconn = "Data Source=.;Initial Catalog=db_Test;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Sconn);
sqlconn.Open();
sqlconn.Close();
OleDbConnection oledbconn = new OleDbConnection(Econn);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(Sconn);
bulkcopy.DestinationTableName = ssqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
dr.Close();
oledbconn.Close();
}
中有一个完整的示例
据此,我删除了 sqlconn.close() 行和循环 while (dr.Read())
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
openFileDialog.ShowDialog();
if (!string.IsNullOrEmpty(openFileDialog.FileName))
{
string ssqltable = "Student";
string myexceldataquery = "select Name,DOB,Email from [Sheet1$]";
try
{
//Excel connection string
string Econn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName + ";Extended Properties=Excel 12.0;";
//Sql connection string
string Sconn = "Data Source=.;Initial Catalog=db_Test;Integrated Security=True";
using (SqlConnection sqlconn = new SqlConnection(Sconn))
{
sqlconn.Open();
//sqlconn.Close();
OleDbConnection oledbconn = new OleDbConnection(Econn);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(Sconn);
bulkcopy.DestinationTableName = ssqltable;
bulkcopy.WriteToServer(dr);
}
}
finally
{
dr.Close();
oledbconn.Close();
}
我正在尝试将 excel 数据批量复制到 SQL,但数据没有复制,程序也没有抛出错误。 帮我指出哪里做错了。
这是我的代码片段
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
openFileDialog.ShowDialog();
if (!string.IsNullOrEmpty(openFileDialog.FileName))
{
string ssqltable = "Student";
string myexceldataquery = "select Name,DOB,Email from [Sheet1$]";
try
{
//Excel connection string
string Econn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName + ";Extended Properties=Excel 12.0;";
//Sql connection string
string Sconn = "Data Source=.;Initial Catalog=db_Test;Integrated Security=True";
SqlConnection sqlconn = new SqlConnection(Sconn);
sqlconn.Open();
sqlconn.Close();
OleDbConnection oledbconn = new OleDbConnection(Econn);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(Sconn);
bulkcopy.DestinationTableName = ssqltable;
while (dr.Read())
{
bulkcopy.WriteToServer(dr);
}
dr.Close();
oledbconn.Close();
}
据此,我删除了 sqlconn.close() 行和循环 while (dr.Read())
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
openFileDialog.ShowDialog();
if (!string.IsNullOrEmpty(openFileDialog.FileName))
{
string ssqltable = "Student";
string myexceldataquery = "select Name,DOB,Email from [Sheet1$]";
try
{
//Excel connection string
string Econn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName + ";Extended Properties=Excel 12.0;";
//Sql connection string
string Sconn = "Data Source=.;Initial Catalog=db_Test;Integrated Security=True";
using (SqlConnection sqlconn = new SqlConnection(Sconn))
{
sqlconn.Open();
//sqlconn.Close();
OleDbConnection oledbconn = new OleDbConnection(Econn);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(Sconn);
bulkcopy.DestinationTableName = ssqltable;
bulkcopy.WriteToServer(dr);
}
}
finally
{
dr.Close();
oledbconn.Close();
}