I want to return my sql query with parameters assigned, but I can't. Can you explain how I can return the parameters as defined?
public string InsertText(string Ad, string Soyad, string OkulNo, int RolId, string TelNo)
{
string query = "INSERT INTO kutuphane.kisiler (Adi,Soyadi,No,RolId,TelNo) VALUES (@Adi,@Soyadi,@No,@RolId,@TelNo)";
MySqlCommand command = new MySqlCommand(query);
command.Parameters.Add("@Adi", MySqlDbType.VarChar).Value = Ad;
command.Parameters.Add("@Soyadi", MySqlDbType.VarChar).Value = Soyad;
command.Parameters.Add("@OkulNo", MySqlDbType.VarChar).Value = OkulNo;
command.Parameters.Add("@RolId", MySqlDbType.Int32).Value = RolId;
command.Parameters.Add("@TelNo", MySqlDbType.VarChar).Value = TelNo;
return command.CommandText;
}
CodePudding user response:
You cannot. A parameterized query is more than just a string
. If you are expecting to see the parameters replaced in the query: that's not what parameters are. I'd probably just return command
here, and have the caller execute it (after setting the connection). Or just execute it inside the method and never have the query leave.
CodePudding user response:
I'm not sure about my answer... but have you tried to do this?
public MySqlCommand InsertText(string Ad, string Soyad, string OkulNo, int RolId, string TelNo)
{
string query = "INSERT INTO kutuphane.kisiler (Adi,Soyadi,No,RolId,TelNo) VALUES (@Adi,@Soyadi,@No,@RolId,@TelNo)";
MySqlCommand command = new MySqlCommand(query);
command.Parameters.Add("@Adi", MySqlDbType.VarChar).Value = Ad;
command.Parameters.Add("@Soyadi", MySqlDbType.VarChar).Value = Soyad;
command.Parameters.Add("@OkulNo", MySqlDbType.VarChar).Value = OkulNo;
command.Parameters.Add("@RolId", MySqlDbType.Int32).Value = RolId;
command.Parameters.Add("@TelNo", MySqlDbType.VarChar).Value = TelNo;
return command;
}
You can have MySqlCommand return directly instead of the string. Remember that later you have to print the query in string format, as you did previously