Quantcast
Channel: MySQL Forums - Connector/NET and C#, Mono, .Net
Viewing all articles
Browse latest Browse all 1447

Formatting Query in .Net (9 replies)

$
0
0
I'm trying to execute an insert query where I do a select and select the records where userId is in the userIds I pass in.

This is the error:
Truncated incorrect DOUBLE value: '1,2'"

I get the error because in my code, the query comes out looking like this:
INSERT INTO Table1(DisciplineId, UserId) (SELECT 1, UserId FROM Users WHERE UserId IN ('1,2'))

I need to know how to get the query to be formatted without the single quotes around the 1,2. How can I change my code to get that result?


My code:

long recordsAffected = 0; 

String connectionString = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connectionString); 
String sql = "INSERT INTO facilitators (DisciplineId, UserId)" 
    + " (SELECT ?disciplineId, UserId FROM Users" 
    + " WHERE UserId IN (?facilitators))"; 

try 
{ 
    using (conn) 
    { 
        conn.Open(); 
        using (MySqlCommand cmd = new MySqlCommand(sql, conn)) 
        { 
            cmd.CommandType = System.Data.CommandType.Text; 
            cmd.Parameters.AddWithValue("?disciplineId", disciplineId); 
            cmd.Parameters["?disciplineId"].Direction = System.Data.ParameterDirection.Input;
            cmd.Parameters.AddWithValue("?facilitators", facilitators); 
            cmd.Parameters["?facilitators"].Direction = System.Data.ParameterDirection.Input;
            recordsAffected = cmd.ExecuteNonQuery(); 
        } 
    } 

    return recordsAffected; 
}

Viewing all articles
Browse latest Browse all 1447

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>