我有以下代码,msgBox表示“DB changed”从不显示,我在即时窗口中收到如下消息。
但最后,表单加载了,我无法知道我的数据库是否已创建!
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim strConn As String = "Data Source=localhost;Initial Catalog=master;Integrated Security=True"
Dim conn As New SqlConnection(strConn)
conn.Open()
Try
conn.ChangeDatabase("productsDB")
MsgBox("DB changed")
Catch ex As Exception
Try
Dim command1 As New SqlCommand("CREATE DATABASE productsDB", conn)
command1.ExecuteNonQuery()
command1.Connection.ChangeDatabase("productsDB")
Dim command2 As New SqlCommand("CREATE TABLE products ([id][int],[name][char](30),[quantity][int],[dealer_price][int],[unit_price][int])", conn)
command2.ExecuteNonQuery()
Catch ex2 As Exception
MsgBox(ex2.Message)
End Try
End Try
End Sub
答案 0 :(得分:0)
如果你在try块中实例化连接并打开它,看看你要抛出的异常,可能会有所帮助。
Dim conn as SqlConnection
Try
conn = new SqlConnection(strConn)
con.Open()
...
Catch ex As Exception
MsgBox.Show(ex.Message)
End Try