C# - Create SQL Server table programmatically

前端 未结 7 2350
温柔的废话
温柔的废话 2020-12-15 06:37

I am trying to create a SQL Server table programmatically. Here is the code.

using (SqlConnection con = new SqlConnection(conStr))
{

    try
    {
        /         


        
相关标签:
7条回答
  • 2020-12-15 07:21

    If you don't like remembering SQL syntax, using Mig# you can simply:

    var schema = new DbSchema(ConnectionString, DbPlatform.SqlServer2014);
    schema.Alter(db => db.CreateTable("Customer")
         .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
         .WithNotNullableColumn("First_Name", DbType.String).OfSize(50)
         .WithNotNullableColumn("Last_Name", DbType.String).OfSize(50)
         ...);
    

    If you are not sure if it already exists, call DropIfExists before:

    db.Tables["Customers"].DropIfExists();
    
    0 讨论(0)
提交回复
热议问题