我正在使用VB 2012和SQL Server 2012 Localdb
我想在我的文件夹.mdf
C:\AppData
)
例如。 db文件名0105.mdf
&日志文件名0105.ldf
有人可以告诉我程序吗?
我可以使用连接字符串
连接到SQL Server 2012 localdbData Source=(LocalDB)\v11.0;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30
答案 0 :(得分:2)
使用SqlCommand.ExecuteNonQuery
Imports System.Data.SqlClient
Dim queryString As String = "CREATE DATABASE [0105] ON PRIMARY (NAME = N'0105', FILENAME = N'C:\APPDATA\Pluto_Data.MDF' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) " & _
"LOG ON (NAME = N'0105_Log', FILENAME = N'C:\APPDATA\0105_Log.LDF' , SIZE = 1024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )"
Using connection As New SqlConnection(queryString)
Dim command As New SqlCommand(queryString, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
只需更改上面需要更改的位,如文件组,文件增长和初始大小等。我认为虽然我没有使用LocalDB来尝试它,但我可能有点偏离正常。
请尝试使用我在下面评论中添加的链接。
Public Function CreateDatabase(ByVal dbName As String, ByVal dbFileName As String) As Boolean
Try
Dim connectionString As String = String.Format("Data Source=(LocalDB)\v11.0;Initial Catalog=master;Integrated Security=True")
Using connection As New SqlConnection(connectionString))
connection.Open()
Dim cmd As SqlCommand = connection.CreateCommand()
DetachDatabase(dbName)
cmd.CommandText = String.Format("CREATE DATABASE {0} ON (NAME = N'{0}', FILENAME = '{1}')", dbName, dbFileName)
cmd.ExecuteNonQuery()
End Using
If (My.Computer.FileSystem.FileExists(dbFileName)) Then
Return True
Else
Return False
End If
Catch
Throw
End Try
End Function
答案 1 :(得分:0)
感谢Steve Pettifer
这是最终的代码
Dim Conn As New SqlConnection("Data Source=(LocalDB)\v11.0;Initial Catalog=master;Integrated Security=True")
Using Conn
Conn.Open()
Dim cmd As SqlCommand = Conn.CreateCommand
Dim str As String = "CREATE Database {0} ON (Name= N'{0}', FileName='{1}\{0}.mdf')"
cmd.CommandText = String.Format(str, T2.Text, T1.Text)
cmd.ExecuteNonQuery()
End Using
T1.text:文件夹位置 T2.text:没有扩展名的文件名