Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c记录更新中的一个问题#_C#_Sql - Fatal编程技术网

C# c记录更新中的一个问题#

C# c记录更新中的一个问题#,c#,sql,C#,Sql,我不知道为什么每次执行记录更新时,我更新的查询不会将ID从0增加到1,并且总是需要0。。我不知道如何将id增加到1,到目前为止。。请解释一下。。我的代码是: private void btnUpdate_Click(object sender, EventArgs e) { int CustomerID =0; SqlConnection cn = new SqlConnection(@"Data Sourc

我不知道为什么每次执行记录更新时,我更新的查询不会将ID从0增加到1,并且总是需要0。。我不知道如何将id增加到1,到目前为止。。请解释一下。。我的代码是:

private void btnUpdate_Click(object sender, EventArgs e)
            {
                int CustomerID =0;
                SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;
                Initial Catalog=Gym;Integrated Security=True");
                SqlCommand cmd  = new SqlCommand("Update Customer set Customer_Name = '" + tbName.Text + "',Cell_Number = '" + tbContactNumber.Text + "',Customer_Address = '" + tbAddress.Text + "' where CustomerID = " + CustomerID, cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                BindGridView();
            }


private void BindGridView()
        {
            SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Gym;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("Select * from Customer", cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dgView_CustomerInfo.DataSource = dt.DefaultView;
        }
您正在使用“更新客户”sql子句。 这意味着您将更新现有记录,而不是插入新记录

ID仅对新记录递增,而对现有记录不递增。 此外,请确保您的ID列配置正确。 它应该有如下示例中的标识(1,1)条款:

CREATE TABLE [dbo].[td_Component](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Url] [nvarchar](250) NOT NULL,
    [Caption] [nvarchar](50) NOT NULL,
    [Description] [varchar](4000) NULL,

您需要使用
命令.ExecuteNonQuery()

您确实需要阅读一本关于.net编程的书。你的代码充满了小故障

为了让你开始

        // put the connection string into the app.config
        using (SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS; Initial Catalog=Gym;Integrated Security=True"))
        {
            int result = new SqlCommand("Update Customer set Customer_Name = '" + tbName.Text + "',Cell_Number = '" + tbContactNumber.Text + "',Customer_Address = '" + tbAddress.Text + "' where CustomerID = " + CustomerID, cn).ExecuteNonQuery();
            // eval result to see wether there was realy an updated record...
        }
SqlConnection
上,使用
using()
语句。这样就可以处理对象的处置。事实上,在所有一次性物品上使用它

尝试对连接字符串使用
app.config/web.config


转义所有进入sql server的用户输入,以防止sql注入

您知道更新会更新现有记录。若ID是一个标识列,那个么它只会在插入记录时递增……首先也是最重要的一点,请阅读SQL注入和参数化查询。其次,您正在做一些非常可疑的事情……您正在将一个空数据表绑定到DataGridView……两次。这似乎很奇怪。第三,如果你从不增加Id,为什么Id要增加?在我看来,您是指表的自动Id,但是
更新
不会影响它。。我想更新现有记录。。我已初始化int CustomerID=0,但。。我不明白当用户点击gridview中存在的现有记录时,id将如何更改..您是否得到了问题的解决方案?您无法获得更新操作的自动递增值。但是,您可以为ExecuteOnQuery()获取0或1;这意味着0=未更新记录,1=更新记录。我已从您的代码中替换了我的代码,但我不知道当我单击“更新”按钮时,它为什么不执行任何操作。如果在更新事件中设置断点,它是否真的调用了更新?而且ID在任何情况下都不会增加,因为您正在更新…是的。Id不会增加。。我真正的问题其实是关于ID的,你能帮我把我的ID增加到1或者用户想要什么吗..如果我是你,我会在第二列中加入用户可以设置的数字。尽量保持CustomerID的原样。