C#中数据库的问题~~~~~

tckoo 2009-03-09 05:43:13
我使用的sql server,在VS新建了一个WinForm窗体,输入用户名密码进行登录。设置了几个特定的账号密码保存在数据库中,保存账号密码的表为Account,列UserName和PassWord保存的是账号和密码,我应该怎样写代码来获取数据库中的账号和密码,将其与输入的账号密码进行比较,完成登录呢?请各位高手们帮帮忙啊~~谢谢了~~~~~~
...全文
93 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Stephen_Kang 2009-03-09
  • 打赏
  • 举报
回复
哦!不好意思,要将关闭数据库放到if(jduge){}里面去,呵呵,
Stephen_Kang 2009-03-09
  • 打赏
  • 举报
回复
你在窗体上应该有两个文本框吧!一个是输入账户的,另外一个是输入密码的!(前者用txtUser表示,后者用txtPass表示)
在“登录”按钮的click事件中写一下代码:
{前提是连接数据库,且连接数据库在一个公共类里,如类DBHelper类}
1.先写个方法判断是否有一个文本框为空!
public bool jduge()
{
if (txtUser.text == "" || txtPass.text == "")
{
messageBox.Show("请输入完整信息!");
return false;
}else
{
return true;
}
}
2.click事件中的代码:
if(jduge())
{
SqlCommand command = con.CreateCommand();
DBHelper.con.open();
string sql = string.Format("select count(*) from Account where UserName = {0} and PassWord = {1} ",txtUser.text,txtPass.text);
int result = (int)command.ExecuteReader();
if(result == 1)
{
messageBox.Show("登录成功!");
(在这可以用窗体跳转:主界面.show())
}else
{
messageBox.Show("登录失败!");
}
}
DBHelper.con.close();
最好是,将上面的代码放到try{}catch{}finnly{}中将关闭数据库放到finnly里面,可以检查出程序的异常,以便积累编程的经验!





TkingCN 2009-03-09
  • 打赏
  • 举报
回复
int score=0;
select score=count(*)
from Account
where UserName=账号 and PassWord=密码
if(score>0)
{
messagebox.show("登陆成功")
}else
{
messagebox.show("登陆失败")
}
UN_Solomon 2009-03-09
  • 打赏
  • 举报
回复
3楼的可以了
a21999 2009-03-09
  • 打赏
  • 举报
回复
上面的代码已经给全了,不错,拷贝过去你就能用了。
tckoo 2009-03-09
  • 打赏
  • 举报
回复
麻烦谁说得详细点啊?我对数据库不是好熟哦,嘿嘿~谢谢大家哦~
PandaIT 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xisimao 的回复:]
打开数据库
SqlConnection con = SQLConnection.createConnection();
con.Open();

建立数据库命令
SqlCommand command = con.CreateCommand();
command.CommandText = "select * from Login";

利用SqlDataReader读取数据
SqlDataReader reader = command.ExecuteReader();
while (reader.Rea…
[/Quote]

多此一举!
xisimao 2009-03-09
  • 打赏
  • 举报
回复
打开数据库
SqlConnection con = SQLConnection.createConnection();
con.Open();

建立数据库命令
SqlCommand command = con.CreateCommand();
command.CommandText = "select * from Login";

利用SqlDataReader读取数据
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (rbtnName.Checked == true)
{
if (reader["Name"].ToString() == txtName.Text && reader["Password"].ToString() == txtPassword.Text)
{
功能选择 sf = new 功能选择((主窗体)this.MdiParent);
sf.Show();
this.Tag = "true";
}
}
else
{
if (reader["ID"].ToString() == txtName.Text && reader["Password"].ToString() == txtPassword.Text)
{
功能选择 sf = new 功能选择((主窗体)this.MdiParent);
sf.Show();
this.Tag = "true";
}
}

}
贫僧又回来了 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Guyschaos 的回复:]
select count(*) from Account where UserName=输入的 and PassWord=输入的

最后用SqlCommand的ExecuteScalar()方法来判断,如果大于1就登录!
[/Quote]
说错了,应该是等于1
贫僧又回来了 2009-03-09
  • 打赏
  • 举报
回复
select count(*) from Account where UserName=输入的 and PassWord=输入的

最后用SqlCommand的ExecuteScalar()方法来判断,如果大于1就登录!
哈哈镜0000 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zfh198601 的回复:]
select count(*) from Account where UserName=输入的账号 and PassWord=密码

再判断count是否大于0
[/Quote]


同意!

但要防止SQL注入问题呀
PandaIT 2009-03-09
  • 打赏
  • 举报
回复

一个sql语句查询表里的数据

然后

int count=(int)command.ExecuteScalar();//执行语句
//如果找到一个,就通过
if(count==1){
MessageBox.Show("登录成功");
}
zfh198601 2009-03-09
  • 打赏
  • 举报
回复
select count(*) from Account where UserName=输入的账号 and PassWord=密码

再判断count是否大于0

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧