0
我想問一下,如果有人能指出我如何在visual studio上運行的C++程序中自動創建SQL Server數據庫。我也想定義將用數據庫創建的表。謝謝在正在運行的C++程序中自動創建SQL Server數據庫
我想問一下,如果有人能指出我如何在visual studio上運行的C++程序中自動創建SQL Server數據庫。我也想定義將用數據庫創建的表。謝謝在正在運行的C++程序中自動創建SQL Server數據庫
使用此代碼從C#創建數據庫。如果要定義表,請使用相同的代碼並使用create table script替換str值。
String str;
SqlConnection myConn = new SqlConnection ("Server=localhost;Integrated security=SSPI;database=master");
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 (Exception ex)
{
}
finally
{
if (myConn.State == ConnectionState.Open)
{
myConn.Close();
}
}