语言c#,数据库Access,
数据库名为A.mdb,有一表B,表中有UserID和Pswd两列,
现有Form1,上面有textbox1,textbox2,button1
Form2中有label1,学艺不精啊.....虽然简单,可是我写了,好多个错误,根本不知道从何修改只有向高手请教....现在我实现的功能如下:(和普通的用户登陆无两样)在textbox1,textbox2中分别输入内容,单击button1后
textbox1,textbox2分别连接到数据库的UserID(帐户)和Pswd(密码)两列,
并且内容一致的话,连接到Form2,并且Form2中的labl1显示为Form1 textbox1的内容...如果密码错误显示"密码错误"信息
如果不存在帐户,提示"没有此用户"先向看贴的高手们致敬,
向发代码的前辈表示至高的感谢....高手有空请帮帮忙,给小弟发段代码,让小弟和自己的对比对比,学习学习
数据库名为A.mdb,有一表B,表中有UserID和Pswd两列,
现有Form1,上面有textbox1,textbox2,button1
Form2中有label1,学艺不精啊.....虽然简单,可是我写了,好多个错误,根本不知道从何修改只有向高手请教....现在我实现的功能如下:(和普通的用户登陆无两样)在textbox1,textbox2中分别输入内容,单击button1后
textbox1,textbox2分别连接到数据库的UserID(帐户)和Pswd(密码)两列,
并且内容一致的话,连接到Form2,并且Form2中的labl1显示为Form1 textbox1的内容...如果密码错误显示"密码错误"信息
如果不存在帐户,提示"没有此用户"先向看贴的高手们致敬,
向发代码的前辈表示至高的感谢....高手有空请帮帮忙,给小弟发段代码,让小弟和自己的对比对比,学习学习
SqlConnection con = new SqlConnection(constring);
SqlCommand com = new SqlCommand();
com.CommandText = "select id,name from info where id=@id";
com.Parameters.AddWithValue("id",this.textBox1.Text.Trim());
com.Connection = con;
SqlDataAdapter ap = new SqlDataAdapter(com);
DataTable dt = new DataTable();
ap.Fill(dt); if (dt.Rows.Count == 0)
{
MessageBox.Show("该用户不存在!");
return;
}
if ((dt.Rows[0]["id"].ToString() == this.textBox1.Text.Trim()) && (dt.Rows[0]["name"].ToString() == this.textBox2.Text.Trim()))
{
MessageBox.Show(dt.Rows[0]["name"].ToString() + " 登录成功!");
return;
}
MessageBox.Show("登录失败!密码错误!");
呵
Create Table UserInfomation--用于存放用户信息
(
Uid varchar(20) constraint PK_UserInformation_Uid primary key,--设为该表的主键
Pwd varchar(20) not null,
UserTitle varchar(20) constraint Ck_UserInformation_UserTitle Check (UserTitle in ('Sysadmin','Dbadmin'))--设置用户权限
)
Client:
class User
{
private string uid;
private string pwd;
private string userTitle==null;public User(){}
public User(string uid,string pwd,string userTitle)
{
this.uid=uid;
this.pwd=pwd;
this.userTitle=userTitle;
}}
//===========主程序===========
class Main
{
private static User user=new user();//程序启动时新建user实例,用于存放启动该程序的用户信息
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); if(user.userTitle!=null)
{
Aplicateion.Run(new Form2());
}}}//===================Form1============
using System.data;
using System.data.sqlclient;private void ButtonLogin_Click(Object sender,EventArgs e)//==单机登陆按扭触发验证事件
{
string uid=this.textUid.text.Tostring();
string pwd=this.textPwd.text.Tostring();sqlconnection con = new sqlconnection("Server=localhost;Database=数据库名称;uid=sa;pwd=;")
con.open();
string strCMD=string.Format("Select UserTitle From UserInfomation Where Uid='{0}' and Pwd='{1}'",uid,pwd);
sqlcommand cmd = new sqlcommand(strCMD,con);string title =Convert .ToString ( cmd.ExecuteScalar());//读出用户的权限
if (title==""||title==null)
{
System.Windows.Forms.MessageBox.Show("登陆失败");
con.Close();
cmd.Dispose();
return false;
}
else
{
主程序空间名称.user=new (uid,pwd,title);//登陆成功,将该用户信息保存到程序中
con.Close();
cmd.Dispose();
System.Windows.Forms.MessageBox.Show("登陆成功");
return true;
}
}//大致是这样写,还为加异常处理
{
System.Windows.Forms.MessageBox.Show("登陆失败");
con.Close();
cmd.Dispose();
retrun;
}
else
{
主程序空间名称.user=new (uid,pwd,title);//登陆成功,将该用户信息保存到程序中
con.Close();
cmd.Dispose();
System.Windows.Forms.MessageBox.Show("登陆成功");
this.close();//将本窗体关闭,回到主程序判断是否继续执行下一窗体
}
}