我怎么用vb.net中的单个按钮在数据库(sql server)中插入和更新数据,但我没有得到结果。 这是我的代码.......
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Try
Dim reader As SqlDataReader
Dim query As String
Dim n As Integer
n = 0
sqlcom1 = New SqlCommand("select * from mydatabase.masters")
sqlcom1.ExecuteReader()
sqlcom = New SqlCommand("select * from mydatabase.masters")
sqlcom.ExecuteReader()
reader = sqlcom1.ExecuteReader
reader = sqlcom.ExecuteReader
sqlcom = New SqlCommand("Update masters SET EmpName=@EmpName, Age=@Age, Address=@Address where Empid=@Empid", conn)
sqlcom.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = TextBox4.Text
sqlcom.Parameters.Add("@Age", SqlDbType.Int).Value = TextBox3.Text
sqlcom.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox2.Text
sqlcom.Parameters.Add("@Empid", SqlDbType.Int).Value = TextBox1.Text
sqlcom1 = New SqlCommand("insert into masters(Empid, EmpName, Age, Address) values(@Empid, @EmpName, @Age, @Address)", conn)
sqlcom1.Parameters.AddWithValue("@Empid", TextBox1.Text)
sqlcom1.Parameters.AddWithValue("@EmpName", TextBox4.Text)
sqlcom1.Parameters.AddWithValue("@Age", TextBox3.Text)
sqlcom1.Parameters.AddWithValue("@Address", TextBox2.Text)
conn.Open()
While reader.Read
n = n + 1
End While
If table.Rows.Count = n Then
sqlcom1.ExecuteNonQuery()
ElseIf table.Rows.Count = n + 1 Then
sqlcom.ExecuteNonQuery()
End If
Catch ex As Exception
MessageBox.Show("error" + ex.Message)
End Try
End Sub
答案 0 :(得分:0)
使用块可确保即使发生错误也可以关闭并放置连接对象。 通常,我将注释放在一行,但是代码太混乱了,不得不将其中的大多数移到这里。我希望您能弄清楚它们的归属。
Dim reader As SqlDataReader
-未使用
Dim query As String
-未使用
Integer
被自动初始化为零
将查询和连接传递给命令的构造函数。
您的连接字符串将告诉SQL Server使用哪个数据库。在查询中没有必要。
显然,您想要的只是计数,而不是全部数据。
此查询与sqlcom1完全相同
Dim sqlcom As New SqlCommand("select * from mydatabase.masters", cn)
sqlcom.ExecuteReader()
你做了两次
reader = sqlcom1.ExecuteReader
不一定,我们已经获取了计数
`While reader.Read
n = n + 1
End While`
我假设表是在其他时间填充的DataTable。将计数与表中的计数进行比较并不是确定命令是插入还是更新的好方法,但是只要未将数据库和表与更新数据库的DataAdapter一起使用,它就可以工作。
Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Try
Dim n As Integer
Using cn As New SqlConnection("Your connection string")
Dim sqlcom1 As New SqlCommand("select Count(*) from masters", cn)
cn.Open()
n = CInt(sqlcom1.ExecuteScalar) 'n is now the number of rows in the database table
Dim sqlcom As New SqlCommand
sqlcom.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = TextBox4.Text
'Age is not a good idea to enter in a database. It changes over time.
'Enter the birth date and calculate the age as needed.
sqlcom.Parameters.Add("@Age", SqlDbType.Int).Value = CInt(TextBox3.Text)
sqlcom.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox2.Text
If table.Rows.Count > n Then
'Normally EmpId would be an auto increment (identity) field
'and would NOT be included in an insert.
sqlcom.CommandText = "insert into masters(EmpName, Age, Address) values(@EmpName, @Age, @Address)"
Else
sqlcom.CommandText = "Update masters SET EmpName=@EmpName, Age=@Age, Address=@Address where Empid=@Empid"
sqlcom1.Parameters.Add("@Empid", SqlDbType.Int).Value = CInt(TextBox1.Text)
End If
sqlcom.ExecuteNonQuery()
End Using
Catch ex As Exception
MessageBox.Show("error" + ex.Message)
End Try
End Sub
再三考虑,忘记整个事情。使用一个DataGridView和一个DataAdapter。然后,您可以使用Update,它将进行更新,插入和删除。