我在csdn里搜索了很久,没有找到一个符合我要求的登录文档。这次把我的经验和自己的成果分享给大家,希望对后来人有所帮助。这是我第一次做。可能代码不是很规范,思路也不是很清晰,但是能达到我想要的效果。希望有兄弟能帮我完善代码。我在数据库中有一个用户表,如下:ID用户名UserPasswd 1 admin 2 user user 3 guest我准备这么做。首先,我会判断我输入的用户名是否与表中的用户名相同。如果有,我就对比一下同一个用户名下的userpasswd。如果这些都正确,我就可以进入系统了。整个代码如下(我把我的部分加粗了):Form1的代码:使用System使用系统。绘图;使用系统。收藏;使用系统。组件模型;使用系统。Windows . Forms使用系统。数据;使用系统。Data . SqlClient命名空间登录{/// lt;总结 gt///表格1的汇总说明。/// lt;/summary gt;公开课形式1:系统。windows . forms . form { private system。Windows . Forms.Label label1私人系统。Windows . Forms.Label label2私人系统。Windows . Forms.TextBox txtUser私人系统。windows . forms . textbox txtPasswd;私人系统。Windows . Forms.Button btnOK私人系统。Windows . Forms.Button btnCancel/// lt;总结 gt///必需的设计器变量。/// lt;/summary gt;私人系统。ComponentModel.Container组件= nullPublicform1 () {///Windows窗体设计器支持必要的//initialize component();
//// TODO:在InitializeComponent调用之后添加任何构造函数代码//}//< summary gt;///清理所有正在使用的资源。/// lt;/summary gt;受保护的重写void Dispose(bool disposing){ if(disposing){ if(components!= null){组件。dispose();} }基地。Dispose(处置);} #由}#region Windows窗体设计器生成的代码///
this . txt passwd . password char = ' * ';this . txtpasswd . tabindex = 3;this . txtpasswd . text = " ";////Bt NOK//this . Bt NOK . location =新系统。绘图点(40,120);this . Bt NOK . name = " Bt NOK ";this.btnOK.Size =新系统。图纸。尺寸(72,23);this . Bt NOK . tabindex = 4;this . Bt NOK . text = " OK ";this.btnOK.Click +=新系统。EventHandler(this . Bt NOK _ Click);//// btnCancel//
this . BTN cancel . dialog result = System。windows . forms . dialog result . cancel;this.btnCancel.Location =新系统。绘图点(176,120);this . BTN cancel . name = " BTN cancel ";this . BTN cancel . tabindex = 5;this . BTN Cancel . text = " Cancel ";this.btnCancel.Click +=新系统。EventHandler(this . BTN cancel _ Click);//// Form1//this。accept button = this . Bt NOK;这个。AutoScaleBaseSize =新系统。图纸。尺寸(6,14);这个。cancel button = this . BTN cancel;这个。ClientSize =新系统。图纸。尺寸(298,167);这个。controls . Add(this . BTN cancel);这个。controls . Add(this . Bt NOK);这个。controls . Add(this . txtpasswd);这个。controls . Add(this . txt user);这个。controls . Add(this . label 2);这个。controls . Add(this . label 1);这个。FormBorderStyle =System。windows . forms . form border style . fixed dialog;这个。MaximizeBox = false这个。MaximumSize =新系统。图纸。尺寸(304,192);这个。MinimizeBox = false这个。MinimumSize =新系统。图纸。尺寸(304,192);这个。Name = " Form1这个。ShowInTaskbar = false这个。Text = "登录";这个。负荷+=新系统。EventHandler(this。form 1 _ Load);这个。ResumeLayout(false);} # end region/// lt;总结 gt///应用程序的主入口点。/// lt;/summary gt;private void btnCancel_Click(对象发送方,系统。应用程序。exit();}private void Form1_Load(对象发送方,系统。e){this。SetDesktopLocation(280,180);
}私有void btnOK_Click(对象发送方,系统。EventArgs e){
Form2 theOwner = (Form2)this。业主;if(this . txt User . text = = " "){ messagebox . show("用户名不能是空!",“错误”);} else if(this . txtpasswd . text = = " "){ messagebox . show("密码不能空!",“错误”);} else { if(I user(this . txt user . text)){ if(this . txt passwd . text = = log in user(this . txt user . text)){ this。DialogResult = DialogResult。OK;theowner . get username = this . txt user . text;}else{MessageBox。Show("用户名或密码错误!");}}else{MessageBox。Show("用户名或密码错误!");}}
} private string log in User(string User){ SqlConnection conn = new SqlConnection(" XXXXXXXX ");SqlCommand cmd = new SqlCommand();cmd。CommandType =CommandType。存储过程;cmd。CommandText = " Logincmd。连接= connconn . Open();
SQL parameter parName = new SQL parameter(" @ Name ",SqlDbType)。VarChar,50);parName。值=用户;cmd。参数. Add(parName);cmd。ExecuteNonQuery();
conn . Close();sqldata adapter da = new sqldata adapter();达。SelectCommand = cmd数据集ds =新数据集();达。填充(ds);返回ds。表格[0]。行[0]["UserPasswd"]。ToString();} private bool IsUser(string User){ SqlConnection conn = new SqlConnection(" XXXXXXXX ");SqlCommand cmd = new SqlCommand();cmd。CommandType =CommandType。存储过程;cmd。CommandText = " IsUsercmd。连接= connconn . Open();
SQL parameter parName = new SQL parameter(" @ UserName ",SqlDbType)。VarChar,50);parName。值=用户;cmd。参数. Add(parName);cmd。ExecuteNonQuery();
conn . Close();sqldata adapter da = new sqldata adapter();达。SelectCommand = cmd数据集ds =新数据集();达。填充(ds);int n;n = ds。tables[0]. rows . count;如果(n gt0)返回true否则返回false}}}我在Form2中设计了一个标签,接受Form1传来的用户名,这样我们在以后的设计中就可以判断用户权限的大小。表格2的代码:使用系统;使用系统。绘图;使用系统。收藏;使用系统。组件模型;使用系统。Windows . Forms使用系统。数据;使用系统。Data . SqlClient命名空间登录{/// lt;总结 gt///表格2的摘要说明。/// lt;/summary gt;公开课形式2:系统。Windows . Forms.Form {私有字符串用户名;私人系统。Windows . Forms.Label label1/// lt;总结 gt///必需的设计器变量。/// lt;/summary gt;私人系统。ComponentModel.Container组件= nullPublicform2 () {///Windows窗体设计器支持必要的//initialize component();form 1 my form = new form 1();我的表单。ShowDialog(this);
//// TODO:在InitializeComponent调用之后添加任何构造函数代码//}//< summary gt;///清理所有正在使用的资源。/// lt;/summary gt;受保护的重写void Dispose(bool disposing){ if(disposing){ if(components!= null){组件。dispose();} }基地。Dispose(处置);} #由}#region Windows窗体设计器生成的代码///