关于网友提出的“jsp连接数据库 在循环读取数据库时出现 连接超时 在线等!!”问题疑问,本网通过在网上对“jsp连接数据库 在循环读取数据库时出现 连接超时 在线等!!”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:jsp连接数据库 在循环读取数据库时出现 连接超时 在线等!!
描述: 1.页面点cs文件
protected void Button1_Click(object sender, EventArgs e)
{
string caseno = "2009030001";
string version="";
string notarycert="";
string invoice="";
string genuine="";
string bokno = "";
string memo = "";
int isApply = 1;
string[] ClientNo ={ "", "" };
string[] ClientName ={ "", "" };
string[] BookNo ={ "P2009010001", "P2009010002" };
string[] BookName ={ "", "" };
string[] PrinterName ={ "", "" };
string[] PrintNo ={ "", "" };
string[] BatchNo ={ "", "" };
string[] PirateReason={"",""};
int BookIndex = 2;
SqlConnection MyCn = new SqlConnection(ConfigurationManager.AppSettings["SQLConnectionString"].ToString());
string InsertCommand = "INSERT INTO [SurveyObject] ([caseno],[parenttype],[parentid],[prodno],[prodname],[piracyreason]) VALUES (@caseno,@parenttype,@parentid,@prodno,@prodname,@piracyreason)";
string InsertCm = "insert into SurveyClose (closeno,caseno,version,notarycert,invoice,genuine,bokno,surveyor,applytime,recordtime,memo) values (@closeno,@caseno,@version,@notarycert,@invoice,@genuine, @bokno,@surveyor,@applytime,@recordtime,@memo)";
string UpdateProductionCm = "update Production set prodname=@prodname, publisher=@publisher,version=@version,batch=@batch where prodno=@prodno";//更新
string InsertProductionCm = "insert into Production (prodno,prodname,publisher,version,batch) values (@prodno,@prodname,@publisher,@version,@batch)";//插入
string parenttype = "1";//0shenqingbiao 1lia an biao
MyCn.Open();
SqlTransaction Tr = MyCn.BeginTransaction();
try
{
SqlCommand MyCm = new SqlCommand();
SqlCommand MyCmProduction = new SqlCommand();
MyCm.Connection = MyCn;
MyCm.Transaction = Tr;
MyCmProduction.Connection = MyCn;
MyCmProduction.Transaction = Tr;
string MaxSurveyCloseNo = CloseOP.SelectMaxSurveyCloseNo();
for (int i = 0; i < BookIndex; i++)
{
// test = clino.Length.ToString() + "-" + cliname.Length.ToString() + "-" + BookNo.Length.ToString() + "-" + BookName.Length.ToString() + "-" + PrinterName.Length.ToString() + "-" + version.Length.ToString() + "_" + BatchNo.Length.ToString() + "-" + PirateReason.Length.ToString() + "_" + index.ToString();
////判断作品是否存在,不存在插入,存在更新
//bool flag = IProductionOP.AlreadyHasProdNo(BookNo[i].Trim());
if (BookNo[i].Trim() != "" && IProductionOP.LAlreadyHasProdNo(BookNo[i].Trim()))
{
//插入SurveyObject
MyCm.CommandText = InsertCommand;
MyCm.Parameters.Clear();
SqlParameter caseno1 = new SqlParameter("@caseno", caseno);
SqlParameter parenttype1 = new SqlParameter("@parenttype", parenttype);
SqlParameter parentid1 = new SqlParameter("@parentid", MaxSurveyCloseNo);
SqlParameter prodno1 = new SqlParameter("@prodno", BookNo[i]);
SqlParameter prodname1 = new SqlParameter("@prodname", BookName[i]);
SqlParameter piracyreason1 = new SqlParameter("@piracyreason", PirateReason[i]);
MyCm.Parameters.Add(caseno1);
MyCm.Parameters.Add(parenttype1);
MyCm.Parameters.Add(parentid1);
MyCm.Parameters.Add(prodno1);
MyCm.Parameters.Add(prodname1);
MyCm.Parameters.Add(piracyreason1);
MyCm.ExecuteNonQuery();
//判断Production里是否有prodno,没有则插入新行。有则修改
MyCmProduction.CommandText = UpdateProductionCm;
MyCmProduction.Parameters.Clear();
SqlParameter newprodno = new SqlParameter("@prodno", BookNo[i].Trim());
SqlParameter newprodname = new SqlParameter("@prodname", BookName[i]);
SqlParameter newpublisher = new SqlParameter("@publisher", PrinterName[i]);
SqlParameter newversion = new SqlParameter("@version", version);
SqlParameter newbatch = new SqlParameter("@batch", BatchNo[i]);
MyCmProduction.Parameters.Add(newprodno);
MyCmProduction.Parameters.Add(newprodname);
MyCmProduction.Parameters.Add(newpublisher);
MyCmProduction.Parameters.Add(newversion);
MyCmProduction.Parameters.Add(newbatch);
MyCmProduction.ExecuteNonQuery();
}
MyCmProduction.Dispose();
MyCm.Dispose();
}
Tr.Commit();
}
catch (SqlException ex)
{
string Message = ex.Message;
Tr.Rollback();
}
finally
{
MyCn.Close();
}
2.上面调用的方法 检查数据库有没有书籍 没有插入 有就更新
public bool LAlreadyHasProdNo(string prodno)
{
bool flag = false;
try
{
string SelectCmPr = "select p.prodno from Production p where p.prodno='" + prodno + "'";
SqlCommand MyCmSl = new SqlCommand();
MyCmSl.CommandText = SelectCmPr;
MyCmSl.Connection = MyCn;//已经在上面定义了 MyCn = new SqlConnection(ConfigurationManager.AppSettings["SQLConnectionString"].ToString());
MyCn.Open();
SqlDataReader MyCmRed;
MyCmRed = MyCmSl.ExecuteReader();
while (MyCmRed.Read())
{
flag = true;
}
MyCmRed.Close();
}
catch (SqlException ex)
{
string error = ex.Message;
return flag;
}
finally
{
MyCn.Close();
}
return flag;
}
}
解决方案1: 1.适当延长数据库超时时间
2.取值最好用存储过程
解决方案2: 建议写成存储过程,
解决方案3: 是不是数据库连接池中的连接请求完了,你多开个连接,在连接字符串中加 max pool size=2000
在请求数据库连接的时候用完了要及时关闭,不要循环请求数据库连接。。
解决方案4:
server为数据库名称
UID为登陆数据库的ID
PWD为登陆数据库的密码
DATABASE为数据库中具体需要操作的表名
timeout为最长连接时间
把timeout设置大一点
解决方案5: 请求过多,数据库timeout时间过短,你可以设置Connection.timeout值大一些。默认为15秒。。
以上介绍了“jsp连接数据库 在循环读取数据库时出现 连接超时 在线等!!”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2810572.html