1、首先将新增用户的页面设计好,小编这边水平有限,做了一个简易的新增用户页面。
2、在添加用戶事件下,先建立数据库的连接(我用的XML连接方式)。using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Xml;namespace ATE{ public partial class Usermanage : Form { private string Read_Xml(string path) { string Source = null; string m_connStr = null; XmlDocument doc = new XmlDocument(); doc.Load(path); XmlElement rootelem = doc.DocumentElement; XmlNodeList personnodes = rootelem.GetElementsByTagName("Data"); foreach (XmlNode node in personnodes) { Source = ((XmlElement)node).GetAttribute("Source"); XmlNodeList database = ((XmlElement)node).GetElementsByTagName("Base"); XmlNodeList dataUid = ((XmlElement)node).GetElementsByTagName("Uid"); XmlNodeList dataPwd = ((XmlElement)node).GetElementsByTagName("Pwd"); m_connStr = "Data Source=" + Source + ";Database=" + database[0].InnerText + ";Uid=" + dataUid[0].InnerText + ";Pwd=" + dataPwd[0].InnerText + ""; } return m_connStr; } public Usermanage() { InitializeComponent(); } private DataSet ds_source(string sqlstring) { SqlConnection conn = new SqlConnection(Read_Xml(Application.StartupPath + "\\Sqlconnection.xml")); SqlCommand cmd = new SqlCommand(sqlstring, conn); SqlDataAdapter dpt = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); dpt.Fill(ds); return ds; } private DataTable dt_source(string sqlstr) { SqlConnection conn = new SqlConnection(Read_Xml(Application.StartupPath + "\\Sqlconnection.xml")); SqlCommand cmd = new SqlCommand(sqlstr, conn); SqlDataAdapter dpt = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); dpt.Fill(dt); return dt; }
3、“新增用戶”事件,首先检查是否存在该新增用户。SqlConnection conn = new SqlConnection(Read_Xml(Application.StartupPath + "\\Sqlconnection.xml")); conn.Open(); SqlCommand cmd1 = new SqlCommand("select count (*) from Employee where User_id='" + textBox1.Text.Trim() + "'", conn); int a = (int)cmd1.ExecuteScalar(); if (a == 1) { MessageBox.Show("该帐号已存在!", "提示"); }
4、如果当前新增用户不存在,首先检查用户名长度:
5、检查密码和重复密码设置是否正取。
6、通过以上步骤,整个新建用户完成,附上完整代码。using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Xml;namespace ATE{ public partial class Usermanage : Form { private string Read_Xml(string path) { string Source = null; string m_connStr = null; XmlDocument doc = new XmlDocument(); doc.Load(path); XmlElement rootelem = doc.DocumentElement; XmlNodeList personnodes = rootelem.GetElementsByTagName("Data"); foreach (XmlNode node in personnodes) { Source = ((XmlElement)node).GetAttribute("Source"); XmlNodeList database = ((XmlElement)node).GetElementsByTagName("Base"); XmlNodeList dataUid = ((XmlElement)node).GetElementsByTagName("Uid"); XmlNodeList dataPwd = ((XmlElement)node).GetElementsByTagName("Pwd"); m_connStr = "Data Source=" + Source + ";Database=" + database[0].InnerText + ";Uid=" + dataUid[0].InnerText + ";Pwd=" + dataPwd[0].InnerText + ""; } return m_connStr; } public Usermanage() { InitializeComponent(); } private DataSet ds_source(string sqlstring) { SqlConnection conn = new SqlConnection(Read_Xml(Application.StartupPath + "\\Sqlconnection.xml")); SqlCommand cmd = new SqlCommand(sqlstring, conn); SqlDataAdapter dpt = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); dpt.Fill(ds); return ds; } private DataTable dt_source(string sqlstr) { SqlConnection conn = new SqlConnection(Read_Xml(Application.StartupPath + "\\Sqlconnection.xml")); SqlCommand cmd = new SqlCommand(sqlstr, conn); SqlDataAdapter dpt = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); dpt.Fill(dt); return dt; } private void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(Read_Xml(Application.StartupPath + "\\Sqlconnection.xml")); conn.Open(); SqlCommand cmd1 = new SqlCommand("select count (*) from Employee where User_id='" + textBox1.Text.Trim() + "'", conn); int a = (int)cmd1.ExecuteScalar(); if (a == 1) { MessageBox.Show("该帐号已存在!", "提示"); } else { string User_id = textBox1.Text.Trim().ToUpper(); string User_name = textBox2.Text.Trim(); string Depart = textBox3.Text.Trim(); string pwd = textBox4.Text.Trim(); if ((textBox1.Text.Length == 7) && (textBox4.Text.Trim() == textBox5.Text.Trim())) { SqlCommand cmd2 = new SqlCommand("insert into Employee values ('" + User_id + "','" + User_name + "','" + Depart + "','" + pwd + "')", conn); int j = (int)cmd2.ExecuteNonQuery(); if (j > 0) { MessageBox.Show("用户新增成功!请牢记!", "提示"); } else { MessageBox.Show("用户新增失败!请检查资料!", "提示"); } } else { MessageBox.Show("资料有误,请检查!", "提示"); } conn.Close(); } }