在 Asp.Net 中使用 SqLite

发布于 2024-12-17 12:34:27 字数 1093 浏览 0 评论 0原文

我正在尝试在我的 Asp.Net 中使用 SqLite。我在连接字符串 web.config 中有下面的代码

<add name="ConnectionStringName" connectionString="data source=MembersLastLogin3.s3db" providerName="System.Data.SQLite" />

的代码

public  int ExecuteNonQuery(string sql)
    {
        try
        {
            string con = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; 
            SQLiteConnection cnn = new SQLiteConnection(con);
            cnn.Open();
            SQLiteCommand mycommand = new SQLiteCommand(cnn);
            mycommand.CommandText = sql;
            int rowsUpdated = mycommand.ExecuteNonQuery();
            cnn.Close();
            return rowsUpdated;              
        }
        catch { throw; }
    }
}

,这是运行查询和这个简单查询

sql = "INSERT INTO LastLogin (MemId ) VALUES ('" + res + "');";
                LocalDb.ExecuteNonQuery(sql);

,但我收到错误“表 LastLogin 不存在”

我已经使用过 SqLitein .Net,但这是我第一次在 Asp.Net 中使用它,我确信该表存在,但似乎可以连接到它,我的问题在哪里? web.config 中的设置是否足够?有可用的教程吗?谢谢

I'm trying to use SqLite in my Asp.Net. I have the code below in connectionstring web.config

<add name="ConnectionStringName" connectionString="data source=MembersLastLogin3.s3db" providerName="System.Data.SQLite" />

and here is the code for running query

public  int ExecuteNonQuery(string sql)
    {
        try
        {
            string con = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString; 
            SQLiteConnection cnn = new SQLiteConnection(con);
            cnn.Open();
            SQLiteCommand mycommand = new SQLiteCommand(cnn);
            mycommand.CommandText = sql;
            int rowsUpdated = mycommand.ExecuteNonQuery();
            cnn.Close();
            return rowsUpdated;              
        }
        catch { throw; }
    }
}

and this simple query

sql = "INSERT INTO LastLogin (MemId ) VALUES ('" + res + "');";
                LocalDb.ExecuteNonQuery(sql);

but I get the error "Table LastLogin doesn't exist"

I have used SqLitein .Net already but it's the first time I'm using it in Asp.Net,I'm sure the table exists but it seems it can connect to it, where is my problem? is the setting in web.config is enough? is there any tutorial available ? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霓裳挽歌倾城醉 2024-12-24 12:34:27

我想你的文件db存储在asp.net网站的App_Data目录中。

因此,只需尝试更改您的连接字符串:

<add name="ConnectionStringName" connectionString="data source=|DataDirectory|MembersLastLogin3.s3db" providerName="System.Data.SQLite" />

I suppose your file db is stored in App_Data directory of the asp.net website.

So just try to change your connection string with:

<add name="ConnectionStringName" connectionString="data source=|DataDirectory|MembersLastLogin3.s3db" providerName="System.Data.SQLite" />
瀟灑尐姊 2024-12-24 12:34:27

使用 GetSchema 方法验证 SQLite 数据库中是否存在表:

using ( var conn = new SQLiteConnection( "Data Source=<PathToDb>\\MembersLastLogin3.s3db" ) )
{
  DataTable tbl = conn.GetSchema("Tables")
  // The DataTable will list all the tables in the
  // db as well as information about each table.
}

最可能的原因是您使用的连接字符串没有指向 SQLite 数据库,这就是为什么您会看到错误表不存在。

作为关于您发布的示例代码的旁注,如果您没有对异常执行任何操作,那么如果异常只是被抛出,则无需尝试/捕获异常。

Verify if the table(s) exist in your SQLite db using the GetSchema method:

using ( var conn = new SQLiteConnection( "Data Source=<PathToDb>\\MembersLastLogin3.s3db" ) )
{
  DataTable tbl = conn.GetSchema("Tables")
  // The DataTable will list all the tables in the
  // db as well as information about each table.
}

The most likely cause is the connection string you are using is not pointing to the SQLite database which is why you'll see the error table does not exist.

As a side note regarding the sample code you posted, if you are not doing anything with the exception, then there is no need to try/catch the exception if the exception is just going to be thrown back.

扶醉桌前 2024-12-24 12:34:27

编辑:看起来您需要指定绝对路径:代码访问的SQLite没有表?

Edit: Looks like you need to specify an absolute path: SQLite accessed by code has no tables?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文