本文介绍了SqlParameter的正确语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要转换:

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

正常的SqlParameter:

To a normal SqlParameter :

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

我已经设法转换每一行现在除了这一个,我得到这些错误: / p>

I've managed to convert every line now besides this one, I'm getting these errors :

Error   3   Argument 1: cannot convert from 'System.Data.DbType' to 'object[]'
Error   2   The best overloaded method match for 'System.Data.SqlClient.SqlParameter.SqlParameter(object[])' has some invalid arguments

完整的函数代码:

public User GetUser(int id)
{
    var u = new User();
    lock (locker)
    {
        connection = new SqlConnection("Data Source=" + path + ";Initial Catalog=DB;User ID=sa;Password=***********th");
        connection.Open();
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "Select * from tblUsers WHERE UserID = ?";
            command.Parameters.Add(new SqlParameter(DbType.Int32) { Value = id });
            var r = command.ExecuteReader();
            while (r.Read())
            {
                u = FromReader(r);
                break;
            }
        }
        connection.Close();
    }
    return u;
}


推荐答案

this;

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

编辑您的问题后;

使用方法。已过时。

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

如果你使用SQL Server作为数据库,你应该命名你的参数将它添加到 AddWithValue 使用相同的名称。

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:



SELECT * FROM Customers WHERE CustomerID = ?



这篇关于SqlParameter的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 03:57