创建数据库
testContext ctx = new testContext("server=xxx;database=testdb;uid=xxx;pwd=xxx"); ctx.CreateDatabase();
[Table(Name = "test")] public class test { [Column(IsPrimaryKey = true, IsDbGenerated = true)] public int ID { get; set; }
[Column(DbType="varchar(20)")] public string Name { get; set; } }
public partial class testContext : DataContext { public Table<test> test; public testContext(string connection) : base(connection) { } }
|
|
testContext ctx = new testContext("server=xxx;database=testdb;uid=xxx;pwd=xxx");
ctx.CreateDatabase();
[Table(Name = "test")]
public class test
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID { get; set; }
[Column(DbType="varchar(20)")]
public string Name { get; set; }
}
public partial class testContext : DataContext
{
public Table<test> test;
public testContext(string connection) : base(connection) { }
}
|
| | |
这段代码在数据库中创建了名为testdb的数据库,等同于下面的脚本:
CREATE TABLE [dbo].[test]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL, CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
|
同时,DataContext还提供了DeleteDatabase()方法,在这里就不列举了。