我用.net2003编写了一个封装的不是很好的数据库联接类
namespace OA
{
/// <summary>
/// config 的摘要说明。
/// </summary>
public class config: System.Web.UI.Page
{
public SqlConnection myConnection; 
public SqlCommand myCommand;
public SqlDataAdapter myAdapter;
public SqlDataReader myReader;
public SqlCommandBuilder myCommandBuilder; public DataSet ds;
public DataTable dt;
public DataRow dr; /// <summary>
/// 连接数据库
/// </summary>
public void Open()
{   
myConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
//myConnection=new System.Data.OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
myConnection.Open();
} /// <summary>
/// 关闭数据库和清除DateSet对象
/// </summary>
public void Close()
{
if (ds!=null) // 清除DataSet对象
{
ds.Clear();
}
if (myConnection!=null)
{
myConnection.Close(); // 关闭数据库
}
} /// <summary>
/// 执行Transact-SQL语句,对数据库记录做插入,修改,删除等操作
/// </summary>
/// <param name="str_Sql">Transact-SQL语句</param>
public void ExeSql(string str_Sql)
{   
Open();
myCommand = new SqlCommand(str_Sql,myConnection);
myCommand.ExecuteNonQuery();
myCommand.Dispose();
}
...................................
}//config end
}//namespace OA end在页面事例化引用
protected config conn_dept=new config();
protected config conn2=new config();
protected string searchstring;conn_dept控制的for循环里面
conn2.ExeSql(searchstring);
searchstring="insert into Dept_Earn (年,月,dept_NBYH_ID,产品收入,劳务收入,车间收入,产品工时,劳务工时,总工时,变费定额,考核定额,复合值,复合单位,工时值) values ("
+.....这里绝对没有再调用conn2页面报错,插入重复键。我用断点发现停在conn2.ExeSql(searchstring);这一句程序语句的时候
insert into Dept_Earn (年,月,dept_NBYH_ID,...这一句SQL语句已经执行过了。
这时候根本还没有在类里执行myCommand = new SqlCommand(str_Sql,myConnection);考虑可能是断点影响,我取消了程序里所有断点,有的时候出错,有时候正确,折磨人。。