📄 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 + -