Can one give an example of how an sql insert would be in a list in C #?
Can one give an example of how an sql insert would be in a list in C #?
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();
}
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.