Form1.cs
上传用户:chizxy
上传日期:2014-11-29
资源大小:407k
文件大小:16k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- using System.Threading;
- namespace serversetup
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
-
- private string[,] database_name;
- private System.Windows.Forms.PictureBox pictureBox1;
- private System.Windows.Forms.Button btn_esc;
- private System.Windows.Forms.Button btn_ok;
- private System.Windows.Forms.TextBox txt_user;
- private System.Windows.Forms.TextBox txt_pwd;
- private System.Windows.Forms.TextBox txt_ip;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label1;
- private AxSuperMapLib.AxSuperWorkspace axSuperWorkspace1;
- private System.Windows.Forms.Label label5;
- private System.Windows.Forms.TextBox textBox1;
- private System.Windows.Forms.CheckBox checkBox1;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.GroupBox groupBox2;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.btn_esc = new System.Windows.Forms.Button();
- this.btn_ok = new System.Windows.Forms.Button();
- this.txt_user = new System.Windows.Forms.TextBox();
- this.txt_pwd = new System.Windows.Forms.TextBox();
- this.txt_ip = new System.Windows.Forms.TextBox();
- this.label3 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label1 = new System.Windows.Forms.Label();
- this.axSuperWorkspace1 = new AxSuperMapLib.AxSuperWorkspace();
- this.label5 = new System.Windows.Forms.Label();
- this.textBox1 = new System.Windows.Forms.TextBox();
- this.checkBox1 = new System.Windows.Forms.CheckBox();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- ((System.ComponentModel.ISupportInitialize)(this.axSuperWorkspace1)).BeginInit();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.SuspendLayout();
- //
- // pictureBox1
- //
- this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
- this.pictureBox1.Location = new System.Drawing.Point(8, 184);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(560, 4);
- this.pictureBox1.TabIndex = 19;
- this.pictureBox1.TabStop = false;
- //
- // btn_esc
- //
- this.btn_esc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
- this.btn_esc.Location = new System.Drawing.Point(456, 192);
- this.btn_esc.Name = "btn_esc";
- this.btn_esc.TabIndex = 18;
- this.btn_esc.Text = "关闭";
- this.btn_esc.Click += new System.EventHandler(this.btn_esc_Click);
- //
- // btn_ok
- //
- this.btn_ok.Enabled = false;
- this.btn_ok.Location = new System.Drawing.Point(344, 192);
- this.btn_ok.Name = "btn_ok";
- this.btn_ok.TabIndex = 17;
- this.btn_ok.Text = "确定";
- this.btn_ok.Click += new System.EventHandler(this.btn_ok_Click);
- //
- // txt_user
- //
- this.txt_user.Location = new System.Drawing.Point(96, 56);
- this.txt_user.Name = "txt_user";
- this.txt_user.Size = new System.Drawing.Size(158, 21);
- this.txt_user.TabIndex = 13;
- this.txt_user.Text = "";
- //
- // txt_pwd
- //
- this.txt_pwd.Location = new System.Drawing.Point(96, 88);
- this.txt_pwd.Name = "txt_pwd";
- this.txt_pwd.PasswordChar = '*';
- this.txt_pwd.Size = new System.Drawing.Size(158, 21);
- this.txt_pwd.TabIndex = 15;
- this.txt_pwd.Text = "";
- //
- // txt_ip
- //
- this.txt_ip.Location = new System.Drawing.Point(96, 24);
- this.txt_ip.Name = "txt_ip";
- this.txt_ip.Size = new System.Drawing.Size(158, 21);
- this.txt_ip.TabIndex = 11;
- this.txt_ip.Text = "";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(8, 24);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(80, 23);
- this.label3.TabIndex = 16;
- this.label3.Text = "服务器IP地址";
- this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(16, 88);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(72, 24);
- this.label2.TabIndex = 14;
- this.label2.Text = "用户密码";
- this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(8, 56);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(80, 23);
- this.label1.TabIndex = 12;
- this.label1.Text = "数据库用户名";
- this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
- //
- // axSuperWorkspace1
- //
- this.axSuperWorkspace1.Enabled = true;
- this.axSuperWorkspace1.Location = new System.Drawing.Point(176, 144);
- this.axSuperWorkspace1.Name = "axSuperWorkspace1";
- this.axSuperWorkspace1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSuperWorkspace1.OcxState")));
- this.axSuperWorkspace1.Size = new System.Drawing.Size(32, 32);
- this.axSuperWorkspace1.TabIndex = 22;
- //
- // label5
- //
- this.label5.Location = new System.Drawing.Point(40, 160);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(520, 16);
- this.label5.TabIndex = 23;
- this.label5.Text = "提示信息";
- this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
- //
- // textBox1
- //
- this.textBox1.Location = new System.Drawing.Point(8, 16);
- this.textBox1.Multiline = true;
- this.textBox1.Name = "textBox1";
- this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
- this.textBox1.Size = new System.Drawing.Size(264, 96);
- this.textBox1.TabIndex = 24;
- this.textBox1.Text = "系统将创建以下数据库,如果系统中有同名的库,将被覆盖。 此操作将不可撤消,您确认执行本次操作?rnrn数据库列表:rn workspace pm1x " +
- "pm2x pm3x pm4x pm5x pm6x pm7x pm8x pm9x pm10x pm11x pm12x pm13x pm" +
- "14x pm112x pm113x wu8 wu910 ji1617 geng20 ding56 ding4 dibanrn ";
- //
- // checkBox1
- //
- this.checkBox1.ForeColor = System.Drawing.Color.Red;
- this.checkBox1.Location = new System.Drawing.Point(24, 114);
- this.checkBox1.Name = "checkBox1";
- this.checkBox1.Size = new System.Drawing.Size(240, 24);
- this.checkBox1.TabIndex = 26;
- this.checkBox1.Text = "我已经仔细阅读了警告,确认执行操作";
- this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.checkBox1);
- this.groupBox1.Controls.Add(this.textBox1);
- this.groupBox1.ForeColor = System.Drawing.Color.Red;
- this.groupBox1.Location = new System.Drawing.Point(16, 8);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(280, 144);
- this.groupBox1.TabIndex = 27;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "警告";
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.txt_pwd);
- this.groupBox2.Controls.Add(this.txt_ip);
- this.groupBox2.Controls.Add(this.label3);
- this.groupBox2.Controls.Add(this.label2);
- this.groupBox2.Controls.Add(this.label1);
- this.groupBox2.Controls.Add(this.txt_user);
- this.groupBox2.Location = new System.Drawing.Point(304, 8);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(264, 144);
- this.groupBox2.TabIndex = 28;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "参数设置";
- //
- // Form1
- //
- this.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(584, 229);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.label5);
- this.Controls.Add(this.axSuperWorkspace1);
- this.Controls.Add(this.pictureBox1);
- this.Controls.Add(this.btn_esc);
- this.Controls.Add(this.btn_ok);
- this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
- this.MaximizeBox = false;
- this.MinimizeBox = false;
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "GIS服务器设置";
- ((System.ComponentModel.ISupportInitialize)(this.axSuperWorkspace1)).EndInit();
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void btn_ok_Click(object sender, System.EventArgs e)
- {
- database_name = new string[,] {{"ding4","丁4煤层底板等高线及储量计算图"},{"ding56","丁5-6煤层底板等高线及储量计算图"},
- {"geng20","庚20煤层底板等高线及储量计算图"},{"ji1617","己16-17煤层底板等高线及储量计算图"},
- {"wu8","戊8煤层底板等高线及储量计算图"},{"wu910","戊9-10煤层底板等高线及储量计算图"},
- {"pm10x","十线矿井地质剖面图"},{"pm112x","一一二线矿井地质剖面图"},
- {"pm113x","一一三线矿井地质剖面图"},{"pm11x","十一线矿井地质剖面图"},
- {"pm12x","十二线矿井地质剖面图"},{"pm13x","十三线矿井地质剖面图"},
- {"pm14x","十四线矿井地质剖面图"},{"pm1x","一线矿井地质剖面图"},
- {"pm20x","二十线矿井地质剖面图"},{"pm22x","二十二线矿井地质剖面图"},
- {"pm2x","二线矿井地质剖面图"},{"pm3x","三线矿井地质剖面图"},
- {"pm4x","四线矿井地质剖面图"}, {"pm5x","五线矿井地质剖面图"},
- {"pm6x","六线矿井地质剖面图"},{"pm7x","七线矿井地质剖面图"},
- {"pm8x","八线矿井地质剖面图"},{"pm9x","九线矿井地质剖面图"},
- {"diban","某某矿地形地质图"}};
- Thread tt = new Thread(new ThreadStart(set_database));
- tt.IsBackground = true;
- tt.Start();
- this.btn_ok.Enabled = false;
- }
- /// <summary>
- /// 如果存在同名的数据库删除。
- /// </summary>
- private void drop_database()
- {
- }
- private void set_database()
- {
-
-
- string app_url = Application.StartupPath+"\data\";
- string ip = this.txt_ip.Text.Trim();
- string u_name = this.txt_user.Text.Trim();
- string u_pwd = this.txt_pwd.Text.Trim();
- string SqlConn = "Data Source="+ip+";Initial Catalog=master;User ID="+u_name+";Password="+u_pwd;
- SqlConnection myconn = new SqlConnection(SqlConn);
- this.label5.Text = "准备重建立工作空间数据库";
- //先判断是否存在工作空间同名数据库
- SqlCommand Cmd = new SqlCommand();
- Cmd.CommandType = CommandType.Text;
- Cmd.Connection = myconn;
- try
- {
- myconn.Open();
- Cmd.CommandText = "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='workspace') DROP DATABASE workspace";
- Cmd.ExecuteNonQuery();
-
-
- //附加工作空间数据库
- string csql1 = "CREATE DATABASE workspace ON ( NAME = workspace , FILENAME = '"+app_url+"workspace.mdf' )";
- Cmd.CommandText=csql1;
- Cmd.ExecuteNonQuery();
- 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'";
- //string sql= "RESTORE DATABASE workspace FROM DISK = '"+app_url+"workspace'";
- Cmd.CommandText = sql;
- Cmd.ExecuteNonQuery();
- this.label5.Text = "工作空间数据库重建完毕....";
- }
- catch(SqlException ee)
- {
- MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- this.Close();
- }
- finally
- {
- myconn.Close();
- }
- //建立所有的空数据库
- try
- {
- SqlCommand cmd = new SqlCommand();
- cmd.CommandType = CommandType.Text;
- cmd.Connection = myconn;
- myconn.Open();
- for(int i=0;i<25;i++)
- {
- this.label5.Text = "正在建立数据库 "+database_name[i,0];
- // 先判断数据库存在不存在
- cmd.CommandText = "IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name ='"+database_name[i,0]+ "') DROP DATABASE "+database_name[i,0];
- cmd.ExecuteNonQuery(); //删除了同名数据库
- //开始建立空的数据库
- // cmd.CommandText="create database "+database_name[i,0];
- // cmd.ExecuteNonQuery();
- string csql = "CREATE DATABASE "+database_name[i,0]+" ON ( NAME = "+database_name[i,0]+" , FILENAME = '"+app_url+database_name[i,0]+".mdf' )";
- cmd.CommandText = csql;
- cmd.ExecuteNonQuery();
- }
- this.label5.Text = "所有数据库建立完成,准备打开工作空间....";
- }
- catch(SqlException ee)
- {
- MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- this.Close();
- }
- finally
- {
- myconn.Close();
- }
- //打开工作空间,建立数据集
- //打开地图工作空间
- string WorkSpaceConn1 = "UID ="+u_name+";pwd ="+u_pwd;
- string WorkSpaceConn0 = "Provider = SQLOLEDB;Driver = SQL Server;SERVER = "+ip+";Database =workspace ;Caption = 某矿信息管理系统;";//这个是工作空间名
- this.label5.Text = "正在打开工作空间,准备建立数据源...";
- bool aa = this.axSuperWorkspace1.Open(WorkSpaceConn0,WorkSpaceConn1);
- if(!aa)
- {
- MessageBox.Show("工作空间打开失败","系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- this.Close();
- }
- else
- {
- try
- {
- for(int i=0;i<25;i++)
- {
- this.label5.Text = "正在建立数据源 "+database_name[i,1];
- //开始一刚建立的数据库建立空的数据集
- 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);
- //开始恢复数据库
- }
- this.axSuperWorkspace1.Save();
- this.axSuperWorkspace1.Close();
- this.label5.Text = "数据源重建成功,准备还原数据库...";
- }
- catch(SqlException ee)
- {
- MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- this.Close();
-
- }
-
- //还原数据库
- try
- {
- SqlCommand cmd = new SqlCommand();
- cmd.CommandType = CommandType.Text;
- cmd.Connection = myconn;
- myconn.Open();
- for(int i=0;i<25;i++)
- {
- this.label5.Text = "正在还原数据 "+database_name[i,0];
- 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' ";
- //string sql1= "RESTORE DATABASE "+database_name[i,0]+" FROM disk = '"+app_url+database_name[i,0]+"'";
- cmd.CommandText = sql1;
- cmd.ExecuteNonQuery();
- }
- MessageBox.Show("GIS服务器设置成功","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
- }
- catch(SqlException ee)
- {
- MessageBox.Show("错误信息:"+ee.Message,"系统错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
- this.Close();
- }
- finally
- {
- myconn.Close();
- }
- }
-
-
- }
- private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
- {
- if(this.checkBox1.Checked)
- {
- this.btn_ok.Enabled = true;
- }
- else
- {
- this.btn_ok.Enabled = false;
- }
- }
- private void btn_esc_Click(object sender, System.EventArgs e)
- {
- this.Close();
- }
- }
- }