Perform insert into C # List [closed]

0

Can one give an example of how an sql insert would be in a list in C #?

    
asked by anonymous 28.07.2016 / 03:00

2 answers

3

How to store a List in a database?

In this question you can see some examples of how to insert with C #, in Gypsy's answer only EnityFramework is used. In Lucas's response, it uses Dapper and EntityFramework

But to do insert used only SQL commands, you have to use SqlConnection and SqlCommand , getting something like that.

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();
}
    
28.07.2016 / 03:23
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.

    
28.07.2016 / 15:37