我正在尝试通过以下代码通过 VB.net 动态创建数据库
Dim str As String
Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
"uid=sa;pwd=a123;database=master")
str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
"(NAME = MyDatabase_Data, " & _
" FILENAME = 'E:\MyDatabaseData.mdf', " & _
" SIZE = 2MB, " & _
" MAXSIZE = 10MB, " & _
" FILEGROWTH = 10%) " & _
" LOG ON " & _
"(NAME = MyDatabase_Log, " & _
" FILENAME = 'E:\MyDatabaseLog.ldf', " & _
" SIZE = 1MB, " & _
" MAXSIZE = 5MB, " & _
" FILEGROWTH = 10%) "
Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
Try
myConn.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Database is created successfully", _
"MyProgram", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString())
Finally
If (myConn.State = ConnectionState.Open) Then
myConn.Close()
End If
End Try
但是项目运行时出现以下错误:
建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)