我正在使用以下代码将我的数据库与 SQL Server Express 分离。如果有什么不对的地方请指教。我收到一个错误。
"Could not locate entry in sysdatabases for database 'EXEC master'. No entry found with that name. Make sure that the name is entered correctly."
我使用的代码是
Dim conn2 As New SqlConnection("Server=MyHomeServer\SQLExpress;Database=master;Trusted_Connection=False;User ID=sa;Password=abc123;")
Dim cmd2 As New SqlCommand("", conn2)
cmd2.CommandType = CommandType.StoredProcedure
cmd2.CommandText = "EXEC master.dbo.sp_detach_db @dbname = N'MyHomeDBTestPWD'"
conn2.Open()
cmd2.Connection = conn2
cmd2.ExecuteNonQuery()
最佳答案
你的处理方式是错误的。当您只需要添加存储过程名称时,您就在 CommandText 中分配了正确的 SQL 语句。然后,您需要将 Parameter
添加到您的 SqlCommand
。
Dim cmd2 As New SqlCommand("", conn2)
cmd2.CommandType = CommandType.StoredProcedure
cmd2.CommandText = "sp_detach_db"
cmd2.Parameters.AddWithValue("@dbname", "MyHomeDBTestPWD")
cmd2.ExecuteNonQuery()
如果您想执行 SQL 语句,则只需将 CommandType
更改为 CommandType.Text
关于sql-server - 我的分离代码正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262501/