单击按钮更新数据库

时间:2013-12-30 17:29:30

标签: c# winforms forms button sql-update

我有一个按钮,假设将数据更新到数据库中。

private void button4_Click(object sender, EventArgs e)
        {
            //need update code//
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
            conn.Open();

            SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = @cName", conn);
            daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;

            DataTable dtC = new DataTable();
            daCount.Fill(dtC);
            DataRow firstRow = dtC.Rows[0];

            string x = firstRow["iCount"].ToString();
            int y = Int32.Parse(x);
            int z = y + 1;

            //SqlCeCommand cmdC = conn.CreateCommand();
            SqlCommand cmdC = conn.CreateCommand();
            cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
                conn.Close();
}

但是我收到了这个错误..

the error

有人可以帮忙吗?

更新=

我已将代码更改为

cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";

但现在的问题是,没有更新。

数据库中的iCount是INT,值为0。 viewtime和lastview也没有更新。

我现在哪里出错?

3 个答案:

答案 0 :(得分:2)

改变这个:

    cmdC.CommandText = "Update ComDet set iCount = '" + z + "', ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";

    cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";

你不需要它周围的“'”撇号因为它的数字。这肯定会让你string not in correct format错误

答案 1 :(得分:1)

我猜可能icount值不是数字,我建议使用TryParse以防万一。这应该可以防止这种错误发生。如何处理查询返回的错误值是另一个问题。

private void button4_Click(object sender, EventArgs e)
        {
            //need update code//
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
            conn.Open();

            SqlDataAdapter daCount = new SqlDataAdapter("select iCount from ComDet where cName = @cName", conn);
            daCount.SelectCommand.Parameters.Add("@cName", SqlDbType.VarChar).Value = ListU.SelectedValue;

            DataTable dtC = new DataTable();
            daCount.Fill(dtC);
            DataRow firstRow = dtC.Rows[0];

            string x = firstRow["iCount"].ToString();

            int y = 0;
            if(Int32.TryParse(x,out y))
            {      
                System.Diagnostics.Debug.WriteLine("iCount was an valid int32");      
                int z = y + 1;

                //SqlCeCommand cmdC = conn.CreateCommand();
                SqlCommand cmdC = conn.CreateCommand();
                cmdC.CommandText = "Update ComDet set iCount = " + z + ", ViewTime = '" + lblTime.Text + "', LastView = '" + txtUser2.Text + "' Where cName = '" + ListU.SelectedValue.ToString() + "'";
             }
            else
                System.Diagnostics.Debug.WriteLine("iCount was NOT a valid int32, value: " + x);
             conn.Close();
}

答案 2 :(得分:0)

您检查过'x'变量的值吗?该异常通知X的值不是有效整数,因此抛出FormatException。