asp.net+Sqlite 时不时出现Unable to open the database file
网站是能运行的起来,只不过时不时的会出现下面这个错误,没有规律的。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();
}
}
}
}
[解决办法]
sqllite 不支持多个线程同时写入,对于b/s,不应该使用
[解决办法]
以前我好像用过多线程的。。。。。。。。
楼主这个问题还是等高手来吧,我是看到这么多代码就退下来了
[解决办法]
我也遇到了同样的问题,终于解决了connectionstring里面要加上";Pooling=True",sqllite默认没有打开连接池。要指定。
[解决办法]
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里面很可能报异常,但应该不影响数据库