在 Asp.Net 中使用 SqLite
我正在尝试在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你的文件db存储在asp.net网站的App_Data目录中。
因此,只需尝试更改您的连接字符串:
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:
使用 GetSchema 方法验证 SQLite 数据库中是否存在表:
最可能的原因是您使用的连接字符串没有指向 SQLite 数据库,这就是为什么您会看到错误表不存在。
作为关于您发布的示例代码的旁注,如果您没有对异常执行任何操作,那么如果异常只是被抛出,则无需尝试/捕获异常。
Verify if the table(s) exist in your SQLite db using the GetSchema method:
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.
编辑:看起来您需要指定绝对路径:代码访问的SQLite没有表?
Edit: Looks like you need to specify an absolute path: SQLite accessed by code has no tables?