登入框连接数据库问题.望高手解决下.

lpqss1 2009-03-26 07:26:15
本人用的是SqlClient方式连接sql 2000
程序能正确运行.但是不论密码与账户输入
对或错.都执行请输入正确的用户名和密码.
不知道为什么.已经弄了很久了,
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 sqlconnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (namatext.Text.Trim() == "" || pswtext.Text.Trim() == "")
MessageBox.Show("请输入用户名和密码", "提示");
else
{
string Afile = "server=LAIPAN-B0559C3A;uid=sa;pwd=123;database=user";
SqlConnection AconnStr = new SqlConnection(Afile);
AconnStr.Open();

string sql="select USER from UU where USER='"+ namatext.Text.Trim()+ "'and PSW='"+ pswtext.Text.Trim() +"'";
SqlCommand cmd=new SqlCommand(sql,AconnStr);

SqlDataReader myReader = null;
try
{
myReader = cmd.ExecuteReader();
}
catch (Exception ex)
{ if(ex!=null) Console.Write("执行错误");}
if(myReader.Read())
{
this.Visible=false;
Form2 form2=new Form2();
form2.Show();
}

else
{
MessageBox.Show("用户名或者密码错误", "警告");
namatext.Text = "";
pswtext.Text="";
namatext.Focus();
}
}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}


}
}
...全文
176 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
lpqss1 2009-03-29
  • 打赏
  • 举报
回复
谢谢你们的帮助,虽然问题没解决,但肯定是我自己的问题.可能是数据库建表的问题吧
PandaIT 2009-03-29
  • 打赏
  • 举报
回复
 string sql="select count(*) from UU where USER='"+ namatext.Text.Trim()+ "'and PSW='"+ pswtext.Text.Trim() +"'"; 

//用占位符来

string sql=string.Format("select count(*) from UU where USER='{0}' and PSW='{1}'",namatext.Text.Trim(),pswtext.Text.Trim());



h2325124 2009-03-29
  • 打赏
  • 举报
回复
有可能是你的SQL语句有问题,user在SQL中是关键字,你用中括号括起来试试
string sql="select [USER] from UU where USER='"+ namatext.Text.Trim()+ "'and PSW='"+ pswtext.Text.Trim() +"'";
马少华 2009-03-29
  • 打赏
  • 举报
回复
namatext,pwstext肯定是这两个控件关联的是别的控件。而不是你要检测的控件。
IamHandsomeman 2009-03-29
  • 打赏
  • 举报
回复
楼主,好像数据库的数据表不给有这个名字的吧

user

你换一个数据表名字看看!
lpqss1 2009-03-29
  • 打赏
  • 举报
回复
up
yjvjom 2009-03-28
  • 打赏
  • 举报
回复

try
{
myReader = cmd.ExecuteReader(); //你要在此加入一个判断,如下
if (!reader.Read())
MessageBox.Show("用户名或者密码错误", "警告");
}
catch (Exception ex)
{
if(ex!=null) Console.Write("执行错误");}
if(myReader.Read()) //这个判断有问题,因为如果报错则myReader肯定是==null
{
this.Visible=false;
Form2 form2=new Form2();
form2.Show();
}
else
{
MessageBox.Show("用户名或者密码错误", "警告");
namatext.Text = "";
pswtext.Text="";
namatext.Focus();
}
}

但总的来看应该是你的数据联接字符串有问题(登录到SQL的密码错误或其他)
  • 打赏
  • 举报
回复
数据库有问题吧?有这条记录没?
lpqss1 2009-03-27
  • 打赏
  • 举报
回复
up
lpqss1 2009-03-27
  • 打赏
  • 举报
回复
还是一样,肯定就sql这个地方错的,但是怎么也找不出.
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 sqlconnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (namatext.Text.Trim() == "" || pswtext.Text.Trim() == "")
MessageBox.Show("请输入用户名和密码", "提示");
else
{
string Afile = "server=LAIPAN-B0559C3A;uid=sa;pwd=123;database=user";
SqlConnection AconnStr = new SqlConnection(Afile);
AconnStr.Open();

string sql = "select * from UU where USER='" + namatext.Text.Trim() + "'and PSW='" + pswtext.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(sql, AconnStr);

cmd.CommandText = sql;

if (null!=cmd.ExecuteReader())//如果这里的if语句里这样写的话,密码错误也能连上数据库.但如果这样写if(null!=ExecuteDScalar())不管密码对错都连 不上数据库
{
this.Visible = false;
Form2 form2 = new Form2();
form2.Show();
}

else
{
MessageBox.Show("用户名或者密码错误", "警告");
namatext.Text = "";
pswtext.Text = "";
namatext.Focus();
}
}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}


}
}
lpqss1 2009-03-27
  • 打赏
  • 举报
回复
算了,看来是没办法了.
caorenlong 2009-03-27
  • 打赏
  • 举报
回复
myReader = cmd.ExecuteReader();

你跟踪到这句,
如果用户密码正确,应该是1
错误,是 -1
lpqss1 2009-03-27
  • 打赏
  • 举报
回复
大家顶起来,太奇怪了,搞不懂
lpqss1 2009-03-27
  • 打赏
  • 举报
回复
数据库我看了很多次了,没问题啊,我也感觉奇怪了.连access都没问题的,我晕了
聖少俊 2009-03-26
  • 打赏
  • 举报
回复
先F9设置断点,F5运行,然后按F10一步一步来测试,就能发现问题了
PandaIT 2009-03-26
  • 打赏
  • 举报
回复
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 sqlconnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
if (namatext.Text.Trim() == "" || pswtext.Text.Trim() == "")
MessageBox.Show("请输入用户名和密码", "提示");
else
{
string Afile = "server=LAIPAN-B0559C3A;uid=sa;pwd=123;database=user";
SqlConnection AconnStr = new SqlConnection(Afile);
AconnStr.Open();
//你sql这里就有问题
string sql="select count(*) from UU where USER='"+ namatext.Text.Trim()+ "'and PSW='"+ pswtext.Text.Trim() +"'";
SqlCommand cmd=new SqlCommand(sql,AconnStr);

int count=0;
count=(int)cmd.ExecuteScalar();//执行查询语句

if(count==1){
MessageBox.Show("成功!")
this.Visible=false;
Form2 form2=new Form2();
form2.Show();
}
else
{
MessageBox.Show("用户名或者密码错误", "警告");
namatext.Text = "";
pswtext.Text="";
namatext.Focus();
}
catch (Exception ex)
{
MessageBox.Show("出错!")
}
}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
lpqss1 2009-03-26
  • 打赏
  • 举报
回复
因为if语句后面只有一句话,不需要{}
TkingCN 2009-03-26
  • 打赏
  • 举报
回复
if (namatext.Text.Trim() == "" || pswtext.Text.Trim() == "")★
MessageBox.Show("请输入用户名和密码", "提示");
■else
{
string Afile = "server=LAIPAN-B0559C3A;uid=sa;pwd=123;database=user";
SqlConnection AconnStr = new SqlConnection(Afile);
AconnStr.Open();
(★位置)IF后面为什么没有“{”
(■位置)else前面为什么没有"}"
TkingCN 2009-03-26
  • 打赏
  • 举报
回复
仔细看勒下你的代码 貌似格式有问题 你的 private void button1_Click(object sender, EventArgs e)
{
if (namatext.Text.Trim() == "" || pswtext.Text.Trim() == "")
MessageBox.Show("请输入用户名和密码", "提示"); 这个后面没有“{”


你的else前面为什么没有“}”
TkingCN 2009-03-26
  • 打赏
  • 举报
回复
把调试点设在private void button1_Click(object sender, EventArgs e) 旁边
debug单步调试 → F11 观察你的pswtext.Text和namatext.Text的值
加载更多回复(9)

111,126

社区成员

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

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

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