首页上一页 1 下一页尾页 1 条记录 1/1页
刚买了全能词典....
发表在C#图书答疑
2010-11-08
是否精华
是
否
版块置顶:
是
否
刚收到全能词典,我查了一下没有sqlbulkcopy 导入数据库的?
我现在正在做一个sqlbulkcopy导入数据,csv导入为datatalbe 后导入到数据库中.
但出现以下问题:
来自数据源的 String 类型的给定值不能转换为指定目标列的类型 decimal。
这个不知道如何解决?
public static void info_import_allTable(DataTable csvBasicTable, string sqlTable, string[] tableHead) { //SqlBulkCopy try { using (SqlConnection destinationConnection = DbConnection.createConn()) { destinationConnection.Open(); using (SqlBulkCopy bcp = new SqlBulkCopy(destinationConnection)) // use app.config 文件 { // Perform an initial count on the destination table. SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM " + sqlTable + ";", destinationConnection); commandRowCount.CommandTimeout = 180; //180s 延时时间 3 分钟 long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar()); System.Data.SqlClient.SqlBulkCopyColumnMapping sqlbkColumnMapping = new System.Data.SqlClient.SqlBulkCopyColumnMapping(); foreach (string head in tableHead) { //if (head.Trim() == "备注" && sqlTable.Trim() == "salary") //{ // bcp.ColumnMappings.Add(head, "基本类备注"); //} //else //{ bcp.ColumnMappings.Add(head, head); //} } // bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcpRowView); bcp.BatchSize = 1;//每次传输的行数 bcp.NotifyAfter = 1;//进度提示的行数 //// bcp.DestinationTableName = sheetName;//目标表 bcp.DestinationTableName = sqlTable;//目标表 bcp.WriteToServer( csvBasicTable );// excelDataSet.Tables["sheet1"] long countEnd = System.Convert.ToInt32(commandRowCount.ExecuteScalar()); long rowscount = countEnd - countStart; string message = "导入成功,共导入 " + rowscount + " 行"; MessageBox.Show(message, "导入完成", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } } } catch (Exception exc) { /// string error = exc.ToString(); string fileName = "未打开文件或数据已经导入过!"; // string message = error.Substring(0, error.IndexOf("\r")); MessageBox.Show(exc.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Information); // MessageBox.Show(error.Substring(0, error.IndexOf("\r"))); // errorInput.toFile(fileName, exc.ToString()); } } // sqlbulkcopy
我现在正在做一个sqlbulkcopy导入数据,csv导入为datatalbe 后导入到数据库中.
但出现以下问题:
来自数据源的 String 类型的给定值不能转换为指定目标列的类型 decimal。
这个不知道如何解决?
public static void info_import_allTable(DataTable csvBasicTable, string sqlTable, string[] tableHead) { //SqlBulkCopy try { using (SqlConnection destinationConnection = DbConnection.createConn()) { destinationConnection.Open(); using (SqlBulkCopy bcp = new SqlBulkCopy(destinationConnection)) // use app.config 文件 { // Perform an initial count on the destination table. SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM " + sqlTable + ";", destinationConnection); commandRowCount.CommandTimeout = 180; //180s 延时时间 3 分钟 long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar()); System.Data.SqlClient.SqlBulkCopyColumnMapping sqlbkColumnMapping = new System.Data.SqlClient.SqlBulkCopyColumnMapping(); foreach (string head in tableHead) { //if (head.Trim() == "备注" && sqlTable.Trim() == "salary") //{ // bcp.ColumnMappings.Add(head, "基本类备注"); //} //else //{ bcp.ColumnMappings.Add(head, head); //} } // bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcpRowView); bcp.BatchSize = 1;//每次传输的行数 bcp.NotifyAfter = 1;//进度提示的行数 //// bcp.DestinationTableName = sheetName;//目标表 bcp.DestinationTableName = sqlTable;//目标表 bcp.WriteToServer( csvBasicTable );// excelDataSet.Tables["sheet1"] long countEnd = System.Convert.ToInt32(commandRowCount.ExecuteScalar()); long rowscount = countEnd - countStart; string message = "导入成功,共导入 " + rowscount + " 行"; MessageBox.Show(message, "导入完成", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); } } } catch (Exception exc) { /// string error = exc.ToString(); string fileName = "未打开文件或数据已经导入过!"; // string message = error.Substring(0, error.IndexOf("\r")); MessageBox.Show(exc.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Information); // MessageBox.Show(error.Substring(0, error.IndexOf("\r"))); // errorInput.toFile(fileName, exc.ToString()); } } // sqlbulkcopy