今日在导入Excel后要生成一张表,将Excel表内容转化为数据库中表内容时使用到System.Data.SqlClient.SqlBulkCopy这个类的对象,其中有个ColumnMapping.Add的对象方法,view plaincopy to clipboardprint?
/***** Excel导入 *****/
private void PapperImport(string name)
{
string Tablename = name.Remove(name.IndexOf("."));
Tablename = Tablename.Substring(name.LastIndexOf("//") + 1);
string sql = "select ObjectProperty(Object_ID( '" + Tablename + "'),'IsUserTable')"; //判断是否存在某张表的sql语句。
if (new DataBase().GetDataSetSql(sql).Tables[0].Rows[0][0].ToString() == "")
{
//创建Excel连接
//连接字符串
string strConn = "Provider =Microsoft.Jet.OLEDB.4.0; Data Source = " + name + "; Extended Properties='Excel 8.0; HDR = YES';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [Sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
conn.Close();
//===导入到datatable完毕========================
//========生成数据库表==========
sql = "use MyOnlineExam create table " + Tablename + "(题号 int primary Key, 题干 varchar(500), A varchar(500), B varchar(500), C varchar(500), D varchar(500), 学生选择答案 char(2),正确答案 char(2), 学号 varchar(14) )";
new DataBase().ExecuteNonQuery(sql);
//=========往数据库表中添加数据================
System.Data.SqlClient.SqlBulkCopy sqlbulk = new System.Data.SqlClient.SqlBulkCopy(System.Configuration.ConfigurationManager.AppSettings["constr"]);
sqlbulk.DestinationTableName = Tablename;
try
{
sqlbulk.ColumnMappings.Add("题号", "题号");
sqlbulk.ColumnMappings.Add("题干", "题干");
sqlbulk.ColumnMappings.Add("A", "A");
sqlbulk.ColumnMappings.Add("B", "B");
sqlbulk.ColumnMappings.Add("C", "C");
sqlbulk.ColumnMappings.Add("D", "D");
sqlbulk.ColumnMappings.Add("学生选择答案", "学生选择答案");
sqlbulk.ColumnMappings.Add("正确答案", "正确答案");
sqlbulk.ColumnMappings.Add("学号", "学号");
sqlbulk.WriteToServer(dt);
sqlbulk.Close();
}
catch (Exception ex)
{
Response.Write("
sql = "use MyOnlineExam drop table " + Tablename;
new DataBase().ExecuteNonQuery(sql);
return;
}
Response.Write("
}
else
{
Response.Write("
}
return;
}
/***** Excel导入 *****/
private void PapperImport(string name)
{
string Tablename = name.Remove(name.IndexOf("."));
Tablename = Tablename.Substring(name.LastIndexOf("//") + 1);
string sql = "select ObjectProperty(Object_ID( '" + Tablename + "'),'IsUserTable')"; //判断是否存在某张表的sql语句。
if (new DataBase().GetDataSetSql(sql).Tables[0].Rows[0][0].ToString() == "")
{
//创建Excel连接
//连接字符串
string strConn = "Provider =Microsoft.Jet.OLEDB.4.0; Data Source = " + name + "; Extended Properties='Excel 8.0; HDR = YES';";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [Sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
conn.Close();
//===导入到datatable完毕========================
//========生成数据库表==========
sql = "use MyOnlineExam create table " + Tablename + "(题号 int primary Key, 题干 varchar(500), A varchar(500), B varchar(500), C varchar(500), D varchar(500), 学生选择答案 char(2),正确答案 char(2), 学号 varchar(14) )";
new DataBase().ExecuteNonQuery(sql);
//=========往数据库表中添加数据================
System.Data.SqlClient.SqlBulkCopy sqlbulk = new System.Data.SqlClient.SqlBulkCopy(System.Configuration.ConfigurationManager.AppSettings["constr"]);
sqlbulk.DestinationTableName = Tablename;
try
{
sqlbulk.ColumnMappings.Add("题号", "题号");
sqlbulk.ColumnMappings.Add("题干", "题干");
sqlbulk.ColumnMappings.Add("A", "A");
sqlbulk.ColumnMappings.Add("B", "B");
sqlbulk.ColumnMappings.Add("C", "C");
sqlbulk.ColumnMappings.Add("D", "D");
sqlbulk.ColumnMappings.Add("学生选择答案", "学生选择答案");
sqlbulk.ColumnMappings.Add("正确答案", "正确答案");
sqlbulk.ColumnMappings.Add("学号", "学号");
sqlbulk.WriteToServer(dt);
sqlbulk.Close();
}
catch (Exception ex)
{
Response.Write("
sql = "use MyOnlineExam drop table " + Tablename;
new DataBase().ExecuteNonQuery(sql);
return;
}
Response.Write("
}
else
{
Response.Write("
}
return;
}
一开始使用的不是sqlbulk.ColumnMappings.Add("题号", "题号");而是sqlbulk.ColumnMappings.Add(1, 1);
编译时出现:
--------------------------------------------------------------------------------
给定的 ColumnMapping 与源或目标中的任意列均不匹配 错误
--------------------------------------------------------------------------------
在百度了之后发现要用sqlbulk.ColumnMappings.Add("题号", "题号")这样的格式保证列与列的正确匹配。在用了这个方法之后又出现了新的问题 : “ 从bcp 客户端收到的列长度无效” 错误。
再次百度,得到解释:源表数据不符合目标表数据类型,或者超出目标表定义长度 而导致的原因。
修改创建表语句为
--------------------------------------------------------------------------------
view plaincopy to clipboardprint?
sql = "use MyOnlineExam create table " + Tablename + "(题号 int primary Key, 题干 varchar(500), A varchar(500), B varchar(500), C varchar(500), D varchar(500), 学生选择答案 varchar(20),正确答案 varchar(20), 学号 varchar(14) )";
sql = "use MyOnlineExam create table " + Tablename + "(题号 int primary Key, 题干 varchar(500), A varchar(500), B varchar(500), C varchar(500), D varchar(500), 学生选择答案 varchar(20),正确答案 varchar(20), 学号 varchar(14) )";
自此编译通过,完成从Excel到sqlserver中的导入。
以此记录下来,虽然都是百度来的知识,但是成长却是属于自己的。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/HelloNull2010/archive/2010/10/17/5946954.aspx