using System;
using System.Data.SqlClient;
using System.ServiceProcess;
using System.Windows.Forms;
namespace ProductManager
{
static class DBConnection
{
private static SqlConnection sql;
private static ServiceController server;
#region DBConnection
static DBConnection()
{
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = Environment.MachineName; // builder["Server"]
builder.InitialCatalog = "ProductSet"; // builder["Database"]
builder.IntegratedSecurity = true; // builder["Integrated Security"]
builder.TypeSystemVersion = "SQL Server 2000"; // builder["Type System Version"]
//builder.DataSource = @"./SQLEXPRESS";
//builder.InitialCatalog = "ProductSet";
//builder.IntegratedSecurity = true;
//builder.TypeSystemVersion = "SQL Server 2005";
//builder.DataSource = @"./SQLEXPRESS";
//builder.AttachDBFilename = @"|DataDirectory|/App_Data/ProductSet.mdf";
//builder.IntegratedSecurity = true;
//builder.UserInstance = true;
//builder.TypeSystemVersion = "SQL Server 2008";
sql = new SqlConnection(builder.ConnectionString);
server = new ServiceController("MSSQLSERVER"); // "MSSQL$SQLEXPRESS"
switch (server.Status)
{
case ServiceControllerStatus.Stopped: // 服务未运行。
server.Start(); // net start mssqlserver
break;
case ServiceControllerStatus.Paused: // 服务已暂停。
server.Continue(); // net continue mssqlserver
break;
}
do
{
System.Threading.Thread.Sleep(500);
server.Refresh(); // 通过将属性重置为其当前值来刷新属性值。
}
while (server.Status != ServiceControllerStatus.Running);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}
#endregion
public static SqlConnection SQL
{
get { return sql; }
}
public static ServiceController Server
{
get { return server; }
}
}
}