SqlCommandを使用してC#プログラムで直接スクリプトを実行できます。あなたは、外部ファイルからスクリプトを取得する必要がある場合は、あなたが変更するには、この
SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
string str = "CREATE DATABASE MyDatabase ON PRIMARY " +
"(NAME = MyDatabase_Data, " +
"FILENAME = 'C:\\MyDatabaseData.mdf', " +
"SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = MyDatabase_Log, " +
"FILENAME = 'C:\\MyDatabaseLog.ldf', " +
"SIZE = 1MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";
SqlCommand myCommand = new SqlCommand(str, myConn);
try
{
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show("DataBase is Created Successfully", "MyProgram",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
同様
:プロセスの出力ストリームを読ん
string str = System.IO.File.ReadAllText("SQLScript.sql");
は私の心に – Alex
なぜあなたが来て唯一のものですsqlcmdを使用し、直接SqlCommandを使用してスクリプトを実行しないでください。 https://support.microsoft.com/en-us/help/307283/how-to-create-a-sql-server-database-programmatically-by-using-ado-net –
@alex - どうすれば見つけることができますかdbが正常に作成された場合、または失敗した場合に返される値の種類の違い? – RISHABH