SQL Insert Query Using C#

后端 未结 8 1717
长情又很酷
长情又很酷 2020-11-27 17:24

I\'m having an issue at the moment which I am trying to fix. I just tried to access a database and insert some values with the help of C#

The things I tried (worked)

8条回答
  •  执念已碎
    2020-11-27 18:13

    Try

    String query = "INSERT INTO dbo.SMS_PW (id,username,password,email) VALUES (@id,@username, @password, @email)";
    using(SqlConnection connection = new SqlConnection(connectionString))
    using(SqlCommand command = new SqlCommand(query, connection))
    {
        //a shorter syntax to adding parameters
        command.Parameters.Add("@id", SqlDbType.NChar).Value = "abc";
    
        command.Parameters.Add("@username", SqlDbType.NChar).Value = "abc";
    
        //a longer syntax for adding parameters
        command.Parameters.Add("@password", SqlDbType.NChar).Value = "abc";
    
        command.Parameters.Add("@email", SqlDbType.NChar).Value = "abc";
    
        //make sure you open and close(after executing) the connection
        connection.Open();
        command.ExecuteNonQuery();
    }
    

提交回复
热议问题