asp.net+Sqlite 时不时出现Unable to open the database file

Netcean 2012-02-10 01:55:52
网站是能运行的起来,只不过时不时的会出现下面这个错误,没有规律的。app_data已经加了everyone写入权限了。
=================
Unable to open the database file
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SQLite.SQLiteException: Unable to open the database file

源错误:


行 162: {
行 163: if (connection.State != ConnectionState.Open)
行 164: connection.Open();
行 165: }
行 166:

===============================
下面是我的类部份方法,也以及及时的关闭了数据库连接。大家帮忙看看哪里还需要改进的。谢谢
[code=C#]
public class SQLiteHelper
{
public SQLiteHelper()
{

}
//数据库连接字符串(web.config来配置),可以动态更改SQLString支持多数据库.
public static string connectionString = "Data Source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionSQLite"]) + ";Version=3;";

public static SQLiteParameter GetOleDbParameter(string paraName, DbType type, int paraSize, Object value)
{
SQLiteParameter para = new SQLiteParameter(paraName, type, paraSize);
para.Value = value;
return para;
}
public static string GetTableData(string Fields, string ID)
{
string Sql = "select " + Fields + " From [table] where id='" + ID + "'";
return GetData(Sql);
}

public static string GetData(string Sql)
{
object obj = GetSingle(Sql);
if (obj == null)
{
return "";
}
return obj.ToString();
}

public static int GetMaxID(string FieldName, string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}

public static bool Exists(string strSql)
{
object obj = GetSingle(strSql);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}

public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
{
object obj = GetSingle(strSql, cmdParms);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}

#endregion

/// <summary>
/// 保证数据库连接处于打开状态
/// </summary>
/// <param name="connection"></param>
public static void OpenDb(SQLiteConnection connection)
{
if (connection.State != ConnectionState.Open)
connection.Open();
}

public static int ExecuteSql(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
OpenDb(connection);
try
{
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
}
}
}

public static int ExecuteSql(string SQLString, string content)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
using (cmd)
{
SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
if (connection.State != ConnectionState.Open)
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
...全文
1237 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
RLib 2013-07-17
  • 打赏
  • 举报
回复
引用 8 楼 sweatypalms 的回复:
我也遇到了同样的问题,终于解决了connectionstring里面要加上";Pooling=True",sqllite默认没有打开连接池。要指定。
效果如何
袁圈er 2013-06-13
  • 打赏
  • 举报
回复
不允许多线程访问,2楼正解。我就是这样解决的:http://duan.es
xuan.ye 2012-03-02
  • 打赏
  • 举报
回复
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
using (cmd)
{
SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
if (connection.State != ConnectionState.Open)
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
这样的代码有问题,using 相当于 try finally,这样的话finally里面很可能报异常,但应该不影响数据库
  • 打赏
  • 举报
回复
没有可不可能,只是你想不想去做
Netcean 2012-03-02
  • 打赏
  • 举报
回复
现在数据库已经是采用Sqlite了,换mysql是不可能了。
xuan.ye 2012-03-02
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 netcean 的回复:]

这个帖又要以无解收场么?
[/Quote]
只要换成mysql就可以解决了。
Netcean 2012-03-02
  • 打赏
  • 举报
回复
这个帖又要以无解收场么?
Netcean 2012-02-27
  • 打赏
  • 举报
回复
谢谢sweatypalms,我的连接代码改成如下:
<add key="ConnectionSQLite" value="App_Data/test.db3"/>

Data Source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionSQLite"]) + ";Version=3;Pooling=True;
===============
不知跟你的是否一样,能贴出你的代码吗?你现在还有出现这错误么?
meic1985 2012-02-24
  • 打赏
  • 举报
回复
学习了
sweatypalms 2012-02-24
  • 打赏
  • 举报
回复
我也遇到了同样的问题,终于解决了connectionstring里面要加上";Pooling=True",sqllite默认没有打开连接池。要指定。
Netcean 2012-02-14
  • 打赏
  • 举报
回复
那些代码都是操作数据库的。就是普通的读取到DataSet,DataTable 执行SQL语句之类的
bakdfkuk 2012-02-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yeness 的回复:]

sqllite 不支持多个线程同时写入,对于b/s,不应该使用
[/Quote]
以前我好像用过多线程的。。。。。。。。
楼主这个问题还是等高手来吧,我是看到这么多代码就退下来了
Netcean 2012-02-14
  • 打赏
  • 举报
回复
大家帮忙想想会是哪里出了问题。谢谢
Netcean 2012-02-10
  • 打赏
  • 举报
回复
刚找到一篇文章:
SQLite省略了一些人们认为比较有用的特性, 例如高并发性、 严格的存取控制、 丰富的内置功能、 存储过程、复杂的SQL语言特性、
SQLite最佳试用场合

网站
嵌入式设备和应用软件
应用程序文件格式
。。。。。


估计是我上面的操作类写的不够严谨。
Netcean 2012-02-10
  • 打赏
  • 举报
回复
多人同时向sqlite插入数据的话,容易出错?
xuan.ye 2012-02-10
  • 打赏
  • 举报
回复
sqllite 不支持多个线程同时写入,对于b/s,不应该使用
Netcean 2012-02-10
  • 打赏
  • 举报
回复
接着楼上的:

public static object ExecuteSqlGet(string SQLString, string content)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
if (connection.State != ConnectionState.Open)
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}

public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
using (SQLiteCommand cmd = new SQLiteCommand(strSQL, connection))
{
SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
myParameter.Value = fs;
cmd.Parameters.Add(myParameter);
try
{
if (connection.State != ConnectionState.Open)
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}

public static object GetSingle(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SQLite.SQLiteException e)
{
//connection.Close();
throw new Exception(e.Message);
}
finally
{
connection.Close();
cmd.Dispose();
}
}
}
}

public static DataSet Query(string SQLString)
{

using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
try
{
DataSet ds = new DataSet();
using (SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection))
{
command.Fill(ds, "ds");
}
return ds;
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
}

}

public static DataSet Query(string SQLString, string TableName)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
DataSet ds = new DataSet();
try
{
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
using (command)
{
command.Fill(ds, TableName);
}
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
return ds;


}
}
public static DataSet Query(string SQLString, int Times)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
DataSet ds = new DataSet();
try
{
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
using (command)
{
command.SelectCommand.CommandTimeout = Times;
command.Fill(ds, "ds");
}
return ds;
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}


}
}

public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
OpenDb(connection);
using (SQLiteCommand cmd = new SQLiteCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
}
}
}

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧