System.Data.SQLite参数问题

发布于 2024-07-17 18:42:38 字数 2291 浏览 7 评论 0原文

我有以下代码:

try
{
    //Create connection
    SQLiteConnection conn = DBConnection.OpenDB();

    //Verify user input, normally you give dbType a size, but Text is an exception
    var uNavnParam = new SQLiteParameter("@uNavnParam", SqlDbType.Text) { Value = uNavn }; 
    var bNavnParam = new SQLiteParameter("@bNavnParam", SqlDbType.Text) { Value = bNavn };
    var passwdParam = new SQLiteParameter("@passwdParam", SqlDbType.Text) {Value = passwd};
    var pc_idParam = new SQLiteParameter("@pc_idParam", SqlDbType.TinyInt) { Value = pc_id };
    var noterParam = new SQLiteParameter("@noterParam", SqlDbType.Text) { Value = noter };
    var licens_idParam = new SQLiteParameter("@licens_idParam", SqlDbType.TinyInt) { Value = licens_id };

    var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
    "VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam')", conn);
    insertSQL.Parameters.Add(uNavnParam); //replace paramenter with verified userinput
    insertSQL.Parameters.Add(bNavnParam);
    insertSQL.Parameters.Add(passwdParam);
    insertSQL.Parameters.Add(pc_idParam);
    insertSQL.Parameters.Add(noterParam);
    insertSQL.Parameters.Add(licens_idParam);
    insertSQL.ExecuteNonQuery(); //Execute query

    //Close connection
    DBConnection.CloseDB(conn);

    //Let the user know that it was changed succesfully
    this.Text = "Succes! Changed!";
}
catch(SQLiteException e)
{
    //Catch error
    MessageBox.Show(e.ToString(), "ALARM");
}

它执行完美,但是当我查看“brugere”表时,它插入了值:“@uNameParam”、“@bNavnParam”、“@passwdParam”、“@pc_idParam”、“@noterParam” ,字面上的“@licens_idParam”。 而不是更换它们。

我尝试设置断点并检查参数,它们确实具有正确的分配值。 所以这也不是问题。

我现在已经对此进行了很多修改,但没有运气,有人可以帮忙吗?

哦,作为参考,这里是 DBConnection 类中的 OpenDB 方法:

public static SQLiteConnection OpenDB()
{
    try
    {
        //Gets connectionstring from app.config
        const string myConnectString = "data source=data;";

        var conn = new SQLiteConnection(myConnectString);
        conn.Open();
        return conn;
    }

    catch (SQLiteException e)
    {
        MessageBox.Show(e.ToString(), "ALARM");
        return null;
    }
}

I have the following code:

try
{
    //Create connection
    SQLiteConnection conn = DBConnection.OpenDB();

    //Verify user input, normally you give dbType a size, but Text is an exception
    var uNavnParam = new SQLiteParameter("@uNavnParam", SqlDbType.Text) { Value = uNavn }; 
    var bNavnParam = new SQLiteParameter("@bNavnParam", SqlDbType.Text) { Value = bNavn };
    var passwdParam = new SQLiteParameter("@passwdParam", SqlDbType.Text) {Value = passwd};
    var pc_idParam = new SQLiteParameter("@pc_idParam", SqlDbType.TinyInt) { Value = pc_id };
    var noterParam = new SQLiteParameter("@noterParam", SqlDbType.Text) { Value = noter };
    var licens_idParam = new SQLiteParameter("@licens_idParam", SqlDbType.TinyInt) { Value = licens_id };

    var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
    "VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam')", conn);
    insertSQL.Parameters.Add(uNavnParam); //replace paramenter with verified userinput
    insertSQL.Parameters.Add(bNavnParam);
    insertSQL.Parameters.Add(passwdParam);
    insertSQL.Parameters.Add(pc_idParam);
    insertSQL.Parameters.Add(noterParam);
    insertSQL.Parameters.Add(licens_idParam);
    insertSQL.ExecuteNonQuery(); //Execute query

    //Close connection
    DBConnection.CloseDB(conn);

    //Let the user know that it was changed succesfully
    this.Text = "Succes! Changed!";
}
catch(SQLiteException e)
{
    //Catch error
    MessageBox.Show(e.ToString(), "ALARM");
}

It executes perfectly, but when I view my "brugere" table, it has inserted the values: '@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam' literally. Instead of replacing them.

I have tried making a breakpoint and checked the parameters, they do have the correct assigned values. So that is not the issue either.

I have been tinkering with this a lot now, with no luck, can anyone help?

Oh and for reference, here is the OpenDB method from the DBConnection class:

public static SQLiteConnection OpenDB()
{
    try
    {
        //Gets connectionstring from app.config
        const string myConnectString = "data source=data;";

        var conn = new SQLiteConnection(myConnectString);
        conn.Open();
        return conn;
    }

    catch (SQLiteException e)
    {
        MessageBox.Show(e.ToString(), "ALARM");
        return null;
    }
}

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

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

发布评论

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

评论(4

何时共饮酒 2024-07-24 18:42:38

您应该删除 INSERT 语句中参数名称周围的引号。

所以而不是

VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam',
        '@noterParam', '@licens_idParam')

使用

VALUES (@uNameParam, @bNavnParam, @passwdParam, @pc_idParam,
        @noterParam, @licens_idParam)

You should remove the quotes around your parameter names in the INSERT statement.

So instead of

VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam',
        '@noterParam', '@licens_idParam')

use

VALUES (@uNameParam, @bNavnParam, @passwdParam, @pc_idParam,
        @noterParam, @licens_idParam)
红颜悴 2024-07-24 18:42:38

感谢 rwwilden 和 Jorge Villuendas,答案是:

var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
" VALUES (@uNavnParam, @bNavnParam, @passwdParam, @pc_idParam, @noterParam, @licens_idParam)", conn);
insertSQL.Parameters.AddWithValue("@uNavnParam", uNavn);
insertSQL.Parameters.AddWithValue("@bNavnParam", bNavn);
insertSQL.Parameters.AddWithValue("@passwdParam", passwd);
insertSQL.Parameters.AddWithValue("@pc_idParam", pc_id);
insertSQL.Parameters.AddWithValue("@noterParam", noter);
insertSQL.Parameters.AddWithValue("@licens_idParam", licens_id);

insertSQL.ExecuteNonQuery(); //Execute query

Thanks to rwwilden and Jorge Villuendas, the answer is:

var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
" VALUES (@uNavnParam, @bNavnParam, @passwdParam, @pc_idParam, @noterParam, @licens_idParam)", conn);
insertSQL.Parameters.AddWithValue("@uNavnParam", uNavn);
insertSQL.Parameters.AddWithValue("@bNavnParam", bNavn);
insertSQL.Parameters.AddWithValue("@passwdParam", passwd);
insertSQL.Parameters.AddWithValue("@pc_idParam", pc_id);
insertSQL.Parameters.AddWithValue("@noterParam", noter);
insertSQL.Parameters.AddWithValue("@licens_idParam", licens_id);

insertSQL.ExecuteNonQuery(); //Execute query
只是偏爱你 2024-07-24 18:42:38

当您使用 System.Data.SqlClient 时,您可以通过 System.Data.SqlDbType 枚举提供参数类型。

但是如果您使用System.Data.SQLite,那么您必须使用**System.Data.DbType**枚举。

When you use System.Data.SqlClient then you provide parameter types from System.Data.SqlDbType enumeration.

But if you use System.Data.SQLite then you have to use **System.Data.DbType** enumeration.

南风起 2024-07-24 18:42:38

代替

值('@uNameParam','@bNavnParam',
'@passwdParam', '@pc_idParam',
'@noterParam', '@licens_idParam')

值 (?, ?, ?, ?, ?, ?)

replace

VALUES ('@uNameParam', '@bNavnParam',
'@passwdParam', '@pc_idParam',
'@noterParam', '@licens_idParam')

with

VALUES (?, ?, ?, ?, ?, ?)

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