【问题标题】:Create SQL database with specific path创建具有特定路径的 SQL 数据库
【发布时间】:2016-11-08 07:04:56
【问题描述】:

我需要创建具有特定路径的 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 产品?
  • MS SQL 服务器 2008
  • 请调试您的代码并在执行 sql 命令之前告诉我们str 是什么。
  • 刚刚发现错误。你在FILENAME = 之前和之后错过了一个'
  • FILENAME = '" & fullpath & "', SIZE ...

标签: sql-server vb.net


【解决方案1】:
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

试试这个吧

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多