Form1.cs
上传用户:chizxy
上传日期:2014-11-29
资源大小:407k
文件大小:16k
源码类别:

其他行业

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Threading;
  9. namespace serversetup
  10. {
  11. /// <summary>
  12. /// Form1 的摘要说明。
  13. /// </summary>
  14. public class Form1 : System.Windows.Forms.Form
  15. {
  16. private string[,] database_name;
  17. private System.Windows.Forms.PictureBox pictureBox1;
  18. private System.Windows.Forms.Button btn_esc;
  19. private System.Windows.Forms.Button btn_ok;
  20. private System.Windows.Forms.TextBox txt_user;
  21. private System.Windows.Forms.TextBox txt_pwd;
  22. private System.Windows.Forms.TextBox txt_ip;
  23. private System.Windows.Forms.Label label3;
  24. private System.Windows.Forms.Label label2;
  25. private System.Windows.Forms.Label label1;
  26. private AxSuperMapLib.AxSuperWorkspace axSuperWorkspace1;
  27. private System.Windows.Forms.Label label5;
  28. private System.Windows.Forms.TextBox textBox1;
  29. private System.Windows.Forms.CheckBox checkBox1;
  30. private System.Windows.Forms.GroupBox groupBox1;
  31. private System.Windows.Forms.GroupBox groupBox2;
  32. /// <summary>
  33. /// 必需的设计器变量。
  34. /// </summary>
  35. private System.ComponentModel.Container components = null;
  36. public Form1()
  37. {
  38. //
  39. // Windows 窗体设计器支持所必需的
  40. //
  41. InitializeComponent();
  42. //
  43. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  44. //
  45. }
  46. /// <summary>
  47. /// 清理所有正在使用的资源。
  48. /// </summary>
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if (components != null) 
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60. #region Windows 窗体设计器生成的代码
  61. /// <summary>
  62. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  63. /// 此方法的内容。
  64. /// </summary>
  65. private void InitializeComponent()
  66. {
  67. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  68. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  69. this.btn_esc = new System.Windows.Forms.Button();
  70. this.btn_ok = new System.Windows.Forms.Button();
  71. this.txt_user = new System.Windows.Forms.TextBox();
  72. this.txt_pwd = new System.Windows.Forms.TextBox();
  73. this.txt_ip = new System.Windows.Forms.TextBox();
  74. this.label3 = new System.Windows.Forms.Label();
  75. this.label2 = new System.Windows.Forms.Label();
  76. this.label1 = new System.Windows.Forms.Label();
  77. this.axSuperWorkspace1 = new AxSuperMapLib.AxSuperWorkspace();
  78. this.label5 = new System.Windows.Forms.Label();
  79. this.textBox1 = new System.Windows.Forms.TextBox();
  80. this.checkBox1 = new System.Windows.Forms.CheckBox();
  81. this.groupBox1 = new System.Windows.Forms.GroupBox();
  82. this.groupBox2 = new System.Windows.Forms.GroupBox();
  83. ((System.ComponentModel.ISupportInitialize)(this.axSuperWorkspace1)).BeginInit();
  84. this.groupBox1.SuspendLayout();
  85. this.groupBox2.SuspendLayout();
  86. this.SuspendLayout();
  87. // 
  88. // pictureBox1
  89. // 
  90. this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  91. this.pictureBox1.Location = new System.Drawing.Point(8, 184);
  92. this.pictureBox1.Name = "pictureBox1";
  93. this.pictureBox1.Size = new System.Drawing.Size(560, 4);
  94. this.pictureBox1.TabIndex = 19;
  95. this.pictureBox1.TabStop = false;
  96. // 
  97. // btn_esc
  98. // 
  99. this.btn_esc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  100. this.btn_esc.Location = new System.Drawing.Point(456, 192);
  101. this.btn_esc.Name = "btn_esc";
  102. this.btn_esc.TabIndex = 18;
  103. this.btn_esc.Text = "关闭";
  104. this.btn_esc.Click += new System.EventHandler(this.btn_esc_Click);
  105. // 
  106. // btn_ok
  107. // 
  108. this.btn_ok.Enabled = false;
  109. this.btn_ok.Location = new System.Drawing.Point(344, 192);
  110. this.btn_ok.Name = "btn_ok";
  111. this.btn_ok.TabIndex = 17;
  112. this.btn_ok.Text = "确定";
  113. this.btn_ok.Click += new System.EventHandler(this.btn_ok_Click);
  114. // 
  115. // txt_user
  116. // 
  117. this.txt_user.Location = new System.Drawing.Point(96, 56);
  118. this.txt_user.Name = "txt_user";
  119. this.txt_user.Size = new System.Drawing.Size(158, 21);
  120. this.txt_user.TabIndex = 13;
  121. this.txt_user.Text = "";
  122. // 
  123. // txt_pwd
  124. // 
  125. this.txt_pwd.Location = new System.Drawing.Point(96, 88);
  126. this.txt_pwd.Name = "txt_pwd";
  127. this.txt_pwd.PasswordChar = '*';
  128. this.txt_pwd.Size = new System.Drawing.Size(158, 21);
  129. this.txt_pwd.TabIndex = 15;
  130. this.txt_pwd.Text = "";
  131. // 
  132. // txt_ip
  133. // 
  134. this.txt_ip.Location = new System.Drawing.Point(96, 24);
  135. this.txt_ip.Name = "txt_ip";
  136. this.txt_ip.Size = new System.Drawing.Size(158, 21);
  137. this.txt_ip.TabIndex = 11;
  138. this.txt_ip.Text = "";
  139. // 
  140. // label3
  141. // 
  142. this.label3.Location = new System.Drawing.Point(8, 24);
  143. this.label3.Name = "label3";
  144. this.label3.Size = new System.Drawing.Size(80, 23);
  145. this.label3.TabIndex = 16;
  146. this.label3.Text = "服务器IP地址";
  147. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  148. // 
  149. // label2
  150. // 
  151. this.label2.Location = new System.Drawing.Point(16, 88);
  152. this.label2.Name = "label2";
  153. this.label2.Size = new System.Drawing.Size(72, 24);
  154. this.label2.TabIndex = 14;
  155. this.label2.Text = "用户密码";
  156. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  157. // 
  158. // label1
  159. // 
  160. this.label1.Location = new System.Drawing.Point(8, 56);
  161. this.label1.Name = "label1";
  162. this.label1.Size = new System.Drawing.Size(80, 23);
  163. this.label1.TabIndex = 12;
  164. this.label1.Text = "数据库用户名";
  165. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  166. // 
  167. // axSuperWorkspace1
  168. // 
  169. this.axSuperWorkspace1.Enabled = true;
  170. this.axSuperWorkspace1.Location = new System.Drawing.Point(176, 144);
  171. this.axSuperWorkspace1.Name = "axSuperWorkspace1";
  172. this.axSuperWorkspace1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSuperWorkspace1.OcxState")));
  173. this.axSuperWorkspace1.Size = new System.Drawing.Size(32, 32);
  174. this.axSuperWorkspace1.TabIndex = 22;
  175. // 
  176. // label5
  177. // 
  178. this.label5.Location = new System.Drawing.Point(40, 160);
  179. this.label5.Name = "label5";
  180. this.label5.Size = new System.Drawing.Size(520, 16);
  181. this.label5.TabIndex = 23;
  182. this.label5.Text = "提示信息";
  183. this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  184. // 
  185. // textBox1
  186. // 
  187. this.textBox1.Location = new System.Drawing.Point(8, 16);
  188. this.textBox1.Multiline = true;
  189. this.textBox1.Name = "textBox1";
  190. this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
  191. this.textBox1.Size = new System.Drawing.Size(264, 96);
  192. this.textBox1.TabIndex = 24;
  193. this.textBox1.Text = "系统将创建以下数据库,如果系统中有同名的库,将被覆盖。 此操作将不可撤消,您确认执行本次操作?rnrn数据库列表:rn    workspace   pm1x  " +
  194. "pm2x   pm3x   pm4x  pm5x  pm6x  pm7x  pm8x  pm9x  pm10x  pm11x  pm12x  pm13x  pm" +
  195. "14x   pm112x  pm113x  wu8  wu910  ji1617   geng20  ding56  ding4  dibanrn   ";
  196. // 
  197. // checkBox1
  198. // 
  199. this.checkBox1.ForeColor = System.Drawing.Color.Red;
  200. this.checkBox1.Location = new System.Drawing.Point(24, 114);
  201. this.checkBox1.Name = "checkBox1";
  202. this.checkBox1.Size = new System.Drawing.Size(240, 24);
  203. this.checkBox1.TabIndex = 26;
  204. this.checkBox1.Text = "我已经仔细阅读了警告,确认执行操作";
  205. this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
  206. // 
  207. // groupBox1
  208. // 
  209. this.groupBox1.Controls.Add(this.checkBox1);
  210. this.groupBox1.Controls.Add(this.textBox1);
  211. this.groupBox1.ForeColor = System.Drawing.Color.Red;
  212. this.groupBox1.Location = new System.Drawing.Point(16, 8);
  213. this.groupBox1.Name = "groupBox1";
  214. this.groupBox1.Size = new System.Drawing.Size(280, 144);
  215. this.groupBox1.TabIndex = 27;
  216. this.groupBox1.TabStop = false;
  217. this.groupBox1.Text = "警告";
  218. // 
  219. // groupBox2
  220. // 
  221. this.groupBox2.Controls.Add(this.txt_pwd);
  222. this.groupBox2.Controls.Add(this.txt_ip);
  223. this.groupBox2.Controls.Add(this.label3);
  224. this.groupBox2.Controls.Add(this.label2);
  225. this.groupBox2.Controls.Add(this.label1);
  226. this.groupBox2.Controls.Add(this.txt_user);
  227. this.groupBox2.Location = new System.Drawing.Point(304, 8);
  228. this.groupBox2.Name = "groupBox2";
  229. this.groupBox2.Size = new System.Drawing.Size(264, 144);
  230. this.groupBox2.TabIndex = 28;
  231. this.groupBox2.TabStop = false;
  232. this.groupBox2.Text = "参数设置";
  233. // 
  234. // Form1
  235. // 
  236. this.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
  237. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  238. this.ClientSize = new System.Drawing.Size(584, 229);
  239. this.Controls.Add(this.groupBox2);
  240. this.Controls.Add(this.groupBox1);
  241. this.Controls.Add(this.label5);
  242. this.Controls.Add(this.axSuperWorkspace1);
  243. this.Controls.Add(this.pictureBox1);
  244. this.Controls.Add(this.btn_esc);
  245. this.Controls.Add(this.btn_ok);
  246. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  247. this.MaximizeBox = false;
  248. this.MinimizeBox = false;
  249. this.Name = "Form1";
  250. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  251. this.Text = "GIS服务器设置";
  252. ((System.ComponentModel.ISupportInitialize)(this.axSuperWorkspace1)).EndInit();
  253. this.groupBox1.ResumeLayout(false);
  254. this.groupBox2.ResumeLayout(false);
  255. this.ResumeLayout(false);
  256. }
  257. #endregion
  258. /// <summary>
  259. /// 应用程序的主入口点。
  260. /// </summary>
  261. [STAThread]
  262. static void Main() 
  263. {
  264. Application.Run(new Form1());
  265. }
  266. private void btn_ok_Click(object sender, System.EventArgs e)
  267. {
  268. database_name = new string[,] {{"ding4","丁4煤层底板等高线及储量计算图"},{"ding56","丁5-6煤层底板等高线及储量计算图"},
  269. {"geng20","庚20煤层底板等高线及储量计算图"},{"ji1617","己16-17煤层底板等高线及储量计算图"},
  270. {"wu8","戊8煤层底板等高线及储量计算图"},{"wu910","戊9-10煤层底板等高线及储量计算图"},
  271. {"pm10x","十线矿井地质剖面图"},{"pm112x","一一二线矿井地质剖面图"},
  272. {"pm113x","一一三线矿井地质剖面图"},{"pm11x","十一线矿井地质剖面图"},
  273. {"pm12x","十二线矿井地质剖面图"},{"pm13x","十三线矿井地质剖面图"},
  274. {"pm14x","十四线矿井地质剖面图"},{"pm1x","一线矿井地质剖面图"},
  275. {"pm20x","二十线矿井地质剖面图"},{"pm22x","二十二线矿井地质剖面图"},
  276. {"pm2x","二线矿井地质剖面图"},{"pm3x","三线矿井地质剖面图"},
  277. {"pm4x","四线矿井地质剖面图"}, {"pm5x","五线矿井地质剖面图"},
  278. {"pm6x","六线矿井地质剖面图"},{"pm7x","七线矿井地质剖面图"},
  279. {"pm8x","八线矿井地质剖面图"},{"pm9x","九线矿井地质剖面图"},
  280. {"diban","某某矿地形地质图"}};
  281. Thread tt = new Thread(new ThreadStart(set_database));
  282. tt.IsBackground = true;
  283. tt.Start();
  284. this.btn_ok.Enabled = false;
  285. }
  286. /// <summary>
  287. /// 如果存在同名的数据库删除。
  288. /// </summary>
  289. private void drop_database()
  290. {
  291. }
  292. private void set_database()
  293. {
  294. string app_url = Application.StartupPath+"\data\";
  295. string ip = this.txt_ip.Text.Trim();
  296. string u_name = this.txt_user.Text.Trim();
  297. string u_pwd = this.txt_pwd.Text.Trim();
  298. string SqlConn = "Data Source="+ip+";Initial Catalog=master;User ID="+u_name+";Password="+u_pwd;
  299. SqlConnection myconn = new SqlConnection(SqlConn);
  300.             this.label5.Text = "准备重建立工作空间数据库";
  301. //先判断是否存在工作空间同名数据库
  302. SqlCommand Cmd = new SqlCommand();
  303. Cmd.CommandType = CommandType.Text;
  304. Cmd.Connection = myconn;
  305. try
  306. {
  307. myconn.Open();
  308. Cmd.CommandText = "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='workspace') DROP DATABASE workspace";
  309. Cmd.ExecuteNonQuery();
  310. //附加工作空间数据库
  311. string csql1 = "CREATE DATABASE workspace ON ( NAME = workspace , FILENAME = '"+app_url+"workspace.mdf' )";
  312. Cmd.CommandText=csql1;
  313. Cmd.ExecuteNonQuery();
  314. string sql= "RESTORE DATABASE workspace FROM DISK = '"+app_url+"workspace' WITH MOVE 'workspace_Data' TO '"+app_url+"workspace.mdf' ,MOVE 'workspace_log' TO '"+app_url+"workspace.log'";
  315. //string sql= "RESTORE DATABASE workspace FROM DISK = '"+app_url+"workspace'";
  316. Cmd.CommandText = sql;
  317. Cmd.ExecuteNonQuery();
  318. this.label5.Text = "工作空间数据库重建完毕....";
  319. }
  320. catch(SqlException ee)
  321. {
  322. MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  323. this.Close();
  324. }
  325. finally
  326. {
  327. myconn.Close();
  328. }
  329. //建立所有的空数据库
  330. try
  331. {
  332. SqlCommand cmd = new SqlCommand();
  333. cmd.CommandType = CommandType.Text;
  334. cmd.Connection = myconn;
  335. myconn.Open();
  336. for(int i=0;i<25;i++)
  337. {
  338. this.label5.Text = "正在建立数据库  "+database_name[i,0];
  339. // 先判断数据库存在不存在
  340. cmd.CommandText = "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='"+database_name[i,0]+ "') DROP DATABASE "+database_name[i,0];
  341. cmd.ExecuteNonQuery();  //删除了同名数据库
  342. //开始建立空的数据库
  343. // cmd.CommandText="create database "+database_name[i,0];
  344. // cmd.ExecuteNonQuery();
  345. string csql = "CREATE DATABASE "+database_name[i,0]+" ON ( NAME =  "+database_name[i,0]+" , FILENAME = '"+app_url+database_name[i,0]+".mdf' )";
  346.     cmd.CommandText = csql;
  347. cmd.ExecuteNonQuery();
  348. }
  349. this.label5.Text = "所有数据库建立完成,准备打开工作空间....";
  350. }
  351. catch(SqlException ee)
  352. {
  353. MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  354. this.Close();
  355. }
  356. finally
  357. {
  358. myconn.Close();
  359. }
  360. //打开工作空间,建立数据集
  361. //打开地图工作空间
  362. string WorkSpaceConn1 =  "UID ="+u_name+";pwd ="+u_pwd;
  363. string WorkSpaceConn0 = "Provider = SQLOLEDB;Driver = SQL Server;SERVER = "+ip+";Database =workspace ;Caption = 某矿信息管理系统;";//这个是工作空间名
  364. this.label5.Text = "正在打开工作空间,准备建立数据源...";
  365. bool aa = this.axSuperWorkspace1.Open(WorkSpaceConn0,WorkSpaceConn1);
  366. if(!aa)
  367. {
  368. MessageBox.Show("工作空间打开失败","系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  369. this.Close();
  370. }
  371. else
  372. {
  373. try
  374. {
  375. for(int i=0;i<25;i++)
  376. {
  377. this.label5.Text = "正在建立数据源  "+database_name[i,1];
  378. //开始一刚建立的数据库建立空的数据集
  379. this.axSuperWorkspace1.CreateDataSource("Provider = SQLOLEDB;Driver = SQL Server;SERVER ="+ip+";Database = "+database_name[i,0],database_name[i,1],SuperMapLib.seEngineType.sceSQLPlus,false,false,false,"uid="+u_name+";pwd="+u_pwd);
  380. //开始恢复数据库
  381. }
  382. this.axSuperWorkspace1.Save();
  383. this.axSuperWorkspace1.Close();
  384. this.label5.Text = "数据源重建成功,准备还原数据库...";
  385. }
  386. catch(SqlException ee)
  387. {
  388. MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  389. this.Close();
  390. }
  391. //还原数据库
  392. try
  393. {
  394. SqlCommand cmd = new SqlCommand();
  395. cmd.CommandType = CommandType.Text;
  396. cmd.Connection = myconn;
  397. myconn.Open();
  398. for(int i=0;i<25;i++)
  399. {
  400. this.label5.Text = "正在还原数据  "+database_name[i,0];
  401. string sql1= "RESTORE DATABASE "+database_name[i,0]+"  FROM disk = '"+app_url+database_name[i,0]+"'  WITH MOVE '"+database_name[i,0]+"_Data' TO '"+app_url+database_name[i,0]+".mdf', MOVE '"+database_name[i,0]+"_log' TO '"+app_url+database_name[i,0]+".log' ";
  402. //string sql1= "RESTORE DATABASE "+database_name[i,0]+"  FROM disk = '"+app_url+database_name[i,0]+"'";
  403. cmd.CommandText = sql1;
  404. cmd.ExecuteNonQuery();
  405. }
  406. MessageBox.Show("GIS服务器设置成功","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  407. }
  408. catch(SqlException ee)
  409. {
  410. MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
  411.     this.Close();
  412. }
  413. finally
  414. {
  415. myconn.Close();
  416. }
  417. }
  418. }
  419. private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
  420. {
  421. if(this.checkBox1.Checked)
  422. {
  423. this.btn_ok.Enabled = true;
  424. }
  425. else
  426. {
  427. this.btn_ok.Enabled = false;
  428. }
  429. }
  430. private void btn_esc_Click(object sender, System.EventArgs e)
  431. {
  432. this.Close();
  433. }
  434. }
  435. }