2011-05-15 75 views
0

我想從文本框中選擇文本並將其作爲oledb命令的參數之一傳入,但會出現此錯誤消息;oledb參數類型問題

「的OleDbParameterCollection只接受非空OleDbParameter類型的對象,而不是字符串對象」

這裏是我的代碼:

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=EstateAgent.mdb;Persist Security Info=True"; 
      string sqlStatement = "INSERT INTO `house` (`ID`, `County`, `Town`, `Village`, `PropertyType`, `Bedrooms`, `Price`, `EstateAgent`, `Keyword`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

      OleDbConnection myConnection = new OleDbConnection(connectionString); 
      OleDbCommand myAccessCommand = new OleDbCommand(sqlStatement); 

      // System.Data.OleDb.OleDbParameter param; 

      myAccessCommand.Connection = myConnection; 

      for (int i = 0; i < 9; i++) 
      { 
       myAccessCommand.Parameters.Add(textBoxControlArray[i].Text); 
      } 

      myConnection.Open(); 
      myAccessCommand.ExecuteNonQuery(); 
      myConnection.Close(); 

你看到的將不勝感激這是我的第一塊任何其他點在c#中使用數據庫的工作。

請注意,我有一個9個文本框的控制箱數組,必須填寫所有這些文本框才能執行此代碼段。

感謝

回答

3

基本上你要添加在期待一個OleDBParameter對象的方法的字符串對象。

myAccessCommand.Parameters.Add(textBoxControlArray[i].Text);

,你或許需要像做

myAccessCommand.Parameters.Add(new OleDBParameter(textBoxControlArray[i].Name, textBoxControlArray[i].Text);

這裏每個文本框應該被命名一樣原始查詢指定的參數。

0

可以使用來自它:

 string sqlStatement = "INSERT INTO house (ID, County, Town, Village, PropertyType, Bedrooms, Price, EstateAgent, Keyword) VALUES (@ID, @County, @Town, @Village,@PropertyType, @Bedrooms, @Price, @EstateAgent, @Keyword)"; 

myAccessCommand.Parameters.AddWithValue( 「@價格」,txtPrice.Text);

0

在你的循環中使用這個。

 for (int i = 0; i < 9; i++) 
     { 
      OleDbParameter op = new OleDbParameter("OP", OleDbType.VarChar, 50); 
      op.Value = textBoxControlArray[i].Text; 
      myAccessCommand.Parameters.Add(op); 
     }