Hi i was using stored procedure in SQL Server to pass parameters to the query , but now I'm changing my database to ms access and it's my first time to deal with.
how can i pass byte[] to sql query ? bacause i got this error Syntax error (missing operator) in query expression 'System.Byte[]'.
this is my code
public static int EditWhois(object ID,object Image, object Ranswer, object Fanswer1, object Fanswer2, object Fanswer3)
{
int result = 0;
String sql = "UPDATE Whois SET [Image]="+@Image+", Ranswer=" + Ranswer + ", Fanswer1=" + Fanswer1 + ",Fanswer2=" + Fanswer2 + ",Fanswer3=" + Fanswer3 + " WHERE ID=" + ID;
System.Windows.Forms.MessageBox.Show(sql);
cmd = new OleDbCommand(sql, con);
//cmd.Parameters.AddWithValue("@ID", ID);
//cmd.Parameters.AddWithV开发者_开发问答alue("@Image", Image);
//cmd.Parameters.AddWithValue("@Ranswer", Ranswer);
//cmd.Parameters.AddWithValue("@Fanswer1", Fanswer1);
//cmd.Parameters.AddWithValue("@Fanswer2", Fanswer2);
//cmd.Parameters.AddWithValue("@Fanswer3", Fanswer3);
if (con.State != ConnectionState.Open)
{
con.Open();
result = cmd.ExecuteNonQuery();
con.Close();
}
return result;
}
Use @ parameter substitution. Also as @BoltClock says, change you method signature.
public static int EditWhois(object ID,object Image, object Ranswer,
object Fanswer1, object Fanswer2, object Fanswer3)
{
int result = 0;
String sql = "UPDATE Whois SET [Image]=@Image, Ranswer=@Ranswer, " +
"Fanswer1=@Fanswer1, Fanswer2=@Fanswer2, Fanswer3=@Fanswer3 " +
"WHERE ID=@ID";
cmd = new OleDbCommand(sql, con);
cmd.Parameters.AddWithValue("@ID", ID);
cmd.Parameters.AddWithValue("@Image", Image);
cmd.Parameters.AddWithValue("@Ranswer", Ranswer);
cmd.Parameters.AddWithValue("@Fanswer1", Fanswer1);
cmd.Parameters.AddWithValue("@Fanswer2", Fanswer2);
cmd.Parameters.AddWithValue("@Fanswer3", Fanswer3);
if (con.State != ConnectionState.Open)
{
con.Open();
result = cmd.ExecuteNonQuery();
con.Close();
}
return result;
}
result is still 0; i know the problem where it was . if the connection is closed the query will be executed successfully , but if it is opend it will not be executed due to this condition .
if (con.State != ConnectionState.Open)
{
con.Open();
result = cmd.ExecuteNonQuery();
con.Close();
}
it must be
if (con.State != ConnectionState.Open)
{
con.Open();
}
result = cmd.ExecuteNonQuery();
con.Close();
thanks all .
精彩评论