错误信息如下:
写了如下的一个方法来返回数据操作影响的行数:如下
实现代码如下:

private int GetReturnValue(string sStr, string conn) {
OleDbConnection odbconn = AccessHelp(conn);
OleDbCommand odbcmd = new OleDbCommand(sStr, odbconn);
return odbcmd.ExecuteNonQuery();
}

用下面的一个方法来调用这个类:
实现代码如下:

public int wsbm(string[] str) {
StringBuilder sb=new StringBuilder();
sb.Append("INSERT INTO ").Append("wsbm(zy, studentname, parentname)");
sb.Append(" VALUES (");
sb.Append("'" + str[0] + "', '" + str[1] + "', '" + str[2] + "'");
sb.Append(")");
return GetReturnValue(sb.ToString(), "ODBconn");
}

在这个事件中发送数据:
实现代码如下:

protected void Bttj_Click(object sender, EventArgs e) {
string[] sStr = new string[] {
this.ddlzy.SelectedItem.Text,
this.tbName.Text,
this.tbbb.Text,
};
if (ad.wsbm(sStr) > 0) {
Response.Write("<script>alert('插入成功!')</script>");
}
}

执行了之后的结果如下图,这是什么原因???

我把能连接到Access数据库所有的方式全用了下,结果还是这个样,连接方式如下:
实现代码如下:

private OleDbConnection AccessHelp(string str){
OleDbConnection odbconn = new OleDbConnection();
try{
string sStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb");
odbconn.ConnectionString = sStr2;
if (odbconn.State == ConnectionState.Closed){
odbconn.Open();
}
else{
sStr2 = "DBQ=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};";
odbconn.ConnectionString = sStr2;
if (odbconn.State == ConnectionState.Closed){
odbconn.Open();
}
}
}
catch{
try{
//odbconn = new OleDbConnection(ConfigurationManager.ConnectionStrings[str].ConnectionString);
odbconn.ConnectionString = ConfigurationManager.ConnectionStrings[str].ConnectionString;
if (odbconn.State == ConnectionState.Closed){
odbconn.Open();
}
}
catch{
string sStr1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;Data Source=.\App_Data\#fdaeg35@#gds.mdb";
//odbconn = new OleDbConnection(sStr1);
odbconn.ConnectionString = sStr1;
if (odbconn.State == ConnectionState.Closed){
odbconn.Open();
}
}
}
return odbconn;
}

我的上片中把链接的方式全部在本地测试了下,一点问题也没有,性能方面一样是绝对没问题的,他是在我开发完这个的时候在本地是好的,一发到域名空间去就有问题来了,这里我看了下我的异常问题:异常结果如图

 结果把英文给翻译过来说的是:请求超时,连接池已经达到了最高上限制。看完后心多凉了一节,回想了下,原来我的连接已经被系统给自己默认了15秒请求时间,后来我直接把连接时间改成了1分钟。如代码:

实现代码如下:

private OleDbConnection AccessHelp(string str) {
OleDbConnection odbconn = new OleDbConnection();
try {
string sStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;connection timeout=120;Data Source=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb");
odbconn.ConnectionString = sStr2;
if (odbconn.State == ConnectionState.Closed) {
odbconn.Open();
}
else{
sStr2 = "DBQ=" + Server.MapPath(@"App_Data\#fdaeg35@#gds.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};connection timeout=120;";
odbconn.ConnectionString = sStr2;
if (odbconn.State == ConnectionState.Closed) {
odbconn.Open();
}
}
}
catch {
try {
//odbconn = new OleDbConnection(ConfigurationManager.ConnectionStrings[str].ConnectionString);
odbconn.ConnectionString = ConfigurationManager.ConnectionStrings[str].ConnectionString;
if (odbconn.State == ConnectionState.Closed) {
odbconn.Open();
}
}
catch {
string sStr1 = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=false;connection timeout=120;Data Source=.\App_Data\#fdaeg35@#gds.mdb";
//odbconn = new OleDbConnection(sStr1);
odbconn.ConnectionString = sStr1;
if (odbconn.State == ConnectionState.Closed) {
odbconn.Open();
}
}
}
return odbconn;
}

结果我把这些代码改成了这样了,就解决了连接超时的问题了。
在上面的几篇中写到连接数据库,出现不稳定是连接池的默认时间改长些,改了之后,要是在很卡的时间内,不停的刷新,可是,就出来了有一个严重的问题是,平凡的数据丢失问题来了,结果我用测试软件来测试,没看到结果,因为我们本地妹有办法测试出来,我的配置和服务器的配置是有点不同的,在说了,通常以个服务器不是和我们的本地那样,就我们一个网站在用,我的类是把SQLHelper连接数据Sql数据库的连接方式改成了连接Access数据库的,类就排除了问题,我只是直接掉用他的类,给了些参数而以,所以这些全是没什么问题的,可以是在很卡的时间内就会出现,去望是找便了,就是没看到结果,我就在后来在英文的博客中看到,原来我勿略了一个属性,这个属性是:OldbConnection成员下的ConnectionTimeout这个连接错误并发时间在什么时间内结束,如果你是在很卡的情况下,正好就是被默认的30秒个定义成看超时状态,你修下里面的如何文件就会恢复正常。结果我个这个属性给了个1分钟,就正常了。我建议你们别给得太长的时间了,给长时间了,一但真的出错了,那可是要把别人的电脑卡定屏幕的哦。总是在那尝试这连接。那经后就会越来越少的人来访问了。
实现代码如下:

private static void PrepareCommand(OldbCommand command, OldbConnection connection, OldbTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection) {
if (command == null) throw new ArgumentNullException("command");
if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
// If the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open) {
mustCloseConnection = true;
connection.Open();
}
else {
mustCloseConnection = false;
}
// Associate the connection with the command
command.Connection = connection;
// Set the command text (stored procedure name or SQL statement)
command.CommandText = commandText;
//Set the command Time
command.CommandTimeout = 60;
// If we were provided a transaction, assign it
if (transaction != null) {
if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
command.Transaction = transaction;
}
// Set the command type
command.CommandType = commandType;
// Attach the command parameters if they are provided
if (commandParameters != null) {
AttachParameters(command, commandParameters);
}
return;
}

上面是我用的那个类,修改的时间位置。已经用背景标出来了。
呵呵,这个连接不稳定的问题到此就结束了。
本文专业技术是ASP.Net开发,在次谢谢你对我的博客的关注。
有问题也可以加我Q我哦。

以上就是【asp.net Accee数据库连接不稳定解决方案】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论