C#源代码 创建用户等(1)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public class CUser
{
public string UserID;
public string UserPass;
public string UserName;
public string Memo;
private SqlConnectionStringBuilder scsb;
private SqlConnection conn;
private SqlCommand cmd;
private SqlDataReader dr;
public CUser()
{
scsb = new SqlConnectionStringBuilder();
scsb.DataSource = "(local)";
scsb.InitialCatalog = "UserDB";
scsb.IntegratedSecurity = false;
erID = "student";
scsb.Password = "123456";
conn = new SqlConnection();
conn.ConnectionString = scsb.ConnectionString;
cmd = conn.CreateCommand();
}
public bool Open()
{
try
{
conn.Open();
return true;
}
catch
{
//
}
return false;
}
public void Close()
{
conn.Close();
}
public bool AddUser()
{
mandText = string.Format("insert into TUser
values('{0}','{1}','{2}','{3}')",
UserID, UserPass, UserName, Memo);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{
//
}
return false;
}
public bool ChgPasswd()
{
mandText = string.Format("update TUser set UserPass='{0}' where UserID='{1}'",
UserPass, UserID);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch
{
//
}
return false;
}
public bool CheckPasswd()
{
mandText = string.Format("select * from TUser where UserID='{0}' and UserPass='{1}'",
UserID, UserPass);
dr = cmd.ExecuteReader();
bool hasData = dr.HasRows;
dr.Close();
return hasData;
}
}
}
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void新建用户ToolStripMenuItem_Click(object sender, EventArgs e) {
FormAddUser frm = new FormAddUser();
frm.ShowDialog();
}
private void修改密码ToolStripMenuItem_Click(object sender, EventArgs e) {
FormChgPass frm = new FormChgPass();
frm.ShowDialog();
}
}
}
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class FormAddUser : Form
{
public FormAddUser()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
CUser ObjUser = new CUser();
erID = this.textBoxUserID.Text.Trim(); erPass = this.textBoxPasswd.Text.Trim(); erName = this.textBoxUName.Text.Trim(); ObjUser.Memo = this.textBoxMemo.Text.Trim();
if (ObjUser.Open())
{
if (ObjUser.AddUser())
{
MessageBox.Show("成功添加用户");
}
else
{
MessageBox.Show("添加用户失败");
}
}
else
{
MessageBox.Show("打开数据库失败");
}
ObjUser.Close();
}
}
}
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class FormChgPass : Form
{
public FormChgPass()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CUser userobj = new CUser();
erID = this.textBoxUserID.Text.Trim();
erPass = this.textBoxOldPass.Text.Trim();
if (userobj.Open())
{
if (userobj.CheckPasswd())
{
erPass = this.textBoxNewPass.Text.Trim();
if (userobj.ChgPasswd())
{
MessageBox.Show("密码修改成功");
}
else
{
MessageBox.Show("密码修改失败");
}
}
else
{
MessageBox.Show("密码错误");
}
}
else
{
MessageBox.Show("数据库打开失败");
}
userobj.Close();
}
}
}。