Right syntax of SqlParameter

后端 未结 1 1832
离开以前
离开以前 2021-01-25 06:05

I\'m trying to convert :

command.Parameters.Add (new SqliteParameter (DbType.Int32) { Value = id });

To a normal SqlParameter :



        
1条回答
  •  暖寄归人
    2021-01-25 06:07

    I think you are looking something this;

    command.Parameters.Add(new SqlParameter("@YourParameterName", SqlDbType.Int32).Value = id;
    

    After edit your question;

    You can use AddWithValue method. Add(String, Object) method is obsolete. like;

    command.CommandText = "Select * from tblUsers WHERE UserID = @id";
    command.Parameters.AddWithValue("@id", id);
    

    If you use SQL Server as a database, you should named your parameter add it to AddWithValue with the same name.

    When you use OleDbCommand, order of parameter is important. Because as MSDN says:

    The OLE DB .NET Provider does not support named parameters for passing parameters to an SQL statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

    SELECT * FROM Customers WHERE CustomerID = ?
    

    Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter in the command text.

    0 讨论(0)
提交回复
热议问题