?? form1.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Threading;
namespace conn
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox server_ip;
private System.Windows.Forms.TextBox server_uname;
private System.Windows.Forms.TextBox server_upass;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
private string ConnectionString;
private Thread myt;
private System.Windows.Forms.PictureBox pictureBox1;
private SqlConnection myConn = 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.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.server_ip = new System.Windows.Forms.TextBox();
this.server_uname = new System.Windows.Forms.TextBox();
this.server_upass = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 24);
this.label1.TabIndex = 0;
this.label1.Text = "服務器IP地址";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(88, 16);
this.label3.TabIndex = 2;
this.label3.Text = "數據庫用戶名";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label4
//
this.label4.Location = new System.Drawing.Point(40, 80);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 3;
this.label4.Text = "數據庫密碼";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 128);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 4;
this.button1.Text = "測試";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 128);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 24);
this.button2.TabIndex = 5;
this.button2.Text = "生成文件";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(256, 128);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(72, 24);
this.button3.TabIndex = 6;
this.button3.Text = "退出";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// server_ip
//
this.server_ip.Location = new System.Drawing.Point(128, 15);
this.server_ip.Name = "server_ip";
this.server_ip.Size = new System.Drawing.Size(112, 21);
this.server_ip.TabIndex = 7;
this.server_ip.Text = "";
//
// server_uname
//
this.server_uname.Location = new System.Drawing.Point(128, 48);
this.server_uname.Name = "server_uname";
this.server_uname.Size = new System.Drawing.Size(112, 21);
this.server_uname.TabIndex = 9;
this.server_uname.Text = "";
//
// server_upass
//
this.server_upass.Location = new System.Drawing.Point(128, 80);
this.server_upass.Name = "server_upass";
this.server_upass.PasswordChar = '*';
this.server_upass.Size = new System.Drawing.Size(112, 21);
this.server_upass.TabIndex = 10;
this.server_upass.Text = "";
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Location = new System.Drawing.Point(13, 112);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(328, 4);
this.pictureBox1.TabIndex = 11;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 165);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.server_upass);
this.Controls.Add(this.server_uname);
this.Controls.Add(this.server_ip);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "煤礦信息管理系統授權文件生成器";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
string server_ip = this.server_ip.Text.ToString();
string server_db ="workspace";
string server_uname = this.server_uname.Text.ToString();
string server_upass = this.server_upass.Text.ToString();
ConnectionString = "Server="+server_ip+";database="+server_db+";UID="+server_uname+";Password="+server_upass+"";
myt = new Thread(new ThreadStart(tryConn));
myt.Start();
}
private void tryConn()
{
string ConnectionString = this.ConnectionString;
try
{
//string ConnectionString = "Server="+server_ip+";database="+server_db+";UID="+server_uname+";Password="+server_upass+"";
myConn = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "select id from users";
myCommand.Connection = myConn;
myConn.Open();
SqlDataReader myData = myCommand.ExecuteReader();
if(myData!= null)
{
MessageBox.Show("測試成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
myConn.Close();
}
else
{
MessageBox.Show("連接錯誤,請檢查輸入是否正確!","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
catch(SqlException e)
{
MessageBox.Show("連接錯誤,請檢查輸入是否正確!\n\n錯誤信息:"+e.Message.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
myConn.Close();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
FolderBrowserDialog ff = new FolderBrowserDialog();
string server_ip = this.server_ip.Text.ToString().Trim();
string server_db = "workspace";
string server_uname = this.server_uname.Text.ToString().Trim();
string server_upass = this.server_upass.Text.ToString().Trim();
server_ip =code_en_de.Encode(server_ip);
server_db = code_en_de.Encode(server_db);
server_uname = code_en_de.Encode(server_uname);
server_upass = code_en_de.Encode(server_upass);
string connString = server_ip+"|"+server_db+"|"+server_uname+"|"+server_upass;
StreamWriter sr=null;
if(ff.ShowDialog() == DialogResult.OK)
{
try
{
string url= ff.SelectedPath;
url = url+"\\授權文件.dat";
sr = File.CreateText(url);
sr.WriteLine (connString);
sr.Close();
MessageBox.Show("在指定目錄下成功生成授權文件","信息");
}
catch (Exception ea)
{
MessageBox.Show("錯誤信息: " + ea.Message);
}
finally
{
sr.Close();
}
}
}
private void button3_Click(object sender, System.EventArgs e)
{
this.Dispose();
Application.Exit();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -