我将验证函数(UserValiData)放在一个公共类(PubCalss)中.然后在主窗口中调用。公共类及验证函数如下:using System;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;namespace PackList
{
  /// <summary>
  /// PubClass 的摘要说明。
  /// </summary>
  ///
  public class PubClass
  {
      protected static string strconn;
      public SqlConnection sqlconn=null;
      
      public PubClass()
      {
       //
       // TODO: 在此处添加构造函数逻辑
       //
       //strconn="Initial Catalog=nhsoft;data source=localhost;Security=SSPI";
sqlconn = null;
       }       ~PubClass()
       {
       }       //数据库是否打开
public void SetConnectionString(string sqlstring)
{
strconn=sqlstring;
sqlconn=null;
sqlconn= new SqlConnection(strconn);
} public void openconn()
{
if(sqlconn.State==ConnectionState.Open)
{
sqlconn.Close();
}
sqlconn.Open();
} //验证用户的函数
public bool UserValidate(string UserName,string password)
{
string sqluser="select count(*) from User where name='"+UserName+"' and pwd='"+password+"'"; SetConnectionString(strconn);
openconn();
SqlCommand cmd=new SqlCommand(sqluser,sqlconn);
SqlDataReader dr;
dr=cmd.ExecuteReader();                                 //报错处。
cmd.Dispose(); //消毁。
if(dr.Read())
{
return false;
}
else
{
return true;
} }

}}窗体调用过程如下:
private void btnOK_Click(object sender, System.EventArgs e)
{
   PubClass pc=new PubClass();
   if(pc.UserValidate(this.tbUser.Text,this.tbPassWord.Text))
   {
this.DialogResult=DialogResult.OK;
   }
   else
   {
this.DialogResult=DialogResult.None;
   }}
老是执行到dr=cmd.ExecuteReader();这一处时报系统错误。
我用object o=cmd.ExecuteScale();也报同样的错误。已搞烦了我两天了,希望高人指点。(vs.2003)

解决方案 »

  1.   

    没有人会吗?折腾了两天了,是不是我不能在公共类中用dr=cmd.executeReader()等过程啊?
      

  2.   

    第一步你先把你的string sqluser="select count(*) from User where name='"+UserName+"' and pwd='"+password+"'";监测,看看执行结果是什么?然后放在查询分析器里执行看看有没有错误‘
      

  3.   

    string sqluser="select count(*) from User where name='"+UserName+"' and pwd='"+password+"'";而且这句话最好写成
    string sqluser="select count(*) from User where name='"+UserName.Repalce("'","''")+"' and pwd='"+password.Replace("'","''")+"'";
      

  4.   

    试过了,老问题。在dr=cmd.ExecuteReader();处报错说是系统错误。
    没有通过啊。
      

  5.   

    我在查询分析器里执行监测的sql语句可以执行吗?
      

  6.   

    我将sql语句写成:string sqluser="select count(*) from User
    然后换成cmd.ExecuteScal();还是报同样的错误。
      

  7.   

    string sqluser="select count(*) from User where name='"+UserName+"' and pwd='"+password+"'";你这句监测看看,把结果拷到数据库查询分析器里执行看看看看直接在数据库里是否能够执行通过,然后再考虑你的其他问题
      

  8.   

    嗯。没有问题啊。我的user表里就只有一条记录
    id    name    chnname  pwd
    1     lhc     落花     lhc啊。
      

  9.   

    我的老天。user不能做表名。我真是蠢到家了。
    非常感谢!
      

  10.   

    sqlconn这个数据库连接变量你在那里赋值啦?