我的数据链接代码是在一个CLass里面写的
private string strConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\CommFuntion/DebetContrl.mdb", Application.StartupPath);
private OleDbDataAdapter myAdapter;
private DataSet myds;
private DataTable mytb;
private DataSet dss = new DataSet();
OleDbConnection _SConn;
public OleDbConnection SConn
{
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
}
#region 打开关闭连接 Open(),Close()
/// <summary>
/// 打开数据库连接
/// </summary>
public void Open()
{
if (_SConn.State == ConnectionState.Closed)
_SConn.Open();
} /// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (_SConn.State == ConnectionState.Open)
{
_SConn.Close();
//_SConn.Dispose();
}
}
#endregion
而在这里引用
Func.SConn.Open();
Func.Open();
OleDbCommand cmd = new OleDbCommand("insert into Tb_Customerinfo(CID,CName,Csex,CStyle,CCAddress,"+
"CPhone,CIDCard,CPost,CNetAddress,CEmail,BasicBankName,BasicBankNum,BankID,CAccountNum,"+
"CCredit,CompanyName,CompanyChart,LicenseNum,Legal,LegalAddress,LegalPhone,LegalNum,Business,"+
"RegMoney,Caddress,CoutrlerID)) values('" + textBox1.Text + "',"+
"" + textBox2.Text + "," + Convert.ToString(Convert.ToInt32(comboBox3.SelectedIndex)) + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox1.SelectedIndex)) + "," + textBox4.Text + ","+
"" + textBox6.Text + "," + textBox7.Text + "," + textBox8.Text + "," + textBox9.Text + ","+
"" + textBox15.Text + "," + textBox10.Text + "," + textBox14.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox2.SelectedIndex) + 1) + ","+
"" + textBox11.Text + "," + textBox13.Text + "," + textBox27.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox5.SelectedIndex) + 1) + ","+
"" + textBox24.Text + "," + textBox23.Text + "," + textBox22.Text + ","+
"" + textBox20.Text + "," + textBox21.Text + "," + textBox3.Text + ","+
"" + textBox17.Text + "," + textBox18.Text + "," + Convert.ToString(Convert.ToInt32(comboBox4.SelectedIndex) + 1)+ ")", Func.SConn);
cmd.ExecuteNonQuery();
Func.Close();
他老是提示 数据链接未打开,为什么啊,难道是我用的链接方式错了嘛?
private string strConnection = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\CommFuntion/DebetContrl.mdb", Application.StartupPath);
private OleDbDataAdapter myAdapter;
private DataSet myds;
private DataTable mytb;
private DataSet dss = new DataSet();
OleDbConnection _SConn;
public OleDbConnection SConn
{
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
}
#region 打开关闭连接 Open(),Close()
/// <summary>
/// 打开数据库连接
/// </summary>
public void Open()
{
if (_SConn.State == ConnectionState.Closed)
_SConn.Open();
} /// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (_SConn.State == ConnectionState.Open)
{
_SConn.Close();
//_SConn.Dispose();
}
}
#endregion
而在这里引用
Func.SConn.Open();
Func.Open();
OleDbCommand cmd = new OleDbCommand("insert into Tb_Customerinfo(CID,CName,Csex,CStyle,CCAddress,"+
"CPhone,CIDCard,CPost,CNetAddress,CEmail,BasicBankName,BasicBankNum,BankID,CAccountNum,"+
"CCredit,CompanyName,CompanyChart,LicenseNum,Legal,LegalAddress,LegalPhone,LegalNum,Business,"+
"RegMoney,Caddress,CoutrlerID)) values('" + textBox1.Text + "',"+
"" + textBox2.Text + "," + Convert.ToString(Convert.ToInt32(comboBox3.SelectedIndex)) + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox1.SelectedIndex)) + "," + textBox4.Text + ","+
"" + textBox6.Text + "," + textBox7.Text + "," + textBox8.Text + "," + textBox9.Text + ","+
"" + textBox15.Text + "," + textBox10.Text + "," + textBox14.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox2.SelectedIndex) + 1) + ","+
"" + textBox11.Text + "," + textBox13.Text + "," + textBox27.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox5.SelectedIndex) + 1) + ","+
"" + textBox24.Text + "," + textBox23.Text + "," + textBox22.Text + ","+
"" + textBox20.Text + "," + textBox21.Text + "," + textBox3.Text + ","+
"" + textBox17.Text + "," + textBox18.Text + "," + Convert.ToString(Convert.ToInt32(comboBox4.SelectedIndex) + 1)+ ")", Func.SConn);
cmd.ExecuteNonQuery();
Func.Close();
他老是提示 数据链接未打开,为什么啊,难道是我用的链接方式错了嘛?
在cmd 调用时, "" + textBox17.Text + "," + textBox18.Text + "," + Convert.ToString(Convert.ToInt32(comboBox4.SelectedIndex) + 1)+ ")", Func.SConn);并没有打开数据连接!
Func.SConn
=>
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
} 那不等于又返回一个新的连接,还是没打开的
OleDbCommand cmd = new OleDbCommand("insert into Tb_Customerinfo(CID,CName,Csex,CStyle,CCAddress,"+
"CPhone,CIDCard,CPost,CNetAddress,CEmail,BasicBankName,BasicBankNum,BankID,CAccountNum,"+
"CCredit,CompanyName,CompanyChart,LicenseNum,Legal,LegalAddress,LegalPhone,LegalNum,Business,"+
"RegMoney,Caddress,CoutrlerID)) values('" + textBox1.Text + "',"+
"" + textBox2.Text + "," + Convert.ToString(Convert.ToInt32(comboBox3.SelectedIndex)) + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox1.SelectedIndex)) + "," + textBox4.Text + ","+
"" + textBox6.Text + "," + textBox7.Text + "," + textBox8.Text + "," + textBox9.Text + ","+
"" + textBox15.Text + "," + textBox10.Text + "," + textBox14.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox2.SelectedIndex) + 1) + ","+
"" + textBox11.Text + "," + textBox13.Text + "," + textBox27.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox5.SelectedIndex) + 1) + ","+
"" + textBox24.Text + "," + textBox23.Text + "," + textBox22.Text + ","+
"" + textBox20.Text + "," + textBox21.Text + "," + textBox3.Text + ","+
"" + textBox17.Text + "," + textBox18.Text + "," + Convert.ToString(Convert.ToInt32(comboBox4.SelectedIndex) + 1)+ ")", Func.SConn); Func.SConn.Open();
cmd.ExecuteNonQuery(); Func.Close(); 看看。
public OleDbConnection SConn
{
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
}
你这个属性定义的也太狠了,访问一次属性,你就new一个连接……
public OleDbConnection SConn
{
get {
if(_SConn == null)
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
}
{
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
}
这里错了,这表示每一次使用Func.SConn都要实例化一次_SConn,连接当然就没有被Open了。
应把红色部分去掉。
要把这一部分写在类的构造方法中。。
http://topic.csdn.net/u/20081213/14/4c273fe5-49af-4b85-b159-6eff580ccc63.html
里面的数据连接类的代码只需要注释掉_SConn.Dispose(); 就可以了,怎么又把SConn这个属性给改了呢?
public OleDbConnection SConn
{
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
} 这个是问题的根本,Func.SConn.Open(); // 这里创建了链接 A
Func.Open(); // 打开链接 A
OleDbCommand cmd = new OleDbCommand("insert into Tb_Customerinfo(CID,CName,Csex,CStyle,CCAddress,"+
"CPhone,CIDCard,CPost,CNetAddress,CEmail,BasicBankName,BasicBankNum,BankID,CAccountNum,"+
"CCredit,CompanyName,CompanyChart,LicenseNum,Legal,LegalAddress,LegalPhone,LegalNum,Business,"+
"RegMoney,Caddress,CoutrlerID)) values('" + textBox1.Text + "',"+
"" + textBox2.Text + "," + Convert.ToString(Convert.ToInt32(comboBox3.SelectedIndex)) + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox1.SelectedIndex)) + "," + textBox4.Text + ","+
"" + textBox6.Text + "," + textBox7.Text + "," + textBox8.Text + "," + textBox9.Text + ","+
"" + textBox15.Text + "," + textBox10.Text + "," + textBox14.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox2.SelectedIndex) + 1) + ","+
"" + textBox11.Text + "," + textBox13.Text + "," + textBox27.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox5.SelectedIndex) + 1) + ","+
"" + textBox24.Text + "," + textBox23.Text + "," + textBox22.Text + ","+
"" + textBox20.Text + "," + textBox21.Text + "," + textBox3.Text + ","+
"" + textBox17.Text + "," + textBox18.Text + "," + Convert.ToString(Convert.ToInt32(comboBox4.SelectedIndex) + 1)+ ")", Func.SConn); // 访问 SConn 又创建了一个新的对象 B,不同于A,而且默认是关闭的,
cmd.ExecuteNonQuery();
Func.Close();
OleDbConnection _SConn;
而你get的的时候
public OleDbConnection SConn
{
get {
_SConn = new OleDbConnection(strConnection);
return _SConn;
}
set {
_SConn = value; }
}
这个 只是 返回一个新的实例对象
你的
而在这里引用
Func.SConn.Open();
Func.Open();
OleDbCommand cmd = new OleDbCommand("insert into Tb_Customerinfo(CID,CName,Csex,CStyle,CCAddress,"+
"CPhone,CIDCard,CPost,CNetAddress,CEmail,BasicBankName,BasicBankNum,BankID,CAccountNum,"+
"CCredit,CompanyName,CompanyChart,LicenseNum,Legal,LegalAddress,LegalPhone,LegalNum,Business,"+
"RegMoney,Caddress,CoutrlerID)) values('" + textBox1.Text + "',"+
"" + textBox2.Text + "," + Convert.ToString(Convert.ToInt32(comboBox3.SelectedIndex)) + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox1.SelectedIndex)) + "," + textBox4.Text + ","+
"" + textBox6.Text + "," + textBox7.Text + "," + textBox8.Text + "," + textBox9.Text + ","+
"" + textBox15.Text + "," + textBox10.Text + "," + textBox14.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox2.SelectedIndex) + 1) + ","+
"" + textBox11.Text + "," + textBox13.Text + "," + textBox27.Text + ","+
"" + Convert.ToString(Convert.ToInt32(comboBox5.SelectedIndex) + 1) + ","+
"" + textBox24.Text + "," + textBox23.Text + "," + textBox22.Text + ","+
"" + textBox20.Text + "," + textBox21.Text + "," + textBox3.Text + ","+
"" + textBox17.Text + "," + textBox18.Text + "," + Convert.ToString(Convert.ToInt32(comboBox4.SelectedIndex) + 1)+ ")", Func.SConn);
cmd.ExecuteNonQuery();
Func.Close();
这个代码
可以修改下
OleDbConnection MyConn;
MyConn=Func.SConnMyConn.Open();
然后 用MyConn 代替你里面的那些 Func的操作,应该就可以了