我正在制作图书馆管理软件.我有一个获取书籍信息的方法:
public static Book GetInfoAboutBook(string title)
{
using (SqliteConnection db = new SqliteConnection("Filename=" + fileName))
{
db.Open();
Book b = new Book();
SqliteCommand sqliteCommand = new SqliteCommand
{
Connection = db,
CommandText = "select Title, Author, Publisher, ISBN, Quantity, CoverImageLocation, Tags from MyBooks where Title = '@Title'",
};
sqliteCommand.Parameters.AddWithValue("@Title", title);
SqliteDataReader query = sqliteCommand.ExecuteReader();
if(query.Read())
{
b.Title = query.GetString(0);
b.Author = query.GetString(1);
b.Publisher = query.GetString(2);
b.Quantity = query.GetInt32(3);
b.CoverImageLocation = query.GetString(4);
b.Tags = query.GetString(5);
}
return b;
}
}
Run Code Online (Sandbox Code Playgroud)
无论输入是什么,'b'都不会被初始化.我确保所有输入实际上都存在于数据库中.但它不起作用.我调试了这个函数.query.Read()永远不会成功.这就是书对象保持为空的方式.这是Book类:
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public string Publisher { get; set; }
public string ISBN { get; set; }
public int Quantity { get; set; }
public string CoverImageLocation { get; set; }
public string Tags { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
注意:标题保证是唯一的.
这是数据库的架构:
CREATE TABLE MyBooks(标题文本主键,作者文本不为空,发布者文本不为空,ISBN文本不为空,数量int不为空,CoverImageLocation文本不为空,标签文本不为空);
问题出在你的SQL中:
select Title, Author, Publisher, ISBN, Quantity, CoverImageLocation, Tags
from MyBooks where Title = '@Title'
Run Code Online (Sandbox Code Playgroud)
那是在寻找一本字面标题的书@Title- 它不是在寻找参数值,因为你的@Title引号是文本文字.你想要的SQL:
select Title, Author, Publisher, ISBN, Quantity, CoverImageLocation, Tags
from MyBooks where Title = @Title
Run Code Online (Sandbox Code Playgroud)
差异恰到好处.SQL将@Title在搜索中使用该参数.
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |