开发者

oledb param type question

I am trying to select the text from a text box and pass it in as one of the parameters of an oledb command but this error message occurs;

"The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects"

Here is my code:

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();

Any other points that开发者_如何学Go you see would be appreciated this is my first piece of work using a database in c#.

Note i have a controlbox array of 9 textboxes that all have to be populated in order for this section of code to be executed.

Thanks


Basically you're adding a string object in a method which expects an OleDBParameter object.

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

You perhaps want to do something like

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

Here each textbox should be named the same as parameters specified in the original query.


Can Use From it:

      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("@price",txtPrice.Text);


Use this, inside your for loop.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜