| 发表于:2007-03-29 17:27:504楼 得分:0 |
//全给你们看 using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.data.sqlclient; namespace hellocsharp { public partial class frm_login : form { private int ilogin; public frm_login() { initializecomponent(); } private void btn_close_click(object sender, eventargs e) { this.close(); } private void frm_login_load(object sender, eventargs e) { } private void btn_login_click(object sender, eventargs e) { bool check; check = checkinput(txt_uid.text.tostring().trim()) && checkinput(txt_pwd.text.tostring()); if(check==ture) { string uid=txt_uid.text.trim().tostring(); string pwd=txt_pwd.text.tostring(); sqlconnection conn = new sqlconnection() ; conn=dbconnect();//调用函数连接数据库 sqlcommand cmd= conn.createcommand() ; cmd.commandtext = "select 1 from t_user where uid= ' " + uid + " 'and pwd= ' " + pwd + " ' "; cmd.commandtype = commandtype.text; sqldataadapter da = new sqldataadapter(cmd); dataset ds = new dataset(); da.fill(ds); if (ds.tables[ "t_user "].rows.count > 0) { this.visible = false; frmmain.show(); cmd.dispose(); conn.close(); conn.dispose(); ds.dispose(); da.dispose(); frm_login.close(); } else { ilogin++; msgbox( "无效用户名或密码 ", "登录失败 ", messageboxbuttons.ok, messageboxicon.exclamation);//调用函数msgbox } } else { msgbox( "请输入用户名和密码 ", "输入数据 ", messageboxbuttons.ok, messageboxicon.exclamation);//调用函数msgbox } } //检查文本框是否为空的函数 private bool checkinput(string s) { bool bflag; if(s.length > 0) return true; else return false; } //数据库连接函数 public sqlconnection dbconnect(string server, string db, string uid, string pwd) { if (server == null) server = "(local) "; if (db == null) db = "db_cfgl "; if (uid == null) uid = "sa "; if (pwd == null) pwd = "houyichong "; string strconn= "server= "+server+ ";database= "+db+ ";integrated security=false;user id= "+uid+ ";password=houyichong "; sqlconnection conn = new sqlconnection(strconn); //conn.connectionstring =strconn; conn.connectiontimeout = 60; conn.open(); return conn; } //定义各种返回客户端的消息函数 private void msgbox(string txt, string title, messageboxbuttons bnt,messageboxicon icon) { messagebox.show(txt, title, bnt,icon ); } } } | | |
|