CharlieChau
Active member
- Joined
- Jun 15, 2004
- Messages
- 26
- Programming Experience
- Beginner
Hi,
I have just learned from the link http://support.microsoft.com/kb/821765/EN-US/ to use the database with VB .NET
Then, I want to use the ExecuteScalar to verify the table Student in the link above as a reference table. For example, I can validate the SNo already existed or the SName is matching with the SNo. The following code is the simple test to verify if I have any SNo existed in the database Student.
Dim SqlConnection1 As SqlConnection
SqlConnection1 = New SqlConnection("server = localhost;uid=;password=;database=testdb")
Dim SQLStatement As String = "SELECT * FROM student"
Dim RecordCount As Integer
Dim SelectCommand As New SqlClient.SqlCommand (SQLStatement, SqlConnection1)
Try
SqlConnection1.Open()
RecordCount = CInt(SelectCommand.ExecuteScalar())
Catch ex As Exception
MessageBox.Show("Failed to execute command")
Finally
SqlConnection1.Close()
End Try
MessageBox.Show("Customers table contains " & RecordCount & " records")
It seems to me that the parameters SqlConnection1 does not work well,
but I do not know which parameters. Since I always get
the catch error message: ("Failed to execute command").
I do not know what I did wrong. Could someone help me out, please.
Thanks,
/CC.
I have just learned from the link http://support.microsoft.com/kb/821765/EN-US/ to use the database with VB .NET
Then, I want to use the ExecuteScalar to verify the table Student in the link above as a reference table. For example, I can validate the SNo already existed or the SName is matching with the SNo. The following code is the simple test to verify if I have any SNo existed in the database Student.
Dim SqlConnection1 As SqlConnection
SqlConnection1 = New SqlConnection("server = localhost;uid=;password=;database=testdb")
Dim SQLStatement As String = "SELECT * FROM student"
Dim RecordCount As Integer
Dim SelectCommand As New SqlClient.SqlCommand (SQLStatement, SqlConnection1)
Try
SqlConnection1.Open()
RecordCount = CInt(SelectCommand.ExecuteScalar())
Catch ex As Exception
MessageBox.Show("Failed to execute command")
Finally
SqlConnection1.Close()
End Try
MessageBox.Show("Customers table contains " & RecordCount & " records")
It seems to me that the parameters SqlConnection1 does not work well,
but I do not know which parameters. Since I always get
the catch error message: ("Failed to execute command").
I do not know what I did wrong. Could someone help me out, please.
Thanks,
/CC.