linq 数据库已存在,直接添加数据
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace ConsoleApplication1388
{
class Program
{
static void Main(string[] args)
{
string sourceString = "Server=SMOOTHFU;Database=mydb;Integrated Security=SSPI";
CreateDB newDB = new CreateDB(sourceString);
if(newDB.DatabaseExists())
{
Console.WriteLine("数据库已经存在!\n");
}
else
{
//创建数据库
newDB.CreateDatabase();
}
Student stu = new Student
{
id = 11,
name = "ZFF"
};
newDB.tb2.InsertOnSubmit(stu);
newDB.SubmitChanges();
Console.ReadLine();
}
}
[Table(Name="mt1")]
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)
{
}
}