J.Goalater
J.Goalater

Reputation: 1

C# SQL Update Current Insert Method Parameters.AddWithValue

I wrote an SQL insert method in C # and would like to convert it, so that the rubrics (username, victories, defeats) can be changed with @ and Parameters.AddWithValue. How can I solve this problem

SqlConnection con = new SqlConnection(@"Data Source=******;Initial Catalog=UserDB;Integrated Security=True");

SqlCommand cmdinsert = new SqlCommand("Insert UserTabelle values('" + 0 + "','" + 0 + "','" + txtBenutzerName.Text + "')", con);
con.Open();                    
cmdinsert.CommandType = CommandType.Text;

cmdinsert.ExecuteNonQuery();
con.Close();

MessageBox.Show("Account erfolgreich erstellt");

Login login = new Login(txtBenutzerName.Text);
login.Show();
this.Close();

Upvotes: 0

Views: 138

Answers (1)

Sergey L
Sergey L

Reputation: 1492

Use the following code as an example.

            using (var connection =
                new SqlConnection(
                    @"Data Source=******;Initial Catalog=UserDB;Integrated Security=True"))
            {
                connection.Open();
                using (var command =
                        new SqlCommand(
                            @"
insert into UserTabelle([Name], [Other])
values (@name, @other)",
                            connection)){
                    command.Parameters.AddWithValue("@name", txtBenutzerName.Text);
                    command.Parameters.AddWithValue("@other", "some value");
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }

Upvotes: 1

Related Questions