"No value given for one or more required parameters."访问 C# 简单选择



错误返回:

"一个或多个必需的参数没有给出的值。"

字符串数组传递到函数:

String[,] arrParams = new String[1, 2] {
                       {"@ToUpper_user_id", id}
                };

ID的值:

"test" (without the quotes)

sql:

strSQL = "select * from users where ToUpper_user_id = ?;";

sql函数呼叫:

if (jdb.getdb_data(strSQL, arrParams, strTableName, out dsGet, out strTechMessage))
{
    ...
}

呼叫从DB获取数据的功能:

public static bool getdb_data(String strSQL, String[,] arrParams, String strTableName, out DataSet dsGet, out String strTechMessage)
    {
        bool boolRC = true;
        String key = String.Empty;
        String val = String.Empty;
        dsGet = new DataSet();
        strTechMessage = String.Empty;
        String strSQL_Empty = String.Empty;
        string connectionString = jdb.getConnString();
        using (OleDbConnection connection =
            new OleDbConnection(connectionString))
        {
            OleDbCommand command = new OleDbCommand(strSQL, connection);
            if (arrParams.GetLength(0) > 0)
            {
                for (int i = 0; i < arrParams.GetLength(0); i++)
                {
                    for (int j = 0; j < arrParams.GetLength(1); j++)
                    {
                        if (j.Equals(0)) { key = arrParams[i, j]; }
                        if (j.Equals(1)) { val = arrParams[i, j]; }
                    }
                    command.Parameters.AddWithValue(key, val);
                }
            }
            else
            {
                boolRC = false;
                strTechMessage = "No parameters found";
            }
            // Open the connection in a try/catch block. 
            // Create and execute the DataReader, writing the result
            // set to the console window.
            if (boolRC)
            {
                try
                {
                    connection.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL, connection);
                    adapter.Fill(dsGet, strTableName);
                }
                catch (Exception ex)
                {
                    boolRC = false;
                    strTechMessage = ex.Message;
                }
            }
        }
        return boolRC;
    }

请帮助 - 我想我疯了!(更新crud都与参数一起使用……仅选择代码给我错误。)

在" get_dbdata(...)"中,我应该有:

OleDbDataAdapter adapter = new OleDbDataAdapter(command);

而不是:

OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL, connection);

在代码中,SQL和参数都已添加到上面的命令中。

现在工作!

相关内容

最新更新