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());
}
{
//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();
{//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);
{//...
sqlcmd2.ExecuteReader();//换成sqlcmd2.ExecuteNonQuery();试试
}
{
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可以参考下