我第一次尝试连接数据库,我收到此错误:
尝试为文件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
答案 0 :(得分:0)
我能够解决我的问题。 我只是将我的连接字符串更改为:
myconnect.ConnectionString =“Data Source =。\ SQLEXPRESS; Initial Catalog = VBTestDB; Integrated Security = True; User Id = sa; Password = welcome1”
它工作正常。
由于