Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difficulty with Connector/NET and prepared statements

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
0
0
US
Hello,

I'm trying to executed sequence of INSERTs using a prepared command.

The code:
Code:
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = myConn;
cmd.CommandText = "INSERT INTO someTable VALUES (@param)";
cmd.Prepare();
cmd.Parameters.Add("@param", MySqlDbType.Int16);


for (...)
{
  cmd.Parameters["@param"].Value = i;
  cmd.ExecuteNonQuery(); 
}

The underlying column name is called userid, and I get back "column userid cannot be null". I've checked that there is a value for "i" at the time of the .Value statement. Any ideas?

Thanks
 
The mysql connector manual examples shows parameters as being identifed by ? rather than @ (as used in SQLServer)
The snippet from the help file is
Code:
private void PrepareExample()
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO mytable VALUES (?val)", myConnection);
cmd.Parameters.Add( "?val", 10 );
cmd.Prepare();
cmd.ExecuteNonQuery();

cmd.Parameters[0].Value = 20;
cmd.ExecuteNonQuery();
}
Just as a style note it usual to name the columns e.g. insert into sometable (userid) values (?val).
Also does the table have only one column, I recall sonething with anonymous inserts when you have things like aito increment columns (e.g. id fields)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top