在DataReader循环中做Update操作,总是能catch到错误,如果用DataSet就不会出错???
SqlDataReader dr;while(dr.Read())
{
Update操作
} --------------------编程问答-------------------- 这个与SqlDataReader 还是 DataSet关系不大吧 --------------------编程问答-------------------- 我试过很多次,每次把前面一点一改就可以了,别的地方的代码都不用动 --------------------编程问答-------------------- 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
--------------------编程问答-------------------- 帮帮忙呀 --------------------编程问答-------------------- 可能reader和updatecommand使用了同一connection --------------------编程问答-------------------- 报什么错? --------------------编程问答-------------------- 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
我用了两个不同的连接,conn,conn1 --------------------编程问答-------------------- 还有一个情况就是:如果我只测试几条数据,也是正常的 --------------------编程问答--------------------
using System.Threading;
namespace DelTag
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnUpdate;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string SQLConn;
private SqlConnection conn;
private System.Windows.Forms.TextBox txt1;
private SqlConnection conn1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label lab;
Thread test1;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
SQLConn=System.Configuration.ConfigurationSettings.AppSettings["SQLConn"];
conn=new SqlConnection(SQLConn);
conn1=new SqlConnection(SQLConn);
//线程
test1=new Thread(new ThreadStart(GoRelace));
test1.IsBackground = true;
test1.Name = "test1";
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnUpdate = new System.Windows.Forms.Button();
this.txt1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.lab = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnUpdate
//
this.btnUpdate.Location = new System.Drawing.Point(104, 80);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.TabIndex = 0;
this.btnUpdate.Text = "执行";
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// txt1
//
this.txt1.Location = new System.Drawing.Point(40, 40);
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(408, 21);
this.txt1.TabIndex = 1;
this.txt1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(216, 80);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "退出";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// lab
//
this.lab.Location = new System.Drawing.Point(40, 16);
this.lab.Name = "lab";
this.lab.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 149);
this.Controls.Add(this.lab);
this.Controls.Add(this.button1);
this.Controls.Add(this.txt1);
this.Controls.Add(this.btnUpdate);
this.Name = "Form1";
this.Text = "过滤";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnUpdate_Click(object sender, System.EventArgs e)
{
btnUpdate.Enabled=false;
test1.Start();//开线程
}
/// <summary>
/// 执行循环
/// </summary>
public void GoRelace()
{
string SQL="select Question_ID,Body,Answer,Explain from T_Question nolock where Subject=159 and Grade in (149,150,151) order by Question_ID";
SqlDataReader dr=null;
conn.Open();
conn1.Open();
SqlCommand cmd=new SqlCommand(SQL,conn);
dr=cmd.ExecuteReader();
int ID=0;
string strBody="";
string strAnswer="";
string strExplain="";
string str1="";
string str2="";
string str3="";
int j=0;
try
{
while(dr.Read())
{
ID=(int)dr["Question_ID"];
strBody = (string)dr["Body"];//题干部分
strAnswer=(string)dr["Answer"];
strExplain=(string)dr["Explain"];
str1=GL(strBody);
str2=GL(strAnswer);
str3=GL(strExplain);
txt1.Text="ID:"+ID;
if(strBody!=str1||strAnswer!=str2||strExplain!=str3)
{
j++;
lab.Text=j.ToString();
WiteToDB(ID,str1,str2,str3);
txt1.Text="替换ID:"+ID;
}
}
conn.Close();
conn1.Close();
}
catch(Exception ex)
{
StreamWriter sw=new StreamWriter("Err.txt", true, Encoding.UTF8);
sw.WriteLine(ID+"---"+ex.Message.ToString());
sw.Close();
}
MessageBox.Show("完成");
btnUpdate.Enabled=true;
}
public void WiteToDB(int ID,string strBody,string strAnswer,string strExplain)
{
strBody=strBody.Replace("'","''");
strAnswer=strAnswer.Replace("'","''");
strExplain=strExplain.Replace("'","''");
string str="update T_question set Body='"+strBody+"' ,Answer='"+strAnswer+"' ,Explain='"+strExplain+ "' where Question_id="+ID;
SqlCommand cmd1=new SqlCommand(str,conn1);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
}
public string GL(string str)
{
/*
// 正则表达式对象table
Regex re1 = new Regex(@"<\/?table[^\>]*\>", RegexOptions.IgnoreCase);
str = re1.Replace(str,"<BR>");
Regex re2 = new Regex(@"<\s*?\/?p[^\>]*?>", RegexOptions.IgnoreCase);
str = re2.Replace(str,"<BR>");
Regex re3 = new Regex(@"<tr[^\>]*>", RegexOptions.IgnoreCase);
str = re3.Replace(str," ");
Regex re4 = new Regex(@"<\s*?\/tr>", RegexOptions.IgnoreCase);
str = re4.Replace(str,"<BR>");
Regex re5 = new Regex(@"<\/?td[^\>]*?>", RegexOptions.IgnoreCase);
str = re5.Replace(str," ");
Regex re6 = new Regex(@"<\/?span[^\>]*?>", RegexOptions.IgnoreCase);
str = re6.Replace(str,"");
Regex re7 = new Regex(@"<\/?o:p>", RegexOptions.IgnoreCase);
str = re7.Replace(str,"");
Regex re8 = new Regex(@"<\/?TBODY[^\>]*?>", RegexOptions.IgnoreCase);
str = re8.Replace(str,"");
Regex re9 = new Regex(@"<\/?div[^\>]*?>", RegexOptions.IgnoreCase);
str = re9.Replace(str,"");
Regex re10 = new Regex(@"<\/?strong>", RegexOptions.IgnoreCase);
str = re10.Replace(str,"");
Regex re11 = new Regex(@"<\/?b>", RegexOptions.IgnoreCase);
str = re11.Replace(str,"");
Regex re12 = new Regex(@"<b\s+[^\>]*>", RegexOptions.IgnoreCase);
str = re12.Replace(str,"");
Regex re13 = new Regex(@"<\/?LOCKQUOTE>", RegexOptions.IgnoreCase);
str = re13.Replace(str,"");
//Regex re14 = new Regex(@"<\/?p[^\>]*?>", RegexOptions.IgnoreCase);
//str = re14.Replace(str,"");
Regex re15 = new Regex(@"<\/?font[^\>]*?>", RegexOptions.IgnoreCase);
str = re15.Replace(str,"");
Regex re16 = new Regex(@"<\/?i>", RegexOptions.IgnoreCase);
str = re16.Replace(str,"");
Regex re17 = new Regex(@"<i\s+[^\>]*>", RegexOptions.IgnoreCase);
str = re17.Replace(str,"");
Regex re18 = new Regex(@"<U>( | |\s|\.|_|\+)*</U>", RegexOptions.IgnoreCase);
str = re18.Replace(str,"_____");
Regex re19 = new Regex(@"<\/?u>", RegexOptions.IgnoreCase);
str = re19.Replace(str,"");
Regex re20 = new Regex(@"[\s| ]+[\r\n]", RegexOptions.IgnoreCase);
str = re20.Replace(str,"");
Regex re21 = new Regex(@"( ){3,}", RegexOptions.IgnoreCase);
str = re21.Replace(str," ");
Regex re22 = new Regex(@"(<br>[\s| ]*){2,}", RegexOptions.IgnoreCase);
str = re22.Replace(str,"<BR>");
str=str.Replace("+","+");
str=str.Replace("-","-");
str=str.Replace("=","=");
*/
str=str.Replace(" "," ");
return str;
}
private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}
--------------------编程问答-------------------- 没说清除,谁知道你什么问题 --------------------编程问答-------------------- 什么地方需要清除??谢谢 --------------------编程问答-------------------- 在DataReader循环中做Update操作,总是能catch到错误,如果用DataSet就不会出错
补充:.NET技术 , C#