网站是能运行的起来,只不过时不时的会出现下面这个错误,没有规律的。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();
                }
            }
        }
    }

解决方案 »

  1.   

    接着楼上的: 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);
                    }
                }
            }
        }
      

  2.   

    sqllite 不支持多个线程同时写入,对于b/s,不应该使用
      

  3.   

    多人同时向sqlite插入数据的话,容易出错?
      

  4.   

    刚找到一篇文章:
    SQLite省略了一些人们认为比较有用的特性, 例如高并发性、 严格的存取控制、 丰富的内置功能、 存储过程、复杂的SQL语言特性、 
    SQLite最佳试用场合    网站
    嵌入式设备和应用软件
    应用程序文件格式
    。。
    估计是我上面的操作类写的不够严谨。
      

  5.   

    那些代码都是操作数据库的。就是普通的读取到DataSet,DataTable 执行SQL语句之类的
      

  6.   

    我也遇到了同样的问题,终于解决了connectionstring里面要加上";Pooling=True",sqllite默认没有打开连接池。要指定。
      

  7.   

    谢谢sweatypalms,我的连接代码改成如下:
    <add key="ConnectionSQLite" value="App_Data/test.db3"/>Data Source=" + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnectionSQLite"]) + ";Version=3;Pooling=True;
    ===============
    不知跟你的是否一样,能贴出你的代码吗?你现在还有出现这错误么?
      

  8.   

    只要换成mysql就可以解决了。
      

  9.   

    现在数据库已经是采用Sqlite了,换mysql是不可能了。
      

  10.   

    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里面很可能报异常,但应该不影响数据库
      

  11.   

    不允许多线程访问,2楼正解。我就是这样解决的:http://duan.es