sqlbulkcopy导入的问题.见以下代码.

poloyzhang 2010-11-06 09:30:52

public static void info_import_allTable(string sqlTable, string[] tableHead)
{
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(excelDataSet.Tables[0]);// 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 (System.InvalidOperationException ioe)
{
string error = ioe.ToString();
string captionName = "提示 未打开文件!";
// MessageBox.Show(error.Substring(0, error.IndexOf("\r")),captionName, MessageBoxButtons.OK, MessageBoxIcon.Information );
MessageBox.Show(error, captionName, MessageBoxButtons.OK, MessageBoxIcon.Information);

// errorInput.toFile(captionName, ioe.ToString());


}
catch (SqlException sqlexc)
{
MessageBox.Show(sqlexc.ToString(), "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
//Console.WriteLine(sqlexc.ToString());
}
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());


}
}



传入datatable 对象.然后导入到数据库中.这种方法. ---------------------------
未打开文件或数据已经导入过!
---------------------------
来自数据源的 String 类型的给定值不能转换为指定目标列的类型 decimal。

以下错误怎么解决?
...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
poloyzhang 2010-11-09
  • 打赏
  • 举报
回复
还是自行解决了,散分......................................
deyygywxf 2010-11-08
  • 打赏
  • 举报
回复
String 类型的给定值不能转换为指定目标列的类型 decimal。

这个错误说明够明白了!
自己调试一项,看看是哪列的问题!!!
poloyzhang 2010-11-08
  • 打赏
  • 举报
回复
有空值当做有数据了,这种问题如何处理?
poloyzhang 2010-11-07
  • 打赏
  • 举报
回复
应该不用DataTable 的代码吧,主要是csvDataTable代码太多.都以文本方式读入的.
如果读入时有没有什么转换的方法?
chuzhaowei 2010-11-07
  • 打赏
  • 举报
回复
断点跟踪一下应该能找到错误的原因吧。

没有你的csv文件数据,我们也很难看出是哪地方出的问题。
poloyzhang 2010-11-07
  • 打赏
  • 举报
回复
可能csvBasicTable 的数据类型是不对的.有没有办法导入前转一下? 或读入时自动对应数据类型>
蝜蝂 2010-11-07
  • 打赏
  • 举报
回复
代码太多了,没有细看,我帮顶
mayonglong 2010-11-07
  • 打赏
  • 举报
回复
可能csvBasicTable 的某一个列的类型不对~

来自数据源的 String 类型的给定值不能转换为指定目标列的类型 decimal。
poloyzhang 2010-11-06
  • 打赏
  • 举报
回复
我的是csv文件导入,每一列都是文本.
如何把转呢> /? 数据库中的是decimal. 有没有其它办法.
hch126163 2010-11-06
  • 打赏
  • 举报
回复
String 类型的给定值不能转换为指定目标列的类型 decimal。

这个错误说明够明白了!
自己调试一项,看看是哪列的问题!!!
poloyzhang 2010-11-06
  • 打赏
  • 举报
回复
搞错了,是下面的代码........................



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

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧