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

其他行业

开发平台:

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.IO;
  9. using System.Threading;
  10. namespace conn
  11. {
  12. /// <summary>
  13. /// Form1 的摘要说明。
  14. /// </summary>
  15. public class Form1 : System.Windows.Forms.Form
  16. {
  17. private System.Windows.Forms.Label label1;
  18. private System.Windows.Forms.Label label3;
  19. private System.Windows.Forms.Label label4;
  20. private System.Windows.Forms.Button button1;
  21. private System.Windows.Forms.Button button2;
  22. private System.Windows.Forms.Button button3;
  23. private System.Windows.Forms.TextBox server_ip;
  24. private System.Windows.Forms.TextBox server_uname;
  25. private System.Windows.Forms.TextBox server_upass;
  26. /// <summary>
  27. /// 必需的设计器变量。
  28. /// </summary>
  29. private System.ComponentModel.Container components = null;
  30. private  string ConnectionString;
  31. private Thread myt;
  32. private System.Windows.Forms.PictureBox pictureBox1;
  33. private SqlConnection myConn = null;
  34. public Form1()
  35. {
  36. //
  37. // Windows 窗体设计器支持所必需的
  38. //
  39. InitializeComponent();
  40. //
  41. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  42. //
  43. }
  44. /// <summary>
  45. /// 清理所有正在使用的资源。
  46. /// </summary>
  47. protected override void Dispose( bool disposing )
  48. {
  49. if( disposing )
  50. {
  51. if (components != null) 
  52. {
  53. components.Dispose();
  54. }
  55. }
  56. base.Dispose( disposing );
  57. }
  58. #region Windows 窗体设计器生成的代码
  59. /// <summary>
  60. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  61. /// 此方法的内容。
  62. /// </summary>
  63. private void InitializeComponent()
  64. {
  65. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
  66. this.label1 = new System.Windows.Forms.Label();
  67. this.label3 = new System.Windows.Forms.Label();
  68. this.label4 = new System.Windows.Forms.Label();
  69. this.button1 = new System.Windows.Forms.Button();
  70. this.button2 = new System.Windows.Forms.Button();
  71. this.button3 = new System.Windows.Forms.Button();
  72. this.server_ip = new System.Windows.Forms.TextBox();
  73. this.server_uname = new System.Windows.Forms.TextBox();
  74. this.server_upass = new System.Windows.Forms.TextBox();
  75. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  76. this.SuspendLayout();
  77. // 
  78. // label1
  79. // 
  80. this.label1.Location = new System.Drawing.Point(32, 16);
  81. this.label1.Name = "label1";
  82. this.label1.Size = new System.Drawing.Size(80, 24);
  83. this.label1.TabIndex = 0;
  84. this.label1.Text = "服务器IP地址";
  85. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  86. // 
  87. // label3
  88. // 
  89. this.label3.Location = new System.Drawing.Point(24, 48);
  90. this.label3.Name = "label3";
  91. this.label3.Size = new System.Drawing.Size(88, 16);
  92. this.label3.TabIndex = 2;
  93. this.label3.Text = "数据库用户名";
  94. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  95. // 
  96. // label4
  97. // 
  98. this.label4.Location = new System.Drawing.Point(40, 80);
  99. this.label4.Name = "label4";
  100. this.label4.Size = new System.Drawing.Size(72, 23);
  101. this.label4.TabIndex = 3;
  102. this.label4.Text = "数据库密码";
  103. this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  104. // 
  105. // button1
  106. // 
  107. this.button1.Location = new System.Drawing.Point(88, 128);
  108. this.button1.Name = "button1";
  109. this.button1.Size = new System.Drawing.Size(72, 24);
  110. this.button1.TabIndex = 4;
  111. this.button1.Text = "测试";
  112. this.button1.Click += new System.EventHandler(this.button1_Click);
  113. // 
  114. // button2
  115. // 
  116. this.button2.Location = new System.Drawing.Point(168, 128);
  117. this.button2.Name = "button2";
  118. this.button2.Size = new System.Drawing.Size(72, 24);
  119. this.button2.TabIndex = 5;
  120. this.button2.Text = "生成文件";
  121. this.button2.Click += new System.EventHandler(this.button2_Click);
  122. // 
  123. // button3
  124. // 
  125. this.button3.Location = new System.Drawing.Point(256, 128);
  126. this.button3.Name = "button3";
  127. this.button3.Size = new System.Drawing.Size(72, 24);
  128. this.button3.TabIndex = 6;
  129. this.button3.Text = "退出";
  130. this.button3.Click += new System.EventHandler(this.button3_Click);
  131. // 
  132. // server_ip
  133. // 
  134. this.server_ip.Location = new System.Drawing.Point(128, 15);
  135. this.server_ip.Name = "server_ip";
  136. this.server_ip.Size = new System.Drawing.Size(112, 21);
  137. this.server_ip.TabIndex = 7;
  138. this.server_ip.Text = "";
  139. // 
  140. // server_uname
  141. // 
  142. this.server_uname.Location = new System.Drawing.Point(128, 48);
  143. this.server_uname.Name = "server_uname";
  144. this.server_uname.Size = new System.Drawing.Size(112, 21);
  145. this.server_uname.TabIndex = 9;
  146. this.server_uname.Text = "";
  147. // 
  148. // server_upass
  149. // 
  150. this.server_upass.Location = new System.Drawing.Point(128, 80);
  151. this.server_upass.Name = "server_upass";
  152. this.server_upass.PasswordChar = '*';
  153. this.server_upass.Size = new System.Drawing.Size(112, 21);
  154. this.server_upass.TabIndex = 10;
  155. this.server_upass.Text = "";
  156. // 
  157. // pictureBox1
  158. // 
  159. this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  160. this.pictureBox1.Location = new System.Drawing.Point(13, 112);
  161. this.pictureBox1.Name = "pictureBox1";
  162. this.pictureBox1.Size = new System.Drawing.Size(328, 4);
  163. this.pictureBox1.TabIndex = 11;
  164. this.pictureBox1.TabStop = false;
  165. // 
  166. // Form1
  167. // 
  168. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  169. this.ClientSize = new System.Drawing.Size(352, 165);
  170. this.Controls.Add(this.pictureBox1);
  171. this.Controls.Add(this.server_upass);
  172. this.Controls.Add(this.server_uname);
  173. this.Controls.Add(this.server_ip);
  174. this.Controls.Add(this.button3);
  175. this.Controls.Add(this.button2);
  176. this.Controls.Add(this.button1);
  177. this.Controls.Add(this.label4);
  178. this.Controls.Add(this.label3);
  179. this.Controls.Add(this.label1);
  180. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  181. this.MaximizeBox = false;
  182. this.Name = "Form1";
  183. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  184. this.Text = "煤矿信息管理系统授权文件生成器";
  185. this.ResumeLayout(false);
  186. }
  187. #endregion
  188. /// <summary>
  189. /// 应用程序的主入口点。
  190. /// </summary>
  191. [STAThread]
  192. static void Main() 
  193. {
  194. Application.Run(new Form1());
  195. }
  196. private void button1_Click(object sender, System.EventArgs e)
  197. {
  198.     string server_ip = this.server_ip.Text.ToString();
  199. string server_db ="workspace";
  200. string server_uname = this.server_uname.Text.ToString();
  201. string server_upass = this.server_upass.Text.ToString();
  202.     ConnectionString = "Server="+server_ip+";database="+server_db+";UID="+server_uname+";Password="+server_upass+"";
  203. myt = new Thread(new ThreadStart(tryConn));
  204.      myt.Start();
  205. }
  206. private void tryConn()
  207. {
  208. string ConnectionString = this.ConnectionString;
  209. try
  210. {
  211. //string ConnectionString = "Server="+server_ip+";database="+server_db+";UID="+server_uname+";Password="+server_upass+"";
  212. myConn = new SqlConnection(ConnectionString);
  213. SqlCommand myCommand = new SqlCommand();
  214. myCommand.CommandText = "select id from users";
  215. myCommand.Connection = myConn;
  216. myConn.Open();
  217. SqlDataReader myData = myCommand.ExecuteReader();
  218. if(myData!= null)
  219. {
  220. MessageBox.Show("测试成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
  221. myConn.Close();
  222. }
  223. else
  224. {
  225. MessageBox.Show("连接错误,请检查输入是否正确!","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  226. }
  227. }
  228. catch(SqlException e)
  229. {
  230. MessageBox.Show("连接错误,请检查输入是否正确!nn错误信息:"+e.Message.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
  231. }
  232. finally
  233. {
  234. myConn.Close();
  235. }
  236. }
  237. private void button2_Click(object sender, System.EventArgs e)
  238. {
  239.  FolderBrowserDialog ff = new FolderBrowserDialog();
  240.  string server_ip = this.server_ip.Text.ToString().Trim();
  241.  string server_db = "workspace";
  242.  string server_uname = this.server_uname.Text.ToString().Trim();
  243.  string server_upass = this.server_upass.Text.ToString().Trim();
  244.  
  245.  server_ip =code_en_de.Encode(server_ip);
  246.  server_db = code_en_de.Encode(server_db);
  247.  server_uname = code_en_de.Encode(server_uname);
  248.  server_upass = code_en_de.Encode(server_upass);
  249.  string connString = server_ip+"|"+server_db+"|"+server_uname+"|"+server_upass;
  250.  StreamWriter sr=null;
  251. if(ff.ShowDialog() == DialogResult.OK)
  252. {
  253. try 
  254. {
  255. string url= ff.SelectedPath;
  256. url = url+"\授权文件.dat";
  257. sr = File.CreateText(url);
  258. sr.WriteLine (connString);
  259. sr.Close();
  260. MessageBox.Show("在指定目录下成功生成授权文件","信息");
  261. }
  262. catch (Exception ea) 
  263. {
  264. MessageBox.Show("错误信息: " + ea.Message);
  265. }
  266. finally                
  267. {
  268. sr.Close();
  269. }
  270. }
  271.  
  272. }
  273. private void button3_Click(object sender, System.EventArgs e)
  274. {
  275. this.Dispose();
  276. Application.Exit();
  277. }
  278. }
  279. }