我打算从其他表格向我的文本框插入一个特定值。这样做会给我一个错误“将varchar值'5,000转换为数据类型int时转换失败。在我的数据库中,creditrequest列(5000)为char而目标列为int。对此有什么窍门吗?

这是我的代码:

protected void Button2_Click(object sender, EventArgs e)
    {
        using (SqlConnection scn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"))
        {
            scn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO UserData(CreditRequest) Select CreditRequest FROM CreditRequests Where Username=@Username", scn);
            cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["New"];

            object value = cmd.ExecuteScalar();

            if (value != null)
                txtCredit.Text = value.ToString();
        }


错误是

object value = cmd.ExecuteScalar();

最佳答案

INSERT INTO UserData(CreditRequest)  SELECT CAST(REPLACE(CreditRequest, ',', '') as int) FROM CreditRequests Where Username=@Username

09-25 17:54