已有56人关注
刚买了全能词典....
发表在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

分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2010-11-08
沙发
读者朋友:
    您好,这是你导入的时候,输入的数据与数据库中的字段类型不一样,请检查一下,输入decimal类型的字段的值必须是数字。
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照