SQlBulkCopy 的 System.InvalidOperationException

发布于 2024-08-17 08:19:31 字数 1197 浏览 2 评论 0原文

执行批量复制时出现以下错误。

System.InvalidOperationException
The given value of type String from the data source cannot be converted to 
type decimal of the specified target column.

我使用以下代码。

DataTable empTable = DataTemplate.GetEmployees();
    DataRow row;
    for (int i = 0; i < gv.Rows.Count;i++ )
    {
        row = empTable.NewRow();
        string empName = gv.DataKeys[i].Values[0].ToString(); //first key
        string hourSalary = gv.DataKeys[i].Values[1].ToString(); //second key
        row["Emp_Name"] = empName;
        row["Hour_Salary"] = Convert.ToDecimal(hourSalary);
        row["Advance_amount"] = Convert.ToDecimal(0);
        row["Created_Date"] = Convert.ToDateTime(System.DateTime.Now.ToString());
        row["Created_By"] = Convert.ToInt64(1);
        row["Is_Deleted"] = Convert.ToInt64(0);
        empTable.Rows.Add(row);
    }
    InsertintoEmployees(empTable, "Employee");

我的上述字段的 SQL 数据类型是:

Emp_Name nvarchar(50) ,
Hour_Salary numeric(18, 2), 
Advance_amount numeric(18, 2), 
Created_Date datetime,
Created_By numeric(18, 0), 
Is_Deleted numeric(18, 0) 

我不知道我做错了什么。

I got the following error when executing bulkcopy.

System.InvalidOperationException
The given value of type String from the data source cannot be converted to 
type decimal of the specified target column.

I use the following code.

DataTable empTable = DataTemplate.GetEmployees();
    DataRow row;
    for (int i = 0; i < gv.Rows.Count;i++ )
    {
        row = empTable.NewRow();
        string empName = gv.DataKeys[i].Values[0].ToString(); //first key
        string hourSalary = gv.DataKeys[i].Values[1].ToString(); //second key
        row["Emp_Name"] = empName;
        row["Hour_Salary"] = Convert.ToDecimal(hourSalary);
        row["Advance_amount"] = Convert.ToDecimal(0);
        row["Created_Date"] = Convert.ToDateTime(System.DateTime.Now.ToString());
        row["Created_By"] = Convert.ToInt64(1);
        row["Is_Deleted"] = Convert.ToInt64(0);
        empTable.Rows.Add(row);
    }
    InsertintoEmployees(empTable, "Employee");

My SQL datatypes for the above fields are:

Emp_Name nvarchar(50) ,
Hour_Salary numeric(18, 2), 
Advance_amount numeric(18, 2), 
Created_Date datetime,
Created_By numeric(18, 0), 
Is_Deleted numeric(18, 0) 

I don't know what I am doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2024-08-24 08:19:31

您的 hourSalary 变量是一个字符串,您稍后尝试将其转换为 Decimal,但失败。

您很可能有一个空字符串值或一个无效值。

根据您的要求,您必须决定如何处理无效值。除了失败之外,您基本上有两个选择:存储为默认值(可能是 0?)或存储为 Null 值。

如果您想存储默认值,那么您可以在转换工资时尝试以下操作:

string hourSalary = gv.DataKeys[i].Values[1].ToString();
double salary = 0;
if (!double.TryParse(hourSalary, out salary))
{
  salary = 0; // Set your default value here
}
row["Hour_Salary"] = salary;

这将确保您在 Hour_Salary 列中存储有效的十进制值。

如果要存储空值,则必须稍微更改代码:

string hourSalary = gv.DataKeys[i].Values[1].ToString();
double salary;
object salaryValue;
if (double.TryParse(hourSalary, out salary))
{
   salaryValue = salary;
}
else
{
  salaryValue = DBNull.Value; // Store as a null
}
row["Hour_Salary"] = salaryValue;

在本例中,我们执行相同的 TryParse,但如果失败,我们存储 DBNull.Value 而不是默认的 0 值。

Your hourSalary variable is a string that you later try to convert to a Decimal and this fails.

Most probably you have either an empty string value or an invalid value.

Depending on your requirements you must decide what to do with the invalid value. You basically have two options except failing: Store as a default value (0 maybe?) or store as a Null value.

If you want to store a default value then you can try the following when converting the salary:

string hourSalary = gv.DataKeys[i].Values[1].ToString();
double salary = 0;
if (!double.TryParse(hourSalary, out salary))
{
  salary = 0; // Set your default value here
}
row["Hour_Salary"] = salary;

This will ensure that you get a valid decimal value stored in your Hour_Salary column.

If you want to store a null value then you must change the code somewhat:

string hourSalary = gv.DataKeys[i].Values[1].ToString();
double salary;
object salaryValue;
if (double.TryParse(hourSalary, out salary))
{
   salaryValue = salary;
}
else
{
  salaryValue = DBNull.Value; // Store as a null
}
row["Hour_Salary"] = salaryValue;

In this example we do the same TryParse, but if it fails we store DBNull.Value instead of the default 0 value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文