如何在 ado net 中使用 UPDATE

发布于 2024-11-03 06:45:35 字数 1051 浏览 1 评论 0原文

我需要在表中执行更新(作业)。但这不仅仅是用新值替换旧值,而是用新值替换旧值。对于列中已有的值,我必须添加(SUM)新值(该列的类型为 int)。 这就是我到目前为止所做的,但我陷入困境:

protected void subscribeButton_Click(object sender, EventArgs e)
    {
        string txtStudent = (selectedStudentLabel.Text.Split(' '))[0];
        int studentIndex = 0;
        studentIndex = Convert.ToInt32(txtStudent.Trim());        

        SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Trusted_Connection=True;User Instance=yes");
        conn.Open();
        string sql2 = "UPDATE student SET moneyspent = " + ?????? + " WHERE id=" + studentIndex + ";";
        SqlCommand myCommand2 = new SqlCommand(sql2, conn);

        try
        {
            conn.Open();
            myCommand2.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex);
        }
        finally
        {
            conn.Close();
        }
    }

我应该添加什么而不是 ??? 来实现我的目标?

这样可以吗?我想避免使用很多查询。

I need to perform an update in a table(Homework). But it is not just replacing an old value with a new one; to the already existing value in the column i have to add(SUM) the new value(the column is of type int).
This is what i did so far but i am stuck:

protected void subscribeButton_Click(object sender, EventArgs e)
    {
        string txtStudent = (selectedStudentLabel.Text.Split(' '))[0];
        int studentIndex = 0;
        studentIndex = Convert.ToInt32(txtStudent.Trim());        

        SqlConnection conn = new SqlConnection("Server=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Trusted_Connection=True;User Instance=yes");
        conn.Open();
        string sql2 = "UPDATE student SET moneyspent = " + ?????? + " WHERE id=" + studentIndex + ";";
        SqlCommand myCommand2 = new SqlCommand(sql2, conn);

        try
        {
            conn.Open();
            myCommand2.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex);
        }
        finally
        {
            conn.Close();
        }
    }

What should i add intead of ??? to achieve my goal?

Is it possible to do it this way? I want to avoid using to many queries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2024-11-10 06:45:35

如果我理解正确(我不确定我理解),你想要这样的东西:

string sql2 = "UPDATE student SET moneyspent = moneyspent + @spent WHERE id=@id";
SqlCommand myCommand2 = new SqlCommand(sql2, conn);
myCommand2.Parameters.AddWithValue("@spent", 50 )
myCommand2.Parameters.AddWithValue("@id", 1 )

注意我如何使用参数而不是字符串连接,非常重要!

If i understand you correctly (i'm not sure i do) you want something like this:

string sql2 = "UPDATE student SET moneyspent = moneyspent + @spent WHERE id=@id";
SqlCommand myCommand2 = new SqlCommand(sql2, conn);
myCommand2.Parameters.AddWithValue("@spent", 50 )
myCommand2.Parameters.AddWithValue("@id", 1 )

Notice how i've used parameters and not string concatenation, very important!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文