2016-12-25 53 views
1

返回錯誤:「沒有給出一個或多個必需參數的值。」訪問C#簡單選擇

「沒有給出一個或多個必需參數的值」。

字符串數組傳遞給功能:ID的

String[,] arrParams = new String[1, 2] { 
         {"@ToUpper_user_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)) 
{ 
    ... 
} 

功能調用,從數據庫獲取數據:

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所有的作品與參數只是選擇代碼是給我的錯誤)

+1

你真的有一個專門命名爲ToUpper_user_id的列嗎? – Steve

+0

修復此問題後,您將來會看到「數據類型不匹配」錯誤。 – Plutonix

回答

0

在「get_dbdata(......)」上面,我應該有:

OleDbDataAdapter adapter = new OleDbDataAdapter(command); 

代替:

OleDbDataAdapter adapter = new OleDbDataAdapter(strSQL, connection); 

在代碼中,兩個SQL和參數已經加入到上述命令。

現在工作!

相關問題