System.Data.SQLite parameter issue

你离开我真会死。 提交于 2019-11-30 06:59:25

问题


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;
    }
}

回答1:


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)



回答2:


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



回答3:


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.




回答4:


replace

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

with

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



来源:https://stackoverflow.com/questions/751172/system-data-sqlite-parameter-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!