小弟刚接触C#,现在需要做一登陆界面,用的ACCESS数据库,请问怎么制作登陆页面啊,
我自己的方法是SQL语言查询ID ,然后判断password是否相等
代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
if((TextBox1.Text!="")&&(TextBox2.Text!=""))
{
string connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(@".\db\db1.mdb")+";Mode=Share Deny None;Persist Security Info=False";OleDbConnection conn=new OleDbConnection(connstr);
conn.Open();
string sql="SELECT id FROM id WHERE id='"+TextBox1.Text+"";
OleDbCommand cmd=new OleDbCommand(sql,conn);
OleDbDataReader dr=cmd.ExecuteReader();
if(dr.Read()==true)
{
if(dr.GetValue(0).ToString()==TextBox2.Text)
{MsgBox("OK");
}
else
{MsgBox("Wrong");
}
}
dr.Close();
} }
}//MsgBox类略
但是一直有问题 不能解决 请高人指点啊
PS:登陆用的库名为id 属性有id和password
我自己的方法是SQL语言查询ID ,然后判断password是否相等
代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
if((TextBox1.Text!="")&&(TextBox2.Text!=""))
{
string connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(@".\db\db1.mdb")+";Mode=Share Deny None;Persist Security Info=False";OleDbConnection conn=new OleDbConnection(connstr);
conn.Open();
string sql="SELECT id FROM id WHERE id='"+TextBox1.Text+"";
OleDbCommand cmd=new OleDbCommand(sql,conn);
OleDbDataReader dr=cmd.ExecuteReader();
if(dr.Read()==true)
{
if(dr.GetValue(0).ToString()==TextBox2.Text)
{MsgBox("OK");
}
else
{MsgBox("Wrong");
}
}
dr.Close();
} }
}//MsgBox类略
但是一直有问题 不能解决 请高人指点啊
PS:登陆用的库名为id 属性有id和password
解决方案 »
- 怎样为PropertyGrid排序(属性类别排序,属性排序)
- C#控制台程序,在做do while 循环的过程中,内存慢慢增加,直到do while结束,内存才释放,这个正常吗
- 如何访问主form的控件对象
- 图片处理,如何只保留图片的一种颜色。要求处理速度
- 再次请教关于能把ToolTip固定在某个座标的问题!
- 关于ListView控件中IndexFromPoint方法
- 系统文件监视
- c# 怎么从一个里表中一次读取一行字符串啊
- Directx9.0中的NetworkPacket如果像客户端传一个对象?
- 多窗口技术中, 每个窗口里动态变化的窗口内容是如何实现的?
- 如何得到select 1的返回结果
- 求一算法:用XmlTextReader只向前读的方法如何构建一个棵树,添加到Tree上面??
string sql="SELECT Count(*) FROM 你的用户表 WHERE id='"+TextBox1.Text+"'And pwd = '" + TextBox2.Text+"'";
OleDbCommand cmd=new OleDbCommand(sql,conn);
OleDbDataReader dr=cmd.ExecuteReader();
if(dr.Read()==true)
{
if(dr.GetInt32(0) > 0)
{MsgBox("OK");}
else
{MsgBox("Wrong");}
}
else
{MsgBox("None!");}dr.Close();
string str="select count(*) from id where id='"
+this.TextBox1+"',password='"+this.TextBox2+"'";
OleDbCommand cmd=new OleDbCommand(str,con);
int a=cmd.ExcuteScalar();
然后直接判断a的值,cmd.ExcuteScalar()是返回第一行第一列的值
为0就是没有这个用户,有的话就为1,最简单了
做登录最重要的是要解决你程序的安全性问题所以照你这样写SQL语句应该存在缺陷的。
如果你要把客户端输入的数据与SQL语句连接起来直接执行是很不安全的因为高手们可以很容易的
进入你的系统,譬如你应该避免他人利用你SQL语句字符串连接的特点使SQL语句最终变成select count(*) from table where username = '' and pwd = '' or 1=1 当然or1=1是在PWD中输入的,这还不可怕要是输入";delete table"那就更可怕了,所以我劝各位一定小心你的SQL语句。
int count = 0
while(dr.Read())
{
count++; // 你要考虑如果有多个用户使用相同用户名和密码的情况.
}
dr.Close();if (count >= 1)
{
...
}