Perform Insert into in c#List

Asked

Viewed 216 times

0

One could give an example of what an sql Insert would look like in a list in c#?

2 answers

3

How to store a List<Object> in a database?

in this question you can see some examples of how to do Insert with c#, in the Gypsy answer is used only EnityFramework. In Lucas' answer, he uses Dapper and EntityFramework

But to make Insert used only SQL commands, you have to use the SqlConnection and SqlCommand , getting something like.

using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Banco;Integrated Security=True;MultipleActiveResultSets=True"))
{
    SqlCommand command = new SqlCommand("INSERT INTO [dbo].[AspNetRoles] ([Id],[Name]) VALUES (@Id, @Name)", conn);

    command.Parameters.Add("@Id", SqlDbType.UniqueIdentifier).Value = Guid.NewGuid();
    command.Parameters.Add("@Name", SqlDbType.NVarChar, 256).Value = "Nome";

    conn.Open();
    command.ExecuteNonQuery();
}

0

        tb_User tb = new  tb_User ();
        tb.Nome="Manuel";
        tb.Idade=30;

        DB.DB.AddTotb_User(tb);
        DB.DB.Connection.Open();
        DB.DB.SaveChanges();
        DB.DB.Connection.Close();

Entity Framework.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.