我第一次尝试连接数据库,我收到了这个错误:
尝试为文件 VBTestDB.mdf 附加自动命名的数据库失败。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。
并出现错误
myconnect.Open()
这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myconnect As New SqlClient.SqlConnection
myconnect.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=VBTestDB.mdf;Integrated Security=True;User Instance=True;"
Dim mycommand As SqlClient.SqlCommand = New SqlClient.SqlCommand()
mycommand.Connection = myconnect
mycommand.CommandText = "INSERT INTO Card (CardNo,Name) VALUES (@cardno,@name)"
myconnect.Open()
Try
mycommand.Parameters.Add("@cardno", SqlDbType.Int).Value = TextBox1.Text
mycommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = TextBox2.Text
mycommand.ExecuteNonQuery()
MsgBox("Success")
Catch ex As System.Data.SqlClient.SqlException
MsgBox(ex.Message)
End Try
myconnect.Close()
End Sub