日期:2014-05-19  浏览次数:21132 次

未将对象引用设置到对象的实例 在别人机子上运行正常
form1
public   static   SqlConnection   getConnection()
{
try
{
{
string   strConnection= "user   id=sa;password=123; ";
strConnection+= "initial   catalog=master;Server=(local); ";

conn=new   SqlConnection(strConnection);
MessageBox.Show( "准备打开数据库 ");
conn.Open();
MessageBox.Show( "数据库打开 ");
}
return   conn;
}
catch(Exception   ex)
{
MessageBox.Show(ex.ToString());
return   null;
}
}
Form2   f2;
private   void   button1_Click(object   sender,   System.EventArgs   e)
{

if(f2==null||f2.IsDisposed)
{
f2=new   Form2();
f2.MdiParent=this;
f2.Show();
}

}


form2
private   void   Form2_Load(object   sender,   System.EventArgs   e)
{
cn=new   SqlConnection();
cn=Form1.getConnection();

}

------解决方案--------------------
看看你的机器上的数据库设置对嘛》

string strConnection= "user id=sa;password=123; ";
strConnection+= "initial catalog=master;Server=(local); ";

数据库连接问题
------解决方案--------------------
那就是你的数据库连接有问题。。。
比如把(local)换成你机器名看看,等等
------解决方案--------------------
查查本机数据库状态


报的错误是什么?
------解决方案--------------------
肯定是连接错误!!