try
{

//OleDbConnection conConnection = new OleDbConnection ( connString ) ;
SqlConnection conn = new SqlConnection(MainForm.connString);

//计算查询总数 "select COUNT(*) from 输出总表";
string strInsert99 = "select COUNT(*) from 基础数据";;

//strInsert99=strCommand;
conn.Open ( ) ;
// OleDbCommand  mycmd66=new OleDbCommand (strInsert99,conConnection);
SqlCommand sqlcmd = new SqlCommand(strInsert99, conn);   

object countResult;
countResult=sqlcmd.ExecuteScalar();
conn.Close();
counts=Convert.ToDouble(countResult);

strCommand = "select * from 基础数据";

//OleDbConnection conConnection = new OleDbConnection(connString);


SqlDataReader reader;
conn.Open(); // 打开数据连接

//OleDbCommand cmd = new OleDbCommand(strCommand, conConnection);

sqlcmd = new SqlCommand(strCommand, conn);

reader = sqlcmd.ExecuteReader();// //获得数据集

while (reader.Read())
{
temp_01=reader["乡镇"].ToString().Replace("龙南","");
temp_01=temp_01.Replace("民安","");
temp_01=temp_01.Replace("四九","");

tempstr=temp_01+reader["居委会"].ToString();
czdm=Get_Czdm(tempstr);
// czdm="hky";

ids=Convert.ToInt32(reader["ID"]);
//Update_Czdm(Get_Czdm(tempstr),ids); Get_Czdm(tempstr)
//  Log.Text +=tempstr+" "+czdm+"  "+ids+"\n";
strCommand2 = "update 基础数据 set 村镇代码='"+czdm+"' where ID="+ids;
//OleDbCommand cmd2 = new OleDbCommand(strCommand2, conConnection);
SqlCommand sqlcmd2 = new SqlCommand(strCommand2, conn);  

sqlcmd2.ExecuteReader();
sqlcmd2.Dispose();
czdm="";
tempstr="";
progressBar1.BeginInvoke(new System.EventHandler(SetText2),i+1);

i++; } reader.Close();//关闭数据集 conn.Close(); // 关闭数据连接
MessageBox.Show("生成数据完毕");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

解决方案 »

  1.   

    出错在:
    sqlcmd2.Dispose();
      

  2.   

    提示已经很清楚了,先关掉打开的reader,然后在释放
      

  3.   

    try
    {//OleDbConnection conConnection = new OleDbConnection ( connString ) ;
    SqlConnection conn = new SqlConnection(MainForm.connString);//计算查询总数 "select COUNT(*) from 输出总表";
    string strInsert99 = "select COUNT(*) from 基础数据";;//strInsert99=strCommand;
    conn.Open ( ) ;
    // OleDbCommand mycmd66=new OleDbCommand (strInsert99,conConnection);
    SqlCommand sqlcmd = new SqlCommand(strInsert99, conn);   object countResult;
    countResult=sqlcmd.ExecuteScalar();
    conn.Close();
    counts=Convert.ToDouble(countResult);strCommand = "select * from 基础数据";//OleDbConnection conConnection = new OleDbConnection(connString);
    SqlDataReader reader;
    conn.Open(); // 打开数据连接//OleDbCommand cmd = new OleDbCommand(strCommand, conConnection);sqlcmd = new SqlCommand(strCommand, conn);reader = sqlcmd.ExecuteReader();// //获得数据集while (reader.Read())
    {
    temp_01=reader["乡镇"].ToString().Replace("龙南","");
    temp_01=temp_01.Replace("民安","");
    temp_01=temp_01.Replace("四九","");tempstr=temp_01+reader["居委会"].ToString();
    czdm=Get_Czdm(tempstr);
    // czdm="hky";ids=Convert.ToInt32(reader["ID"]);
    //Update_Czdm(Get_Czdm(tempstr),ids); Get_Czdm(tempstr)
    // Log.Text +=tempstr+" "+czdm+" "+ids+"\n";
    strCommand2 = "update 基础数据 set 村镇代码='"+czdm+"' where ID="+ids;
    //OleDbCommand cmd2 = new OleDbCommand(strCommand2, conConnection);
    SqlCommand sqlcmd2 = new SqlCommand(strCommand2, conn);   sqlcmd2.ExecuteReader();
    sqlcmd2.Dispose();
      

  4.   

    出错在:
    sqlcmd2.ExecuteReader();循环时,关键要执行:
    strCommand2 = "update 基础数据 set 村镇代码='"+czdm+"' where ID="+ids;
    //OleDbCommand cmd2 = new OleDbCommand(strCommand2, conConnection);
    SqlCommand sqlcmd2 = new SqlCommand(strCommand2, conn);  
      

  5.   

    reader还没关闭...ps:别把.NET当asp写...
      

  6.   

    access 数据库却可以执行的
      

  7.   

    while (reader.Read())
    {//...
    sqlcmd2.ExecuteReader();//换成sqlcmd2.ExecuteNonQuery();试试
    }
      

  8.   

    private void openConnection()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.ConnectionString = connString;
                comm.Connection = conn;
                try
                {
                    conn.Open();
                }
                catch (Exception e)
                {
                    System.Web.HttpContext.Current.Response.Write(e.Message);
                }
            }
        }private void closeConnection()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();        }
        }
    以上是打开和关闭的连接,LZ可以参考下