我喜欢FILENAME
在下面的代码中使用参数 for,而不是 N'D:\DBName.mdf' 和 N'D:\DBName.ldf'。
如何在.sql
文件中更改它以及如何使用参数从 C# 调用它?谢谢。
SQL脚本:
CREATE DATABASE [DBName] ON PRIMARY
( NAME = N'DBName', FILENAME = N'D:\DBName.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'DBName_log', FILENAME = N'D:\DBName.ldf' , SIZE = 3456KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
END
C# 代码
string appPathDB = Path.GetDirectoryName(Application.ExecutablePath) + "\\DBName.mdf";
string appPathLog = Path.GetDirectoryName(Application.ExecutablePath) + "\\DBName_log.ldf";
private void CreateDatabase()
{
string connection = "Data Source=localhost;Initial Catalog=master;User ID=sa;Password=abcd1234";
FileInfo file = new FileInfo(Path.GetDirectoryName(Application.ExecutablePath) + "\\DBName.sql");
string script = file.OpenText().ReadToEnd();
ExecSql(script, connection, "DBName");
file.OpenText().Close();
}
public void ExecSql(string sql, string connectionString, string dataBaseNameToPrepend)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(sql);
server.ConnectionContext.Disconnect();
server.ConnectionContext.ExecuteNonQuery(sql);
MessageBox.Show("DB Uploaded.");
}
}