Insert sentence to DB
I'm working on ASP- and I have build in DB and I want to know how to write a code for insert sentences. on C#?开发者_JS百科??
from your comment . You are using entity framework. and the table called TblConstants.
DataClasses1DataContext db = new DataClasses1DataContext();
TblConstant tb = new TblConstant();
tb.Field1 = Value1;
tb.Field2 = Value2;
...
db.AddToTblConstants(tb);
db.SaveChanges();
I presume you are working with ASP.NET, if so you can use ADO.NET
Still something is missing. Whether do you want to insert ACCESS / SQL / MySQL database? If you are looking for Access then this (http://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx) will really help you.
For MS-SQL database, you can do like following:
using (SqlConnection con = new SqlConnection("your connection string"))
{
con.Open();
SqlCommand cmd = new SqlCommand("your insert query");
cmd.ExecuteNonQuery();
}
C# can't directly insert records into a database. Ultimately you need to execute SQL on the database (as per http://msdn.microsoft.com/en-us/library/ms174335.aspx). There are various C# frameworks and wrappers that enable execution of SQL statements in a database, at different levels of abstraction.
Most directly you can use ADO.NET which allows you to open connections to a database and execute SQL directly.
You could use LINQ to SQL or Entity Framework. These allow you work with a database as if it was a collection of C# objects. However you first need to set up mappings from you database schema to the C# classes you wish to use. LINQ to SQL supports direct 1-1 mappings between tables and classes, Entity Framework supports much more complex mappings.
You could use non-Microsoft frameworks such as N-hibernate. This has (as far as I know) a similar feature set to Entity Framework.
精彩评论