c# - MySQL causing fatal error after executing query with parameters -




i creating method execute query 1 or more parameters.

code:

public static ienumerable<t> executequerywithparameters<t>(string query, string connectionstring, string[] parametersarray, string[] valuesarray) {     using (mysqlconnection connection= new mysqlconnection(connectionstring))     {         using (var cmd = new mysqlcommand(query, connection))         {             (int = 0; < parametersarray.length; i++)             {                 cmd.parameters.addwithvalue(parametersarray[i], valuesarray[i]);             }              connection.open();             return connection.query<t>(query);         }     } } 

tempsave table structure:

+------------------------------+ |id|localization|items|quantity| +------------------------------+ 

how works:

when called, method receive 4 parameters:

  1. a query (example: select * tempsave localization = @localization)
  2. a connection string (example: server=localhost; database=wintestbeta;user=root; password=;)
  3. a string array contains 1 or more parameters (they this: @parameter1)
  4. a string array contains 1 ore more values

then create new variable of type mysqlconnection called connection 1 parameter, connectionstring.

after create variable type mysqlcommand called cmd 2 parameters, 1 query , other connection.

then enter loop size of parametersarray length, inside loop add new parameter cmd 2 parameters, parameterarray index equal i, , valuearray index equal i.

then open connection , execute query, receives query return.

code used debug:

ienumerable<tempsave> _checktemp;  string check_tempsave = "select * tempsave localization=@localization";  string[] parameters = new string[] { "@localization" }; string[] values = new string[] { _save.localization };  _checktemp = sql.executequerywithparameters<tempsave>(check_tempsave, sql.connectionstring, parameters, values); 

problem:

when executing code when reaches

return connection.query<t>(query); 

and causes following error:

fatal error encountered during command execution.

when debugging checked , both parameters , values have 1 value is:

@localization counter,1 

the solution found instead of using addwithvalue using loop , replacing every @parameter value letting method this:

    public static ienumerable<t> executequerywithparameters<t>(string query, string connectionstring, string[] parametersarray, string[] valuesarray)     {         using (mysqlconnection connection = new mysqlconnection(connectionstring))         {                 (int = 0; < parametersarray.length; i++)                 {                     //replace @parameters                     query = query.replace(parametersarray[i], valuesarray[i]);                 }                 connection.open();                 return connection.query<t>(query);         }     } 

maybe isn't best method no 1 answering question , solution come with.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -