1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 using System.Data.Sql;
11 using Microsoft.SqlServer.Management.Common;//引用Microsoft.SqlServer.ConnectionInfo
12 using Microsoft.SqlServer.Management.Smo;
13 using Microsoft.SqlServer.Management.Nmo;
14 using System.IO;
15 using System.ServiceProcess;//添加引用
16
17 namespace Skype
18 {
19 /// <summary>
20 /// 20120813 塗聚文 Geovin Du
21 /// csharp run sql script create database
22 /// </summary>
23 public partial class InstallingSQLForm : Form
24 {
25 /// <summary>
26 ///
27 /// </summary>
28 public InstallingSQLForm()
29 {
30 InitializeComponent();
31 }
32 /// <summary>
33 /// 用腳本安裝SQL Server 2005數據庫
34 /// 20120813 塗聚文 締友計算機信息技術有限公司
35 /// </summary>
36 /// <param name="sender"></param>
37 /// <param name="e"></param>
38 private void InstallingSQLForm_Load(object sender, EventArgs e)
39 {
40
41 DataTable dt = new DataTable();
42 dt.Columns.Add("ID", typeof(int));
43 dt.Columns.Add("Databases", typeof(string));
44 dt.Columns.Add("Tables", typeof(string));
45 try
46 {
47
48
49
50 Server server = new Server(@"B2FC96ADD7DC472\GEOVINDU");
51 Database dbMaster = server.Databases["Master"];
52 string strScriptDir = string.Empty;
53 string strInstnwd = "instnwd.sql";
54 string strInstpub = "instpubs.sql";
55 strScriptDir = @"C:\filese\" + @"setup\";
56 if (!server.Databases.Contains("Northwind"))
57 {
58 if (File.Exists(strScriptDir + strInstnwd))
59 {
60 MessageBox.Show("創建數據庫Northwind。。。");
61 StreamReader rdr = new StreamReader(strScriptDir + strInstnwd);
62 dbMaster.ExecuteNonQuery(rdr.ReadToEnd());
63 }
64 }
65 if (!server.Databases.Contains("pubs"))
66 {
67 if (File.Exists(strScriptDir + strInstpub))
68 {
69 MessageBox.Show("創建數據庫pubs。。。");
70 StreamReader rdr = new StreamReader(strScriptDir + strInstpub);
71 dbMaster.ExecuteNonQuery(rdr.ReadToEnd());
72 }
73 }
74 //顯示所有數據庫及數據庫的表;
75 //如果是脫機狀態,後面的數據庫讀不出來,會報錯。如何判斷數據庫是脫機狀態呢?
76 //20120813 塗聚文 締友計算機信息技術有限公司
77 int i = 0;
78 foreach (Database db in server.Databases)
79 {
80 //db.Name;
81
82
83 if (db.Tables.Count > 0)
84 {
85 foreach (Table tbl in db.Tables)
86 {
87 //tbl.Name;
88 dt.Rows.Add(i, db.Name, tbl.Name);
89 i++;
90 }
91 }
92 else
93 {
94 dt.Rows.Add(i, db.Name, null);
95 i++;
96 }
97 }
98
99 this.dataGridView1.DataSource = dt;
100
101 }
102 catch (SqlServerManagementException ex)
103 {
104 this.dataGridView1.DataSource = dt;
105 ex.Message.ToString();
106 }
107
108 }
109
110 /// <summary>
111 /// 判断数据库服务是否已经启动,如果已经启动就返回True,否则返回False
112 /// </summary>
113 /// <returns></returns>
114 private bool DBServerStatus()
115 {
116 bool ExistFlag = false;
117 ServiceController[] service = ServiceController.GetServices();
118 for (int i = 0; i < service.Length; i++)
119 {
120 if (service[i].ServiceName.ToString().Contains(@"B2FC96ADD7DC472\GEOVINDU"))
121 {
122 ExistFlag = true;
123 string strOuput = string.Format("数据库服务器启动了服务名:{0},服务显示名:{1}\n", service[i].ServiceName, service[i].DisplayName);
124 MessageBox.Show(strOuput);
125 }
126 }
127 return ExistFlag;
128 }
129
130
131
132 }
133 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)