简单的SQL命令不适用于特定的参数

时间:2020-12-07 01:50:38

I have a simple SQL command that wont work in certain giving parameters.
For example if TweetID = 59 and UserID = 1 it will return the value;
but if TweetID = 8 and UserID = 1 it will not return the value.
Can some one find the reason for that?

我有一个简单的SQL命令,在某些给定的参数中不起作用。例如,如果TweetID = 59且UserID = 1,它将返回该值;但如果TweetID = 8且UserID = 1,则不会返回该值。有人可以找到原因吗?

public static int GetReTweetIdFromReTweetByUserIdAndTweetId(int TweetID,int UserID)
{
    string sql = "SELECT [ReTweetID] FROM [ReTweet] WHERE [TweetID] = [@TID] AND [UserID] = [@UID]";
    OleDbConnection conn = ConnectToDb();
    OleDbCommand com = new OleDbCommand(sql, conn);
    com.Parameters.Clear();
    OleDbParameter objParamater;
    objParamater = com.Parameters.Add("[@UID]", OleDbType.Integer);
    objParamater.Direction = ParameterDirection.Input;
    objParamater.Value = UserID;
    objParamater = com.Parameters.Add("[@TID]", OleDbType.Integer);
    objParamater.Direction = ParameterDirection.Input;
    objParamater.Value = TweetID;
    OleDbDataAdapter da = new OleDbDataAdapter(com);
    DataTable dt = new DataTable();
    int id=0;
    try
    {
        conn.Open();
        da.Fill(dt);
        id = int.Parse(dt.Rows[0][0].ToString());
    }
    catch (Exception err)
    {
        throw err;
    }
    finally
    {
        da.Dispose();
        dt.Dispose();
        com.Dispose();
        conn.Close();
        conn.Dispose();
    }
    return id;
}

简单的SQL命令不适用于特定的参数

1 个解决方案

#1


4  

When using OleDb with an Access database, parameter names are ignored. You must supply the parameter values in the order Access expects. If you run this query from the Access query designer ...

将OleDb与Access数据库一起使用时,将忽略参数名称。您必须按照Access期望的顺序提供参数值。如果从Access查询设计器运行此查询...

SELECT [ReTweetID] FROM [ReTweet] WHERE [TweetID] = [@TID] AND [UserID] = [@UID]

... you will see that Access asks you to supply a value for [@TID] first and then a value for [@UID].

...您将看到Access首先要求您为[@TID]提供值,然后为[@UID]提供值。

But in your c# code, you're supplying the parameter values in the opposite order.

但是在您的c#代码中,您将以相反的顺序提供参数值。

However there may be something else which confuses the issue because I don't see why it succeeded with TweetID = 59 and UserID = 1. That would only make sense to me if the table actually includes a row with TweetID = 1 and UserID = 59. And perhaps it does. But either way, I urge you to first supply parameter values in the order Access expects, and then see whether you need to make additional code changes.

然而,可能还有其他东西会混淆这个问题,因为我不明白为什么它成功使用TweetID = 59和UserID = 1.这只会对我有意义,如果表实际上包含一个TweetID = 1且UserID = 59的行也许确实如此。但无论如何,我建议您首先按照Access期望的顺序提供参数值,然后查看是否需要进行其他代码更改。

#1


4  

When using OleDb with an Access database, parameter names are ignored. You must supply the parameter values in the order Access expects. If you run this query from the Access query designer ...

将OleDb与Access数据库一起使用时,将忽略参数名称。您必须按照Access期望的顺序提供参数值。如果从Access查询设计器运行此查询...

SELECT [ReTweetID] FROM [ReTweet] WHERE [TweetID] = [@TID] AND [UserID] = [@UID]

... you will see that Access asks you to supply a value for [@TID] first and then a value for [@UID].

...您将看到Access首先要求您为[@TID]提供值,然后为[@UID]提供值。

But in your c# code, you're supplying the parameter values in the opposite order.

但是在您的c#代码中,您将以相反的顺序提供参数值。

However there may be something else which confuses the issue because I don't see why it succeeded with TweetID = 59 and UserID = 1. That would only make sense to me if the table actually includes a row with TweetID = 1 and UserID = 59. And perhaps it does. But either way, I urge you to first supply parameter values in the order Access expects, and then see whether you need to make additional code changes.

然而,可能还有其他东西会混淆这个问题,因为我不明白为什么它成功使用TweetID = 59和UserID = 1.这只会对我有意义,如果表实际上包含一个TweetID = 1且UserID = 59的行也许确实如此。但无论如何,我建议您首先按照Access期望的顺序提供参数值,然后查看是否需要进行其他代码更改。