我在csdn里搜索了很久,也没有找到符合我要求的login文档,我这次把自己的心得和自己做的成果拿出来和大家分享一下,希望对后来的人能有一些帮助。我初次做,可能代码写的不是很规范,思路也不是很清晰,但是它能达到我要的效果就行了' 希望哪位兄弟帮忙完善一下我的代码。
我在数据库里有一个 users 的表,如下:
id username userpasswd
1 admin admin
2 user user
3 guest guest
我准备这样做,先判断输入的用户名是否和表里的username相同,如果相同,再比较相同username下的userpasswd,如果这些都正确了,就可以进入系统了。
全部代码如下(我把我写的部分用黑体):
form1的代码:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.data.sqlclient;
namespace login
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.textbox txtuser;
private system.windows.forms.textbox txtpasswd;
private system.windows.forms.button btnok;
private system.windows.forms.button btncancel;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.txtuser = new system.windows.forms.textbox();
this.txtpasswd = new system.windows.forms.textbox();
this.btnok = new system.windows.forms.button();
this.btncancel = new system.windows.forms.button();
this.suspendlayout();
//
// label1
//
this.label1.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label1.location = new system.drawing.point(40, 24);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(64, 23);
this.label1.tabindex = 0;
this.label1.text = "用户名:";
//
// label2
//
this.label2.font = new system.drawing.font("宋体", 10.5f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label2.location = new system.drawing.point(40, 72);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(56, 23);
this.label2.tabindex = 1;
this.label2.text = "密码:";
//
// txtuser
//
this.txtuser.location = new system.drawing.point(152, 24);
this.txtuser.name = "txtuser";
this.txtuser.tabindex = 2;
this.txtuser.text = "";
//
// txtpasswd
//
this.txtpasswd.location = new system.drawing.point(152, 72);
this.txtpasswd.name = "txtpasswd";
this.txtpasswd.passwordchar = '*';
this.txtpasswd.tabindex = 3;
this.txtpasswd.text = "";
//
// btnok
//
this.btnok.location = new system.drawing.point(40, 120);
this.btnok.name = "btnok";
this.btnok.size = new system.drawing.size(72, 23);
this.btnok.tabindex = 4;
this.btnok.text = "ok";
this.btnok.click += new system.eventhandler(this.btnok_click);
//
// btncancel
//
this.btncancel.dialogresult = system.windows.forms.dialogresult.cancel;
this.btncancel.location = new system.drawing.point(176, 120);
this.btncancel.name = "btncancel";
this.btncancel.tabindex = 5;
this.btncancel.text = "cancel";
this.btncancel.click += new system.eventhandler(this.btncancel_click);
//
// form1
//
this.acceptbutton = this.btnok;
this.autoscalebasesize = new system.drawing.size(6, 14);
this.cancelbutton = this.btncancel;
this.clientsize = new system.drawing.size(298, 167);
this.controls.add(this.btncancel);
this.controls.add(this.btnok);
this.controls.add(this.txtpasswd);
this.controls.add(this.txtuser);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.formborderstyle = system.windows.forms.formborderstyle.fixeddialog;
this.maximizebox = false;
this.maximumsize = new system.drawing.size(304, 192);
this.minimizebox = false;
this.minimumsize = new system.drawing.size(304, 192);
this.name = "form1";
this.showintaskbar = false;
this.text = "login";
this.load += new system.eventhandler(this.form1_load);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
private void btncancel_click(object sender, system.eventargs e)
{
application.exit();
}
private void form1_load(object sender, system.eventargs e)
{
this.setdesktoplocation(280,180);
}
private void btnok_click(object sender, system.eventargs e)
{
form2 theowner = (form2)this.owner;
if(this.txtuser.text == "")
{
messagebox.show("用户名不能为空!","错误");
}
else if(this.txtpasswd.text == "")
{
messagebox.show("密码不能为空!","错误");
}
else
{
if(isuser(this.txtuser.text))
{
if(this.txtpasswd.text == loginuser(this.txtuser.text))
{
this.dialogresult = dialogresult.ok;
theowner.getusername = this.txtuser.text;
}
else
{
messagebox.show("用户名或密码错误!");
}
}
else
{
messagebox.show("用户名或密码错误!");
}
}
}
private string loginuser(string user)
{
sqlconnection conn = new sqlconnection("xxxxxxxx");
sqlcommand cmd = new sqlcommand();
cmd.commandtype = commandtype.storedprocedure;
cmd.commandtext = "login";
cmd.connection = conn;
conn.open();
sqlparameter parname = new sqlparameter("@name",sqldbtype.varchar,50);
parname.value = user;
cmd.parameters.add(parname);
cmd.executenonquery();
conn.close();
sqldataadapter da = new sqldataadapter();
da.selectcommand = cmd;
dataset ds = new dataset();
da.fill(ds);
return ds.tables[0].rows[0]["userpasswd"].tostring();
}
private bool isuser(string user)
{
sqlconnection conn = new sqlconnection("xxxxxxxx");
sqlcommand cmd = new sqlcommand();
cmd.commandtype = commandtype.storedprocedure;
cmd.commandtext = "isuser";
cmd.connection = conn;
conn.open();
sqlparameter parname = new sqlparameter("@username",sqldbtype.varchar,50);
parname.value = user;
cmd.parameters.add(parname);
cmd.executenonquery();
conn.close();
sqldataadapter da = new sqldataadapter();
da.selectcommand = cmd;
dataset ds = new dataset();
da.fill(ds);
int n;
n = ds.tables[0].rows.count;
if(n > 0)
return true;
else
return false;
}
}
}
在form2中我设计了一个label ,让它接受从form1传过来的username,这样我们在以后的设计中好判断用户的权限的大小。
form2的代码:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.data.sqlclient;
namespace login
{
/// <summary>
/// form2 的摘要说明。
/// </summary>
public class form2 : system.windows.forms.form
{
private string username;
private system.windows.forms.label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form2()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
form1 myform = new form1();
myform.showdialog(this);
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.forms.label();
this.suspendlayout();
//
// label1
//
this.label1.location = new system.drawing.point(128, 72);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(152, 40);
this.label1.tabindex = 0;
//
// form2
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(520, 357);
this.controls.add(this.label1);
this.name = "form2";
this.text = "form2";
this.load += new system.eventhandler(this.form2_load);
this.resumelayout(false);
}
#endregion
[stathread]
static void main()
{
application.run(new form2());
}
private void form2_load(object sender, system.eventargs e)
{
this.setdesktoplocation(200,180);
this.label1.text = username;
}
public string getusername
{
get
{
return username;
}
set
{
username = value;
}
}
}
}
新闻热点
疑难解答