当前位置:编程学习 > C#/ASP.NET >>

各位大侠,帮忙看一下,这问题怎么解决

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



--------------------编程问答-------------------- 出错在:
sqlcmd2.Dispose();
--------------------编程问答-------------------- 提示已经很清楚了,先关掉打开的reader,然后在释放 --------------------编程问答--------------------
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(); --------------------编程问答-------------------- 出错在:
sqlcmd2.ExecuteReader();



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


--------------------编程问答-------------------- reader还没关闭...

ps:别把.NET当asp写... --------------------编程问答-------------------- access 数据库却可以执行的 --------------------编程问答--------------------
while (reader.Read())
{//...
sqlcmd2.ExecuteReader();//换成sqlcmd2.ExecuteNonQuery();试试
}
--------------------编程问答--------------------
引用 7 楼 wyfde123 的回复:
C# code
while (reader.Read())
{//...
sqlcmd2.ExecuteReader();//换成sqlcmd2.ExecuteNonQuery();试试
}


一样不行啊 --------------------编程问答-------------------- 用
 using()
{} --------------------编程问答--------------------
引用 9 楼 wdywqc 的回复:

 using()
{}


??? --------------------编程问答-------------------- 先关闭
在打开 --------------------编程问答--------------------
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可以参考下 --------------------编程问答-------------------- --------------------编程问答--------------------
引用 11 楼 jaylongli 的回复:
先关闭
在打开


关了就不能再循环了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,