2011-05-15 76 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); 
     }