简体   繁体   English

需要C#中的SQLite基本帮助-从查询返回字符串或整数

[英]Basic help with SQLite in C# needed - returning strings or ints from a query

I'm using Microsoft Visual C# 2008 Express Edition with SqlLite. 我正在将Microsoft Visual C#2008 Express Edition与SqlLite一起使用。 I'm successfully able to open my database and with C# code, add entries to my tables. 我可以成功打开数据库,并使用C#代码将条目添加到表中。

When it comes to retriving data, I'm having some issues and have been searching and searching the internet for basic tutorial information on how to do these basic things... 在检索数据时,我遇到了一些问题,一直在互联网上搜索有关如何做这些基本事情的基本教程信息。

Here's my code... (after I've opened up a connection to the database which is called 'conn' here): 这是我的代码...(在我打开与数据库的连接后,这里称为“ conn”):

SQLiteCommand cmd = new SQLiteCommand(conn);
cmd.CommandText = "select myField1,myField2 from myTable where myField3 = '" + tempstring + "';";
cmd.CommandType = CommandType.Text;
SQLiteDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
  string tmp = reader.GetString(0);
  System.Console.WriteLine(" my output = " + tmp);
}

When I execute this... I get no errors and because I get no output on that last line, it looks like the while loop is not executing at all. 当我执行此操作时...我没有错误,因为在最后一行没有任何输出,因此看起来while循环根本没有执行。

I'm a beginner to this stuff... what am I missing and is there a good web resource where I can learn these basic things? 我是这些东西的初学者...我想念的是什么?是否有很好的网络资源可用来学习这些基本知识? I'm pretty comfortable in SQL on it's own... just not integrated in C# like this... 我对SQL本身很满意...只是没有像这样集成在C#中...

First, remove the hurtful trailing semicolon from the line while (reader.Read()); 首先,在while (reader.Read());删除行尾有害的分号while (reader.Read()); ...! ...!

This looks correct to me. 在我看来,这是正确的。 Does the property reader.HasRows return true for your query? 属性reader.HasRows是否对您的查询返回true?

A couple of side issues to be aware of are: 需要注意的一些附带问题是:

  1. Be sure to dispose of your SQL resources by wrapping your objects in using { } blocks. 确保通过使用{}块包装对象来处置SQL资源。
  2. Consider using parameterized queries instead of injecting the query parameter directly in the SELECT statement. 考虑使用参数化查询,而不是直接在SELECT语句中插入查询参数。

Answering your question on how to write parameterized queries: 回答有关如何编写参数化查询的问题:

cmd.CommandText = "select myField1,myField2 from myTable where myField3 = @tempString;";
SQLiteParameter param = new SQLiteParameter("@tempString");
cmd.Parameters.Add(param);

// you can modify that value without touching the sql statement (which means you could cache the above command)
param.Value = tempstring;
SQLiteDataReader reader = cmd.ExecuteReader();
[...]

Parameters in SQLite can have several forms which you can find here . SQLite中的参数可以有几种形式,您可以在这里找到。

See here for more info on parameterized queries. 有关参数化查询的更多信息,请参见此处

Good one, Alex. 好人,Alex。

In addition to that and since you are beginning with sqlite (you may want to delete second L from the tag), remember that sqlite does not really guaranty data type safety on the database level. 除此之外,由于您是从sqlite开始的(可能要从标记中删除第二个L),请记住,sqlite并不能真正保证数据库级别的数据类型安全。

Not to divert you from your Sqlite question, but if you are having comfort issues with Sqlite queries embedded in C#, you could try NHibernate coupled with Fluent NHibernate . 并不是为了使您摆脱Sqlite问题,但是如果您对C#中嵌入的Sqlite查询遇到舒适问题,则可以尝试将NHibernateFluent NHibernate结合使用。 These technologies provide an excellent data access mechanism into databases, including Sqlite. 这些技术为数据库(包括Sqlite)提供了出色的数据访问机制。

NHibernate requests into Sqlite are very fast, and you won't have to worry about some of the Sqlite idiosyncrasies. NHibernate向Sqlite的请求非常快,您不必担心Sqlite的某些特质。 If you build out your data-access layer properly with NHibernate, you should be able to up-scale to a more robust database very quickly. 如果您使用NHibernate正确构建了数据访问层,那么您应该能够迅速升级到更强大的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM