首页 > 代码库 > linq,创建数据库,插入数据,newDB.CreateDatabase();newDB.tb2.InsertOnSubmit(stu); newDB.SubmitChanges();

linq,创建数据库,插入数据,newDB.CreateDatabase();newDB.tb2.InsertOnSubmit(stu); newDB.SubmitChanges();

using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace ConsoleApplication1388
{
class Program
{
static void Main(string[] args)
{
string sourceString = "Server=SMOOTHFU;Database=mydb2;Integrated Security=SSPI";
CreateDB newDB = new CreateDB(sourceString);
//创建数据库
newDB.CreateDatabase();
Student stu = new Student
{
id = 10,
name = "ZTT"
};

newDB.tb2.InsertOnSubmit(stu);
newDB.SubmitChanges();
}
}

[Table(Name="SecondDB")]
public class Student
{
[Column(IsPrimaryKey =true,Name="id")]
public int id { get; set; }
[Column(Name="name")]
public string name { get; set; }
}

public class CreateDB:DataContext
{
public Table<Student> tb2;

public CreateDB(string connection):base(connection)
{

}

public CreateDB(System.Data.IDbConnection connection):base(connection)
{

}
}

linq,创建数据库,插入数据,newDB.CreateDatabase();newDB.tb2.InsertOnSubmit(stu); newDB.SubmitChanges();