我的问题是我的代码有问题或没有插入数据库。为什么我不能插入它? 我认为数据已保存但未显示在数据库中。请帮我看看,谢谢。
Dim con As New SqlClient.SqlConnection
Dim cmd As New SqlClient.SqlCommand
con.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\HTPdatabase.mdf;Integrated Security=True"
cmd.CommandText = "INSERT INTO Customer (Username,Password,Name,IC,Address,Email,Carpark,topup)VALUES(@Username,@password,@Name,@IC,@address,@Email,@Carpark,@topup)"
cmd.Connection = con
con.Open()
cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text)
cmd.Parameters.AddWithValue("@password", PasswordTextBox.Text)
cmd.Parameters.AddWithValue("@Name", DBNull.Value)
cmd.Parameters.AddWithValue("@IC", DBNull.Value)
cmd.Parameters.AddWithValue("@address", DBNull.Value)
cmd.Parameters.AddWithValue("@Email", DBNull.Value)
cmd.Parameters.AddWithValue("@Carpark", DBNull.Value)
cmd.Parameters.AddWithValue("@topup", DBNull.Value)
MsgBox("Successfully register!Please remind customer to update own profile,Thank you !")
cmd.ExecuteNonQuery()
con.Close()
End Sub
答案 0 :(得分:0)
根据您的代码,它看起来不错,但
放置
MsgBox("Successfully register!Please remind customer to update own profile,Thank you !")
以下
cmd.ExecuteNonQuery()
因为在cmd.ExecuteNonQuery()期间代码可能会失败并且可能会引发异常。