日期:2014-05-18  浏览次数:20786 次

sql连接时报错(急)
在sqlcn.open()的时候报: "未将对象引用设置到对象实例 ",看了半天都不知道怎么回事,请高手指点,谢谢
代码如下:
using   System;
using   System.Data;
using   System.Data.SqlClient;
using   System.Windows.Forms;

namespace   ddgl
{
///   <summary>
///   usercheck   的摘要说明。
///   </summary>
public   class   usercheck
{
public   static   SqlConnection   sqlcn;
public   static   string   connStr   =   "Initial   Catalog=ddgldata;Data   Source=(local);Integrated   Security=SSPI;User   ID= 'sa ';password= ' ' ";


public   usercheck()
{

}

public   string   UserSortCheck(string   username,string   userpassword)
{
sqlcn   =   new   SqlConnection(connStr);
string   txtSql;
string   usersort= "nobody ";
//创建数据库连接      
txtSql   =   "SELECT   *   FROM   ddgluser ";
SqlCommand   checkuser=new   SqlCommand(txtSql,sqlcn);
//创建SqlCommad
try
{
sqlcn.Open();
SqlDataReader   sqlreader=checkuser.ExecuteReader();
while(sqlreader.Read())
{
if((sqlreader[0].ToString().Trim()==username)&&(sqlreader[1].ToString().Trim()==userpassword))
{
usersort=sqlreader[2].ToString().Trim();//返回用户类别
break;
}
}
}
catch(Exception   e)
{
MessageBox.Show(e.ToString());
}
sqlcn.Close();
return   usersort;

}
}
}


------解决方案--------------------
password= ' '; 是不是应该是password=;
------解决方案--------------------
public static string connStr = "Initial Catalog=ddgldata;Data Source=(local);Integrated Security=SSPI;User ID= 'sa ';password= ' ' ";
============
public static string connStr = "Initial Catalog=ddgldata;Data Source=(local);Integrated Security=SSPI;User ID=sa;password= ";

------解决方案--------------------
public static string connStr = "Initial Catalog=ddgldata;Data Source=(local);Integrated Security=SSPI;User ID= 'sa ';password= ' ' ";
===========================
晕,你都写了Integrated Security=SSPI了,还要User ID= 'sa ';password= ' '做什么?
用这个:public static string connStr = "Initial Catalog=ddgldata;Data Source=(local);Integrated Security=SSPI ";
------解决方案--------------------
public static string connStr = "Initial Catalog=ddgldata;Data Source=(local);Integrated Security=SSPI;User ID= 'sa ';password= ' ' ";
User ID password不用 单引号