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();

}

解决方案 »

  1.   

    一直到显示"准备打开数据库"正常
    在conn.Open()时弹出错误郁闷的是在别人机子上运行正常
      

  2.   

    看看你的机器上的数据库设置对嘛》string strConnection="user id=sa;password=123;";
    strConnection+="initial catalog=master;Server=(local);";数据库连接问题
      

  3.   

    那就是你的数据库连接有问题
    比如把(local)换成你机器名看看,等等
      

  4.   

    连接没有问题  
    打开时出错   用SqlConnection控件可建立连接  但是无法打开   在Open()时出错
      

  5.   

    恩   我错了的   
    连接错误   
    不好意思   
    string strConnection="workstation id=CSU-4583460658D;packet size=4096;integrated security=SSPI;data source=(local);persist security info=False;initial catalog=master";
    抄名字的时候抄错了
    后来整个拷贝过去可以的