1
我需要使用特定路徑創建SQL數據庫。
我發現這個代碼:使用特定路徑創建SQL數據庫
Dim fullpath As String = TextBox4.Text & TextBox1.Text & "_data.mdf"
Dim fullpath1 As String = TextBox4.Text & TextBox1.Text & "_log.ldf"
Dim ExtLog As String = TextBox1.Text & "_Log"
Dim ExtDat As String = TextBox1.Text & "_Data"
Dim myConn As SqlConnection = New SqlConnection("Server='" & ComboBox8.Text & "';uid='" & TextBox14.Text & "';pwd='" & TextBox13.Text & "';database=master")
Dim str As String = "CREATE DATABASE " & TextBox1.Text & " ON PRIMARY (NAME = " & ExtDat & ",FILENAME = " & fullpath & ", SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) LOG ON (NAME = " & ExtLog & ", FILENAME = " & fullpath1 & ", 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
其中:
combobox1= "local", textbox14= "sqlusername", textbox13=
"sqlpassword", textbox1= "newSQL", textbox4= "d:\MyFolder\"
我得到的錯誤說:附近有語法錯誤 'd:',標籤 'd' 已聲明。查詢批次中的標籤名稱必須是唯一的。 但是,當我使用以下str時它工作正常:
str = "CREATE DATABASE newSQL ON PRIMARY (NAME = newSQL_Data,FILENAME = 'D:\MyFolder\newSQLData.mdf', SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) LOG ON (NAME = newSQL_Log, FILENAME = 'D:\MyFolder\newSQLLog.ldf', SIZE = 1MB,MAXSIZE = 5MB, FILEGROWTH = 10%) "
任何人都可以幫忙嗎?
哪個DBMS產品? – jarlh
MS SQL server 2008 – Ayden
請調試您的代碼,並告訴我們在執行sql命令之前什麼是'str'。 –