简体   繁体   English

如何更新 c# 中的数据

[英]How can i update data in c#

  1. I'm working in Visual Studio 2019 In c我在 Visual Studio 2019 中工作 c
  2. I have problem with updating data to database我在将数据更新到数据库时遇到问题
  3. I use local Visual Studio SQL database我使用本地 Visual Studio SQL 数据库

private void button1_Click(object sender, EventArgs e) { String source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Szabolcs\Documents\Adatbázis Kezelés2.mdf;Integrated Security=True;Connect Timeout=30"; private void button1_Click(object sender, EventArgs e) { String source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Szabolcs\Documents\Adatbázis Kezelés2.mdf;Integrated Security=True;Connect Timeout=30 "; SqlConnection con = new SqlConnection(source); SqlConnection con = new SqlConnection(source); con.Open(); con.Open();

        String sqlSelectQuery = "SELECT * FROM [Table] WHERE ID = "+ int.Parse(textBox1.Text);
        SqlCommand cmd = new SqlCommand(sqlSelectQuery, con);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            textBox2.Text = (dr["Name"].ToString());
            textBox3.Text = (dr["Kor"].ToString());
            label4.Text = (dr["Kor"].ToString());
            label5.Text = (dr["Kor"].ToString());

            int s = 11;
            string y = (dr["Kor"].ToString());

            label4.Text = (dr["Kor"].ToString());
            x = Int32.Parse(label4.Text);
            x = x + 0;

            label6.Text = (x.ToString());
            
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        String source = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Szabolcs\Documents\Adatbázis Kezelés2.mdf;Integrated Security=True;Connect Timeout=30";
        SqlConnection con = new SqlConnection(source);
        con.Open();
        x = x + 1;
        label6.Text = (x.ToString());
        String st = "UPDATE supplier SET Kor = " + label6.Text + " WHERE Id = " + textBox1.Text;
    }    
  

Add these lines添加这些行

SqlCommand cmd = new SqlCommand(st, con);
int result = cmd.ExecuteNonQuery();

Please put a breakpoint and check the value of st , is it generating the valid query.请放置一个断点并检查st的值,它是否生成了有效的查询。

I would suggest to use stored procedure and pass the parameters from code我建议使用存储过程并从代码中传递参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM