如何使用C#在数据库中创建表?

时间:2019-01-25 10:23:36

标签: c# sql-server database datatable

我创建了一个放入TableBuilder对象的表。我需要将此表插入数据库,并且想知道该过程是什么。我还没有使用Google找到直接的答案。这是我的代码,谢谢:

var dif = new DataInterfaceFactory(DatabaseTypes.SqlServer, "WDM_SOFTWARE_INFO", "d2sql4.d2.wdm");

            using (DataReader dr = dif.GetDataReader())
            {
                dr.ExecuteReader("SELECT COUNT(1) AS TABLECOUNT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'CALCULATION_SCHEDULE'");

                while (dr.Read())
                {
                    var exists = Convert.ToInt32(dr["TABLECOUNT"]);

                    if (exists == 0)

                    {                                                      
                        TableBuilder calculationSchedule = new TableBuilder("CALCULATION_SCHEDULE", dif.DatabaseType);
                        calculationSchedule.AddField("CALCULATION_SCHEDULE_UID", DbFieldType.int_, 0, false, null, true, null);
                        calculationSchedule.AddField("SERVER_NAME", DbFieldType.nvarchar_);
                        calculationSchedule.AddField("DATABASE_NAME", DbFieldType.nvarchar_);
                        calculationSchedule.AddField("LAST_CHECK_DATE", DbFieldType.datetime_);
                        calculationSchedule.AddField("IS_RUNNING", DbFieldType.int_);

                        using (CommandExecutor cex = dif.GetCommandExecutor())
                        {
                            calculationSchedule.BuildTable(cex);
                        }
                    }
                }
            }

0 个答案:

没有答案