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();
}
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();
}
在conn.Open()时弹出错误郁闷的是在别人机子上运行正常
strConnection+="initial catalog=master;Server=(local);";数据库连接问题
比如把(local)换成你机器名看看,等等
打开时出错 用SqlConnection控件可建立连接 但是无法打开 在Open()时出错
连接错误
不好意思
string strConnection="workstation id=CSU-4583460658D;packet size=4096;integrated security=SSPI;data source=(local);persist security info=False;initial catalog=master";
抄名字的时候抄错了
后来整个拷贝过去可以的