如何以编程方式创建表 [英] How to create a table programically
问题描述
我需要通过以编程方式读取现有数据库来创建重复数据库及其表和约束.
我已经以编程方式创建了数据库.但是我需要按特定顺序创建这些表.在我现有的数据库中,有很多相关的表.因此,我需要先创建父表,然后再创建其子表.
假设数据库DB1中有2个表"Table1"和"Table2".
假设Table2是Table1的父级.那就是使用外键的Table1,外键是Table2的主键.
因此,我需要首先创建Table2.但是当我使用
时
Hi,
I need to create a duplicate database and its tables and constraints by reading from an existing database programmatically.
I have created the database programmatically. But I need to create those tables in a particular order. In my existing database there are lots of related tables.So I need to create parent table first, then its child tables.
Suppose 2 tables "Table1" and "Table2" in database DB1.
Suppose Table2 is the parent of Table1. That is Table1 using a foreign key which is the primary key of Table2.
So I need to create Table2 first. But while I am using
Database objDatabase = objServer.Databases["DB2"];
然后遍历objDatabase.tables
我得到了Table1,Table2的订单
这是我的问题.请帮帮我
预先感谢.
And looping through objDatabase.tables
I am getting the order Table1, Table2
This is my problem. Please help me
Thanks in advance.
推荐答案
hi
试试这个
创建表之前,请先进行如下验证:
hi
try this
before creating table validate like this
string validate="SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE=''BASE TABLE'' AND TABLE_NAME=''" + tablename + "_GroupMaster''";
after validate like this to create
string s= "create table " + tablename + "_GroupMaster(Slno int identity(1,1),GroupId Char(7) null,GroupName varchar(150) null,Status Char(1) null,MasterId notnull)";
这篇关于如何以编程方式创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!