private const string SQL_NAME="sql.sql";
private string sqlPath = string.Empty;
private string serverConn = string.Empty;
ServiceController sqlSer;
private void steupStart()
{
string desPath=string.Empty;
desPath = Directory.GetCurrentDirectory();
sqlPath = desPath+"//"+SQL_NAME;
sqlSer = new ServiceController("MSSQLSERVER");
//如果服务没有在运行中,则运行
if ( sqlSer.Status == ServiceControllerStatus.Stopped )
{
sqlSer.Start();
}
else if( sqlSer.Status == ServiceControllerStatus.Paused )
{
sqlSer.Continue();
}
sqlSer.Dispose();
while( true )
{
sqlSer = new ServiceController("MSSQLSERVER");
if( sqlSer.Status.Equals(ServiceControllerStatus.Running) )
{
sqlSer.Dispose();
break;
}
sqlSer.Dispose();
}
//检测用户名及密码是否正确
SqlConnection conn = new SqlConnection( "Server=" + txtServer.Text.Trim() + ";User id = " + txtUser.Text.Trim() + ";password =" + txtPass.Text.Trim() );
try
{
conn.Open();
conn.Close();
}
catch(Exception ex)
{
MessageBox.Show("错误的用户名或密码" + ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
Application.Exit();
return;
}
//起用进程
Process sqlpro = new Process();
//设置进程路径
sqlpro.StartInfo.FileName = desPath+"//osql.exe";//string.Format("{0}osql.exe",desPath);
//参数
sqlpro.StartInfo.Arguments = String.Format("-S {0} -U {1} -P {2} -i /"{3}/"",txtServer.Text.Trim(),txtUser.Text.Trim(),txtPass.Text.Trim(),sqlPath);
sqlpro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sqlpro.Start();
sqlpro.WaitForExit();
sqlpro.Close();
this.lb.Cursor = System.Windows.Forms.Cursors.Default;
MessageBox.Show("数据安装成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//steupData.Abort();
Application.Exit();
}
public static string GetMachineName()
{
RegistryKey CompName = Registry.LocalMachine ;
CompName = CompName.OpenSubKey("Software//Microsoft//Windows NT//CurrentVersion//Winlogon");
return CompName.GetValue("AltDefaultDomainName").ToString();
}
这个是执行你数据库脚本的代码。你自己改改。
sql.sql是你数据库的脚本