2016-11-08 2 views
1

特定のパスでSQLデータベースを作成する必要があります。特定のパスでSQLデータベースを作成する

combobox1= "local", textbox14= "sqlusername", textbox13= 
"sqlpassword", textbox1= "newSQL", textbox4= "d:\MyFolder\" 

私が手にエラーが言う:

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 

:付近に正しくない構文
私はこのコードを見つけ '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%) " 

を誰が助けることはできますか?

+0

を試してみてください? – jarlh

+0

MS SQL Server 2008 – Ayden

+0

コードをデバッグし、sqlコマンドが実行される前に 'str'が正しいかどうかを教えてください。 –

答えて

0
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 New SqlConnection("Server='" + ComboBox8.Text + "';uid='" + TextBox14.Text + "';pwd='" + TextBox13.Text + "';database=master") 
Dim str As String = (Convert.ToString((Convert.ToString((Convert.ToString((Convert.ToString("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%) " 
Try 
    myConn.Open() 
    ExecuteSQLStmt(str,TextBox1.Text) 
    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 



Private Sub ExecuteSQLStmt(sql As String,mydb As String) 
    If myConn.State = ConnectionState.Open Then 
     myConn.Close() 
    End If 
    ConnectionString = "Integrated Security=SSPI;" + "Initial Catalog="+ mydb +"; + "Data Source=localhost;" 
    myConn.ConnectionString = ConnectionString 
    myConn.Open() 
    cmd = New SqlCommand(sql, myConn) 
    Try 
     cmd.ExecuteNonQuery() 
    Catch ae As SqlException 
     MessageBox.Show(ae.Message.ToString()) 
    End Try 
End Sub 

製品をDBMSこのことの仕事

+0

同じエラーバディ – Ayden

関連する問題