【问题标题】:Updating Database on button click单击按钮更新数据库
【发布时间】:2014-01-17 15:03:36
【问题描述】:

我有一个按钮可以将数据更新到数据库中。

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();
}

但我得到了这个错误..

有人可以帮忙吗?

更新 =

我已将代码更改为

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

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

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

我现在哪里出错了?

【问题讨论】:

  • 去掉 iCount 周围的单引号
  • 尝试将firstRow["iCount"] 输出到调试控制台并检查是否为有效整数。
  • @AlexDenysenko - 你确定吗?因为那么,更新将如何检测 Z 值?
  • 它执行字符串 man... 字符串将是 'Update ComDet set iCount = 3, ViewTime' 或任何值
  • 去掉单引号会弄乱代码..

标签: c# winforms forms button sql-update


【解决方案1】:

您是否检查过“x”变量的值?该异常通知 X 的值不是一个有效的整数,因此会抛出 FormatException。

【讨论】:

  • 我如何检查值?
  • 在第 65 行(有 Int32.Parse(x) 的地方)添加一个断点,然后将鼠标悬停在变量 x 上,它应该会显示该变量的值。
  • 它说 x 是 null ,虽然 iCount 的值是 0 ,这在数据库中是正确的
  • 机器(每次)都是正确的。如果 x 为 null,是因为您的查询返回 null。您是否尝试过直接针对 SQL Server 进行查询?它返回多少行?
【解决方案2】:

我猜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();
}

【讨论】:

  • 代码运行,但没有更新任何东西.. icount 没有增加,也没有增加 viewtime 和 lastview.. 但现在没有错误..
  • @MohdNasrulIwanFajaruddin 好的,这意味着从数据库返回的iCount 无法解析为 Int32 类型,你知道那个值是什么吗?
  • 在数据库中,iCount 为 0,所以当我将 iCount 放入 X 时,Y 应该将 X 解析为 int。那么 Z 应该做 Y+1。
  • @MohdNasrulIwanFajaruddin iCount 值是否可能包含小数 0.00 或除数字之外的任何其他字符?
  • 不,从设计上看,它设置为INT,数据库中的当前值为0
【解决方案3】:

改变这个:

    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 错误

【讨论】:

  • 是的..数据库中的iCount是一个整数..顺便说一句,代码现在可以工作了,但是数据库中没有更新..这是怎么回事?
  • 更新不起作用的唯一原因是 WHERE 子句找不到符合该标准的任何内容。你确定这是正确的:Where cName = '" + ListU.SelectedValue.ToString() + "'
  • 这是你应该做的:在该行放置一个断点,然后直接将 SQL 复制到 SSMS 中。
  • maigad.. 那是我错过的东西.. executenonquery.. 我现在要把它说出来.. 给我一秒钟..
  • 如果你想继续失去你的头发.. 保持在尝试用撇号写出 sql 语句的相同路径上。您应该查看参数化查询。任何人都可以通过在文本框中输入恶意代码来破解您的应用程序(称为 SQL 注入攻击)
猜你喜欢
  • 2016-06-11
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
相关资源
最近更新 更多