0

我编写了这个 C# 代码来将一个文本数据插入到我的数据库表中:

SQLiteConnection con = new SQLiteConnection("Data Source=tDB1.sqlite;Version=3;");
string q1 = "INSERT INTO tMembers(mName) VALUES(?)";

SQLiteCommand cmd1 = new SQLiteCommand(q1, con);
cmd1.Parameters.AddWithValue("@mName", txtName.Text);

con.Open(); 
cmd1.ExecuteNonQuery(); 
con.Close();

我将 tDB1.sqlite 复制到我的程序文件夹并在 sqlite 中创建 tMembers 表。

当我运行我的程序时,程序调试器显示 ""cmd1.ExecuteNonQuery();"" 行并向我显示:SQLite 错误没有这样的表

有什么可以帮助我的吗?谢谢你。

4

2 回答 2

0

仅基于当前目录查找文件有点弱。如果您确定数据库完全放置在可执行文件的相同目录中(即从 Visual Studio $ 调试时,projectdir$\bin\debug您可以强制执行路径,如下面的代码所示:

SQLiteConnection con = new SQLiteConnection(string.Format("Data Source={0};Version=3;"
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"tdb1.sqlite"))
    );
于 2012-07-28T11:45:09.250 回答
0

更改命令字符串

string q1 = "INSERT INTO tMembers(mName) VALUES(?)";

string q1 = "INSERT INTO tMembers(@mName) VALUES(?)";

列名不需要保持对应,但在执行整个命令时必须与args名一致。有关此的更多信息,您可以在 google 中搜索“ PL/SQL ”。

此外,您是否使用“VALUES(?)”执行此命令?我不确定这是否可行INSERT INTO tMembers(@mName) DEFAULT VALUES,如果没有 NOT NULL 约束,您可以改为执行。

于 2013-08-19T02:53:20.817 回答