private void execfile()
{
try
{
string connStr = "data source={0};user id={1};password={2};persist security info=false;packet size=4096";
ExecuteSql(connStr, "master", "CREATE DATABASE " + "数据库名"); //这个数据库名是指你要新建的数据库名称 下同
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
sqlProcess.StartInfo.FileName = "osql.exe ";
sqlProcess.StartInfo.Arguments = " -U 数据库用户名 -P 密码 -d 数据库名 -i 存放sql文本的目录sql.sql";
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
{
try
{
string connStr = "data source={0};user id={1};password={2};persist security info=false;packet size=4096";
ExecuteSql(connStr, "master", "CREATE DATABASE " + "数据库名"); //这个数据库名是指你要新建的数据库名称 下同
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
sqlProcess.StartInfo.FileName = "osql.exe ";
sqlProcess.StartInfo.Arguments = " -U 数据库用户名 -P 密码 -d 数据库名 -i 存放sql文本的目录sql.sql";
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
}
catch (Exception ex)
{
throw ex;
}
}
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}