?? adduser.cs
字號:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication4
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class AddUser : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label4;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private string NewID;
private string NewPwd;
private string NewGrp;
private System.Data.SqlClient.SqlDataAdapter AddAdapter;
private System.Windows.Forms.ComboBox comboGrp;
private System.Windows.Forms.TextBox textPwd;
private System.Windows.Forms.TextBox textID;
private System.Windows.Forms.Button enter_button;
private System.Windows.Forms.Button esc_button;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
//private DataSet UserInfoSet=new DataSet();
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public AddUser()
{
//
// 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()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.comboGrp = new System.Windows.Forms.ComboBox();
this.textPwd = new System.Windows.Forms.TextBox();
this.textID = 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.groupBox2 = new System.Windows.Forms.GroupBox();
this.label4 = new System.Windows.Forms.Label();
this.enter_button = new System.Windows.Forms.Button();
this.esc_button = new System.Windows.Forms.Button();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.AddAdapter = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.comboGrp);
this.groupBox1.Controls.Add(this.textPwd);
this.groupBox1.Controls.Add(this.textID);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Location = new System.Drawing.Point(32, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(256, 160);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "新用戶信息";
//
// comboGrp
//
this.comboGrp.Items.AddRange(new object[] {
"1-經理人員",
"2-倉庫管理員",
"3-普通職工"});
this.comboGrp.Location = new System.Drawing.Point(144, 104);
this.comboGrp.Name = "comboGrp";
this.comboGrp.Size = new System.Drawing.Size(96, 20);
this.comboGrp.TabIndex = 5;
//
// textPwd
//
this.textPwd.Location = new System.Drawing.Point(144, 72);
this.textPwd.Name = "textPwd";
this.textPwd.PasswordChar = '*';
this.textPwd.Size = new System.Drawing.Size(96, 21);
this.textPwd.TabIndex = 4;
this.textPwd.Text = "";
//
// textID
//
this.textID.Location = new System.Drawing.Point(144, 32);
this.textID.Name = "textID";
this.textID.Size = new System.Drawing.Size(96, 21);
this.textID.TabIndex = 3;
this.textID.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 112);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 24);
this.label3.TabIndex = 2;
this.label3.Text = "*請輸入用戶權限";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(136, 24);
this.label2.TabIndex = 1;
this.label2.Text = "*請輸入新用戶密碼:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 32);
this.label1.TabIndex = 0;
this.label1.Text = "*請輸入新用戶ID:";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Location = new System.Drawing.Point(312, 56);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(96, 104);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 17);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 80);
this.label4.TabIndex = 0;
this.label4.Text = "提示:請牢記您的ID和密碼,這是您登陸系統的必要信息。";
//
// enter_button
//
this.enter_button.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.enter_button.Location = new System.Drawing.Point(80, 200);
this.enter_button.Name = "enter_button";
this.enter_button.Size = new System.Drawing.Size(72, 24);
this.enter_button.TabIndex = 2;
this.enter_button.Text = "確定";
this.enter_button.Click += new System.EventHandler(this.enter_button_Click);
//
// esc_button
//
this.esc_button.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.esc_button.Location = new System.Drawing.Point(280, 200);
this.esc_button.Name = "esc_button";
this.esc_button.Size = new System.Drawing.Size(72, 24);
this.esc_button.TabIndex = 3;
this.esc_button.Text = "退出";
this.esc_button.Click += new System.EventHandler(this.esc_button_Click);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=WKX;packet size=4096;user id=sa;data source=\".\";persist security i" +
"nfo=False;initial catalog=storage";
//
// AddAdapter
//
this.AddAdapter.DeleteCommand = this.sqlDeleteCommand1;
this.AddAdapter.InsertCommand = this.sqlInsertCommand1;
this.AddAdapter.SelectCommand = this.sqlSelectCommand1;
this.AddAdapter.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "LonInFrm", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("UserID", "UserID"),
new System.Data.Common.DataColumnMapping("Password", "Password"),
new System.Data.Common.DataColumnMapping("GroupID", "GroupID")})});
this.AddAdapter.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM LonInFrm WHERE (UserID = @Original_UserID) AND (GroupID = @Original_G" +
"roupID OR @Original_GroupID IS NULL AND GroupID IS NULL) AND (Password = @Origin" +
"al_Password OR @Original_Password IS NULL AND Password IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UserID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UserID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_GroupID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "GroupID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Password", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Password", System.Data.DataRowVersion.Original, null));
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO LonInFrm(UserID, Password, GroupID) VALUES (@UserID, @Password, @Grou" +
"pID);SELECT UserID, Password, GroupID FROM LonInFrm WHERE (UserID = @UserID)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.VarChar, 10, "UserID"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 10, "Password"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@GroupID", System.Data.SqlDbType.VarChar, 10, "GroupID"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT UserID, Password, GroupID FROM LonInFrm";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE LonInFrm SET UserID = @UserID, Password = @Password, GroupID = @GroupID WHERE (UserID = @Original_UserID) AND (GroupID = @Original_GroupID OR @Original_GroupID IS NULL AND GroupID IS NULL) AND (Password = @Original_Password OR @Original_Password IS NULL AND Password IS NULL); SELECT UserID, Password, GroupID FROM LonInFrm WHERE (UserID = @UserID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.VarChar, 10, "UserID"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 10, "Password"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@GroupID", System.Data.SqlDbType.VarChar, 10, "GroupID"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_UserID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "UserID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_GroupID", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "GroupID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Password", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Password", System.Data.DataRowVersion.Original, null));
//
// AddUser
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 254);
this.Controls.Add(this.esc_button);
this.Controls.Add(this.enter_button);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "AddUser";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "添加用戶";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void enter_button_Click(object sender, System.EventArgs e)
{
if(textID.Text.Trim()==string.Empty)//判斷用戶ID是否輸入
{
MessageBox.Show("請輸入用戶ID或密碼");
return;
}
NewID=this.textID .Text.Trim();
if( textPwd.Text.Trim() == string.Empty )//判斷用戶密碼是否輸入
{
MessageBox.Show("請輸入用戶ID或密碼");
return;
}
NewPwd=this.textPwd .Text.Trim();
if(this.comboGrp.SelectedItem.Equals("1-經理人員"))//選擇權限
{
NewGrp="1";
}
else
{
if(this.comboGrp.SelectedItem.Equals("2-倉庫管理員"))
{
NewGrp="2";
}
else
{
NewGrp="3";
}
}
SqlConnection myConn = null;
SqlTransaction UserInsertTra = null;
DataSet UserInfoSet = new DataSet();
try
{
myConn = new SqlConnection("server=.;Trusted_Connection=yes;user=sa;pwd=1;database=storage");//建立連接
AddAdapter.SelectCommand.Connection=myConn;
AddAdapter.SelectCommand.CommandText="SELECT * FROM LonInFrm WHERE UserID='"+NewID+"'";
myConn.Open();
AddAdapter.Fill(UserInfoSet,"UserInfo");//填充數據集
if(UserInfoSet.Tables["UserInfo"].Rows.Count!=0)//判斷輸入的ID號是否唯一
{
MessageBox.Show("員工ID必須唯一!");
return;
}
UserInsertTra = myConn.BeginTransaction();
UserInfoSet.Clear();
DataRow NewUserInfoRow = UserInfoSet.Tables["UserInfo"].NewRow();//建立新記錄
NewUserInfoRow["UserID"] = NewID;
NewUserInfoRow["Password"] = NewPwd;
NewUserInfoRow["GroupID"] = NewGrp;
UserInfoSet.Tables[0].Rows.Add(NewUserInfoRow);
AddAdapter.SelectCommand.Connection = myConn;
AddAdapter.InsertCommand.Connection = myConn;
AddAdapter.UpdateCommand.Connection = myConn;
AddAdapter.InsertCommand.Transaction = UserInsertTra;
this.AddAdapter.Update(UserInfoSet.Tables["UserInfo"]);//更新數據集
UserInsertTra.Commit();
MessageBox.Show("OK");
}
catch(Exception ex)
{
if( UserInsertTra != null )
{
UserInsertTra.Rollback();
}
MessageBox.Show(ex.Message);
}
}
private void esc_button_Click(object sender, System.EventArgs e)//退出操作
{
this.Close();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -