using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using System.Data.Sql; using Microsoft.SqlServer.Management.Common;//引用Microsoft.SqlServer.ConnectionInfo using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Nmo; using System.IO; using System.ServiceProcess;//添加引用 namespace Skype { /// <summary> /// 20120813 塗聚文 Geovin Du /// csharp run sql script create database /// </summary> public partial class InstallingSQLForm : Form { /// <summary> /// /// </summary> public InstallingSQLForm() { InitializeComponent(); } /// <summary> /// 用腳本安裝SQL Server 2005數據庫 /// 20120813 塗聚文 締友計算機信息技術有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void InstallingSQLForm_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(int)); dt.Columns.Add("Databases", typeof(string)); dt.Columns.Add("Tables", typeof(string)); try { Server server = new Server(@"B2FC96ADD7DC472\GEOVINDU"); Database dbMaster = server.Databases["Master"]; string strScriptDir = string.Empty; string strInstnwd = "instnwd.sql"; string strInstpub = "instpubs.sql"; strScriptDir = @"C:\filese\" + @"setup\"; if (!server.Databases.Contains("Northwind")) { if (File.Exists(strScriptDir + strInstnwd)) { MessageBox.Show("創建數據庫Northwind。。。"); StreamReader rdr = new StreamReader(strScriptDir + strInstnwd); dbMaster.ExecuteNonQuery(rdr.ReadToEnd()); } } if (!server.Databases.Contains("pubs")) { if (File.Exists(strScriptDir + strInstpub)) { MessageBox.Show("創建數據庫pubs。。。"); StreamReader rdr = new StreamReader(strScriptDir + strInstpub); dbMaster.ExecuteNonQuery(rdr.ReadToEnd()); } } //顯示所有數據庫及數據庫的表; //如果是脫機狀態,後面的數據庫讀不出來,會報錯。如何判斷數據庫是脫機狀態呢? //20120813 塗聚文 締友計算機信息技術有限公司 int i = 0; foreach (Database db in server.Databases) { //db.Name; if (db.Tables.Count > 0) { foreach (Table tbl in db.Tables) { //tbl.Name; dt.Rows.Add(i, db.Name, tbl.Name); i++; } } else { dt.Rows.Add(i, db.Name, null); i++; } } this.dataGridView1.DataSource = dt; } catch (SqlServerManagementException ex) { this.dataGridView1.DataSource = dt; ex.Message.ToString(); } } /// <summary> /// 判断数据库服务是否已经启动,如果已经启动就返回True,否则返回False /// </summary> /// <returns></returns> private bool DBServerStatus() { bool ExistFlag = false; ServiceController[] service = ServiceController.GetServices(); for (int i = 0; i < service.Length; i++) { if (service[i].ServiceName.ToString().Contains(@"B2FC96ADD7DC472\GEOVINDU")) { ExistFlag = true; string strOuput = string.Format("数据库服务器启动了服务名:{0},服务显示名:{1}\n", service[i].ServiceName, service[i].DisplayName); MessageBox.Show(strOuput); } } return ExistFlag; } } }
时间: 2024-11-29 09:04:53