转换失败,从varchar到int,ado.net

时间:2016-03-11 21:00:22

标签: c# mysql sql-server winforms

我在c#中通过ado.net使用sql数据库,并且我试图传递以下更新命令:

cmd.CommandText = @"UPDATE VehicleContract SET regNr='@reg', assoc_id='@assort', percentage='@perc', vehicleType='@type', trailerNr='@trailer' WHERE contractId='@id'";
cmd.Parameters.AddWithValue("@id", id);

id在int中。我把它读成一个int。我甚至做了一个int.Parse(txtbox.text),一切都很好。但是当我在框中插入所有值时,请按提交。我收到转化错误,说它无法转换' @ id' varchar to int ...没有意义

我有什么具体的做法不对吗?需要更多细节吗?

The whole code:
if (tbid.Text == "" || tbper.Text == "" || tbas.Text == "" || tbvt.Text == "")
            {
                MessageBox.Show("All fields must be filled ");
                return;
            }
            if (tbreg.Text == "" && tbtn.Text == "")
            {
                MessageBox.Show("Fill at least one from: Registration nr or trailer nr");
                return;
            }
            int id = 0;
            string reg = "";
            int assort = 0;
            int perc = 0;
            string type = "";
            int trailer = 0;
            try
            {
                id = int.Parse(tbid.Text);
                Console.Write(id);
                if (tbreg.Text != "")
                {
                    reg = tbreg.Text;
                }
                assort = int.Parse(tbas.Text);
                perc = int.Parse(tbper.Text);
                type = tbvt.Text;
                if (tbtn.Text != "")
                {
                    trailer = int.Parse(tbtn.Text);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("id, assoc, perc and trailer nr must be integers ");
                return;
            }
            SqlConnection con = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            con.ConnectionString = "Data Source=.;initial Catalog=Lab5;integrated security=true";
            con.Open();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = @"UPDATE VehicleContract SET regNr='@reg', assoc_id='@assort', percentage='@perc', vehicleType='@type', trailerNr='@trailer' WHERE contractId='@id'";
            cmd.Parameters.AddWithValue("@id", id);
            if (reg == "")
            {
                cmd.Parameters.AddWithValue("@reg", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@reg", reg);
            }
            cmd.Parameters.AddWithValue("@assort", assort);
            cmd.Parameters.AddWithValue("@perc", perc);
            cmd.Parameters.AddWithValue("@type", type);
            if (trailer == 0)
            {
                cmd.Parameters.AddWithValue("@trailer", DBNull.Value);
            }
            else
            {
                cmd.Parameters.AddWithValue("@trailer", trailer);
            }
            cmd.Connection = con;
            SqlDataReader sdr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(sdr);

            childgrid.DataSource = dt;
            sdr.Close();
            con.Close();

1 个答案:

答案 0 :(得分:1)

在查询中使用WHERE contractId='@id'时,您要将int contractId列与字符串'@id'值进行比较。

您需要使用WHERE contractId=@id

请删除@id周围的单引号和其他参数。