Greg McNulty
Greg McNulty

Reputation: 1466

How do I execute an INSERT using SqlCommand?

I have another class that creates the connection (see refined class half way down page) and I believe it connects or at lest I do not hit any of the exceptions in run time.

I would now like to insert data into my database but nothing is updated. I am trying to follow this example but it is for reading.

I did run "querystring" on the show SQL Pane and it does execute correctly. It inserts data properly as a new row.

not sure what to do after my last line? Thanks.

string queryString =
"INSERT INTO UserData UserProfileID, ConfidenceLevel, LoveLevel, HappinessLevel) VALUES ('a051fc1b-4f51-485b-a07d-0f378528974e', 2, 2, 2);";

    protected void Button1_Click(object sender, EventArgs e)
    {
        MySqlConnection database = new MySqlConnection();
        database.CreateConn();

        Command = new SqlCommand(queryString, database.Connection);  //

    }

Upvotes: 0

Views: 1422

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

 Command.ExecuteNonQuery();

By the way, you're missing a parenthesis in your SQL query string.

And of course, you'd use parameters in a real query (and you will not use string concatenation instead of passing parameters as it'll open doors for destructive SQL injection attacks).

Upvotes: 4

Related Questions