如何在sql查询中使用varibale来更新表 [英] how to use varibale in sql query for updating table

查看:90
本文介绍了如何在sql查询中使用varibale来更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何使用sql查询中的vb代码中声明的变量来更新表。

i想要将总数插入分数表。



这里是代码:

How should i use the variable declared in the the vb code in sql query to update the table.
i want to insert the total into score table.

here's the code:

Dim count As Integer
        Dim count1 As Integer
        Dim count2 As Integer
        Dim count3 As Integer
        Dim total As Integer
       
        Dim mycmd As New SqlCommand()
        Dim mycmd1 As New SqlCommand()
        Dim mycmd2 As New SqlCommand()
        Dim mycon As New SqlConnection("Data Source=testvm;Initial Catalog=ULSLLIVE;Integrated Security=True;")
        mycon.Open()
        mycmd.Connection = mycon
        mycmd1.Connection = mycon
        mycmd.CommandText = "select count(*) from answer where a=1 ;"
        mycmd1.CommandText = "select count(*) from  answer where a=0;"
        count = CType(mycmd.ExecuteScalar(), Integer)
        count1 = CType(mycmd1.ExecuteScalar(), Integer)
        count2 = 2 * count
        count3 = 1 * count1
        total = count2 - count3
        
        mycmd2.Connection = mycon
        mycmd2.CommandText = "update score set marks=('" & total & "') where username=('" & Form2.Label3.Text & "');"
        mycmd.ExecuteNonQuery()
        mycon.Close()

推荐答案

试试这个:

Try this:
mycmd2.CommandText = "update score set marks=@TOT where username=@UN";
mycmd2.Parameters.AddWithValue("@TOT", total);
mycmd2.Parameters.AddWithValue("@UN", Form2.Label3.Text);
mycmd.ExecuteNonQuery()





但是,如果Form2是一个类,那么你就无法访问标签像这样,因为它们必须是静态的,控件不能那样工作。如果此代码是Form2类的一部分,则:



However, if Form2 is a class then you can't access labels like that, as they would have to be static, and controls don't work like that. If this code is part of the Form2 class, then:

mycmd2.Parameters.AddWithValue("@UN", Label3.Text);

如果它是一个不同的类,那么你需要看看其中一个:

在两种形式之间转移信息,第1部分:父母对儿童 [ ^ ]

在两种表格之间转移信息,第2部分:儿童到家长 [ ^ ]

在两种表格之间传递信息,第3部分:儿童对孩子 [ ^ ]

具体哪一个将取决于表格之间的关系。

If it is a different class, then you need to look at one of these:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
Exactly which one will depend on the relationship between the forms.


这篇关于如何在sql查询中使用varibale来更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆