我想更新表格 [英] I want to update table
问题描述
我有两张桌子
1)tbl_product
id名称购买
1 abc 10
2 bca 20
1 abc 20
2)tbl_user1
id name purchase(Allow Null)
1 Arpit
2 Rakesh
现在我想要更新tbl_user1购买列ID明智意味着总购买金额(明智的)添加到购买栏中
看起来如下表
1)tbl_product
id name purchase
1 abc 10
2 bca 20
1 abc 20
2)tbl_user1
id名称购买(允许空)
1 Arpi 30
2 Rakesh 20
受保护的子Button1_Click(ByVal发送者A s Object,ByVal e As System.EventArgs)处理Button1.Click
Dim cmd As 新 SqlCommand( select * from tbl_product,con)
Dim da As 新 SqlDataAdapter(cmd)
Dim dt As 新 DataTable
da.Fill(dt)
Dim i 作为 整数
Dim id 作为 整数
对于 i = 1 至 dt.Rows.Count - 1
Dim pur < span class =code-keyword>作为 整数 = 0
' id = dt.Rows(0)(0).ToString()
pur = dt.Rows( 0 )( 2 )。ToString()
total =总计+ pur
' Label1.Text = total
con.Open()
Dim cmd1 作为 新 SqlCommand( update tbl_user1 set purchase = @ purchase where id = @ id,con)
cmd1.Parameters.AddWithValue( @ purchase,total)
cmd1.Parameters.AddWithValue( @ id,dt.Rows( 0 )( 0 )。ToString ())
cmd1.ExecuteNonQuery()
con.Close()
' dt.Rows(0)(0).ToString()=
下一页
End Sub
如果你想将值添加到列中,那就非常简单了:
< pre lang =vb> Dim cmd1 As New SqlCommand( update tbl_user1 set purchase = purchase + @ purchase where id = @ id,con )但是从DB列中删除allow nulls,a d设置默认值为零。这样,你想做的任何数学都将一直有效(你的总数也不需要被忽略)
i have two table
1) tbl_product
id name purchase
1 abc 10
2 bca 20
1 abc 20
2)tbl_user1
id name purchase(Allow Null)
1 Arpit
2 Rakesh
Now I want To Update tbl_user1 purchase column id wise means Total purchase amount(id wise) added into purchase column
look like below table
1) tbl_product
id name purchase
1 abc 10
2 bca 20
1 abc 20
2) tbl_user1
id name purchase(Allow Null)
1 Arpi 30
2 Rakesh 20
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As New SqlCommand("select * from tbl_product", con)
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
Dim i As Integer
Dim id As Integer
For i = 1 To dt.Rows.Count - 1
Dim pur As Integer = 0
'id = dt.Rows(0)(0).ToString()
pur = dt.Rows(0)(2).ToString()
total = total + pur
'Label1.Text = total
con.Open()
Dim cmd1 As New SqlCommand("update tbl_user1 set purchase=@purchase where id=@id", con)
cmd1.Parameters.AddWithValue("@purchase", total)
cmd1.Parameters.AddWithValue("@id", dt.Rows(0)(0).ToString())
cmd1.ExecuteNonQuery()
con.Close()
'dt.Rows(0)(0).ToString() = ""
Next
End Sub
If you means you want to add the value into the column, then that's pretty simple:
Dim cmd1 As New SqlCommand("update tbl_user1 set purchase=purchase+@purchase where id=@id", con)But get rid of the "allow nulls" from the DB column, and set a default of zero instead. That way, any math you want to do will always work (and your totals don't need to be faffed with either)
这篇关于我想更新表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!