我在网上看到一篇写将excel数据导入数据库的文章,就照抄写了下来,其中有部分不明白的地方,而且运行时出错,不知道如何更改,请各位帮我注释一下:DataSet ds = new DataSet();
try {
//获取全部数据
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFile);
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]",sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds,sheetName); //*一下为不明白的地方,请哪位懂的帮我家一下注释(注释越多越好)
//如果目标表不存在则创建,excel文件的第一行为列标题,从第二行开始全部都是数据记录
string strSql = string.Format("if object('{0}')is null create table {0}(",sheetName);
foreach(System.Data.DataColumn c in ds.Tables[0].Columns)
{
strSql += string.Format("[{0}]varchar(255),",c.ColumnName);
}
strSql = strSql.Trim(',') + ")"; using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))
{
sqlconn.Open();
System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
command.CommandText = strSql;
command.ExecuteNonQuery();//报异常--'&'附近有语法错误,即strSql的取值问题
sqlconn.Close();
} //*/
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//每次传输的行数
bcp.NotifyAfter = 100;//进度提示的行数
bcp.DestinationTableName = sheetName;//目标表
bcp.WriteToServer(ds.Tables[0]);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}原文地址是:http://blog.163.com/da7_1@126/blog/static/104072678201193145115982/
try {
//获取全部数据
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFile);
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]",sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds,sheetName); //*一下为不明白的地方,请哪位懂的帮我家一下注释(注释越多越好)
//如果目标表不存在则创建,excel文件的第一行为列标题,从第二行开始全部都是数据记录
string strSql = string.Format("if object('{0}')is null create table {0}(",sheetName);
foreach(System.Data.DataColumn c in ds.Tables[0].Columns)
{
strSql += string.Format("[{0}]varchar(255),",c.ColumnName);
}
strSql = strSql.Trim(',') + ")"; using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))
{
sqlconn.Open();
System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
command.CommandText = strSql;
command.ExecuteNonQuery();//报异常--'&'附近有语法错误,即strSql的取值问题
sqlconn.Close();
} //*/
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//每次传输的行数
bcp.NotifyAfter = 100;//进度提示的行数
bcp.DestinationTableName = sheetName;//目标表
bcp.WriteToServer(ds.Tables[0]);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}原文地址是:http://blog.163.com/da7_1@126/blog/static/104072678201193145115982/
看看这个了解一下,常用的一些转义字符吧
http://www.cnblogs.com/jialine/archive/2006/10/12/527736.html
就是这里。