转换字符串格式 [英] Convert String format

查看:154
本文介绍了转换字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我遇到了错误

Dear All,

I got error

Failed to convert parameter value from a String to a Int32.



此代码将Excel数据导出到SqlServer.如何解决该错误.
预先感谢.



this code is export Excel data to SqlServer. How to resolve the error.
Thank in advance.

public void insertdataintosql(string ye_cm_id, string ye_fy_id, string ye_month, string ye_year, string ye_basic, string ye_hra, string ye_cea, string ye_pa, string ye_spl_allow, string ye_other_allow, string ye_pda, string ye_cadre, string ye_medical, string ye_os_allow, string ye_attire_allow)
    {//inserting data into the Sql Server
        SqlConnection conn = new SqlConnection("Data Source=SUBRAMANYAM\\SQLEXPRESS;Initial Catalog=Incometax;Integrated Security=true;");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "insert into yearly_earnings(ye_cm_id,ye_fy_id,ye_month,ye_year,ye_basic,ye_hra,ye_cea,ye_pa, ye_spl_allow,ye_other_allow,ye_pda,ye_cadre,ye_medical,ye_os_allow,ye_attire_allow) values (@ye_cm_id,@ye_fy_id,@ye_month,@ye_year,@ye_basic,@ye_hra,@ye_cea,@ye_pa, @ye_spl_allow,@ye_other_allow,@ye_pda,@ye_cadre,@ye_medical,@ye_os_allow,@ye_attire_allow)";
        cmd.Parameters.Add("@ye_cm_id", SqlDbType.Int).Value = ye_cm_id;
        cmd.Parameters.Add("@ye_fy_id", SqlDbType.Int).Value = ye_fy_id;
        ////cmd.Parameters.Add("@ye_fy_id", SqlDbType.Int).Value = ye_fy_id;
        cmd.Parameters.Add("@ye_month", SqlDbType.Int).Value = ye_month;
        cmd.Parameters.Add("@ye_basic", SqlDbType.Int).Value = ye_basic;
        cmd.Parameters.Add("@ye_year", SqlDbType.Int).Value = ye_year;
        cmd.Parameters.Add("@ye_hra", SqlDbType.Int).Value = ye_hra;
        cmd.Parameters.Add("@ye_cea", SqlDbType.Int).Value = ye_cea;
        cmd.Parameters.Add("@ye_pa", SqlDbType.Int).Value = ye_pa;
        cmd.Parameters.Add("@ye_spl_allow", SqlDbType.Int).Value = ye_spl_allow;
        cmd.Parameters.Add("@ye_other_allow", SqlDbType.Int).Value = ye_other_allow;
        cmd.Parameters.Add("@ye_pda", SqlDbType.Int).Value = ye_pda;
        cmd.Parameters.Add("@ye_cadre", SqlDbType.Int).Value = ye_cadre;
        cmd.Parameters.Add("@ye_medical", SqlDbType.Int).Value = ye_medical;
        cmd.Parameters.Add("@ye_os_allow", SqlDbType.Int).Value = ye_os_allow;
        cmd.Parameters.Add("@ye_attire_allow", SqlDbType.Int).Value = ye_attire_allow;

        cmd.CommandType = CommandType.Text;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }

推荐答案

无法将参数值从字符串转换为Int32.
在上面的代码中,有很多地方(命令参数)已将变量定义为int类型,并为其分配了一些值.其中很少一部分是作为字符串传递的方法参数本身.稍后,您将这些字符串分配给定义为int的参数.

看起来,其中至少有一个失败了.查看所有必须为int的值是否为整数.另外,将整数形式的整数传递给方法本身,而不是将其作为字符串传递.
Failed to convert parameter value from a String to a Int32.
In above code, there are lots of places(command parameters) where you have defined variable to be of type int and assigning some value to it. Quite few of them are method parameters itself that are passed as strings. Later you assign those strings to parameters defined as int.

Looks like, alteast one of them is failing. See if all the values that has to be int is an integer. Also, pass integers that are integers to the method itself instead of passing them as strings.


如果所有参数均为整数格式,只需将数据类型更改为int或将字符串值转换为整数.

If all the parameters are in integer format just change the data type to int or convert the string values to integer.

cmd.Parameters.Add("@ye_cm_id", SqlDbType.Int).Value = Convert.ToInt32(ye_cm_id);


你好,

看来,您有参数不匹配.您的参数不匹配.您的输入参数的类型为字符串,而sqlcommand参数的类型为int.请更正并重试.

问候
塞巴斯蒂安
Hello,

It seems that, you have parameter mismatch. your parameters are not matching. Your input parameter is of type string and sqlcommand parameter is of type int. please correct it and try again.

Regards
Sebastian


这篇关于转换字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆