将数据类型varchar转换为float时出错。

时间:2017-05-14 11:36:06

标签: c# sql asp.net database

我收到此错误并且不知道问题是什么。 我正在更新的数据库列具有数据类型float,并且在代码的cmd1.ExecuteNonQuery();部分出现错误。 有什么想法吗?

 SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings
            ["connectToEnterpriseAssignmentDB"].ToString());
            SqlCommand cmd1 = new SqlCommand();

            string currency = lstCurrency.SelectedItem.Value.ToString();
            string columnCurr = lstColumnCurr.SelectedItem.Value.ToString();
            float value = float.Parse(txtValue.Text);
            currency = currency.Trim();
            columnCurr = columnCurr.Trim();

            cmd1.CommandText = "UPDATE CurrencyTbl SET " + columnCurr + " = '@Value' WHERE CurrencyName = '@Currency'";

            cmd1.Parameters.Add("@Currency", SqlDbType.Char).Value = currency;
            //cmd1.Parameters.Add("@Column", SqlDbType.Char).Value = columnCurr;
            cmd1.Parameters.Add("@Value", SqlDbType.Float).Value = value;

            cmd1.Connection = connect;
            connect.Open();
            cmd1.ExecuteNonQuery();

            connect.Close();

1 个答案:

答案 0 :(得分:1)

删除单引号。您正在使用正确的类型传递参数:

       cmd1.CommandText = "UPDATE CurrencyTbl SET " + columnCurr + " = @Value WHERE CurrencyName = @Currency";