我使用以下VB代码将我的数据库附加到服务器。
Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Database='" & TextBox1.Text & "';Connect Timeout=30")
cnnConnection.Open()
但是它给出了一个错误:
无法打开物理文件“D:.Net Programs \ SQLDBAttachExperiments \ SQLDBAttachExperiments \ bin \ Debug \ datafiles \ dbSQLtest.mdf”。操作系统错误5:“5(未找到错误)”。无法将文件'D:.Net Programs \ SQLDBAttachExperiments \ SQLDBAttachExperiments \ bin \ Debug \ datafiles \ dbSQLtest.mdf'作为数据库'dbSQLtest'附加。
如何解决? 感谢
答案 0 :(得分:0)
这可能是由于在建立连接时由于某些错误而导致文件已经打开,因此建立如下所示的连接
Using con As System.Data.SqlClient.SqlConnection = New SqlConnection("YourConnection string")
con.Open()
Dim cmd As New SqlCommand()
Dim expression As String = "Parameter value"
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "Your Stored Procedure"
cmd.Parameters.Add("Your Parameter Name", SqlDbType.VarChar).Value = expression
cmd.Connection = con
Using dr As IDataReader = cmd.ExecuteReader()
If dr.Read() Then
End If
End Using
End Using