1

I want to scan a database and then create a table using DataReader if it does not already exist. I found a block of code that is used to create a table, but it is in VB, which is not ideal as the rest of my code is C#. Regardless, I can't get it to work either way.

This is my code, thanks for taking a look:

var dif = new DataInterfaceFactory(DatabaseTypes.SqlServer, " DATABASE_NAME", "[SERVER_NAME]");

            using (DataReader dr = dif.GetDataReader())
            {
                exists = m_api.Database.ExecuteCommand(@"IF EXISTS(SELECT COUNT(1) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'TABLE_NAME')");

                while (dr.Read())
                {
                    if (exists == 0)
                    {
                        TableBuilder calculationSchedule = new TableBuilder("TABLE_NAME", dif.DatabaseType);
                        calculationSchedule.AddField("TABLE_NAME_UID", DbFieldType.int_, 0, false, null, true, null);
                        calculationSchedule.AddField("SERVER_NAME", DbFieldType.nvarchar_);
                        calculationSchedule.AddField("DATABASE_NAME", DbFieldType.nvarchar_);
                        calculationSchedule.AddField("CHECK_DATE", DbFieldType.datetime_);
                        calculationSchedule.AddField("IS_RUNNING", DbFieldType.int_);

                        using (CommandExecutor cex = dif.GetCommandExecutor())
                        {
                            calculationSchedule.BuildTable(cex);
                        }
                    }
               }
           }
Joe Joe Joe
  • 79
  • 1
  • 15

3 Answers3

2

you can use SQL Servers Information Schema in order to identify whether the table(s) exists on the RDBMS or not.

select count(1) from INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'mySchema' and TABLE_NAME = 'myTable'

if the result is 0, it means the table does not exists, if the result is 1, the table exists under the schema.

now, you can use datareader to query your database and check whether your table exists or not.

then you can issue a create command to create your Table


this creates table if it does not exists

BEGIN
    if not exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'CALCULATION_SCHEDULE')
        create table CALCULATION_SCHEDULE (
            Name varchar(64) not null
            ...
        )
END;
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

Here is the solution to my problem:

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_SCHEMA = 'dbo' AND TABLE_NAME = 'CALCULATION_SCHEDULE'");

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

            CommandExecutor CE = dif.GetCommandExecutor();
            try
            {
                if (exists == 0)
                {
                    string sql = "CREATE TABLE dbo.CALCULATION_SCHEDULE(CALCULATION_SCHEDULE_UID INT,SERVER_NAME char(25),DATATABLE_NAME char(20),LAST_CHECK_DATE DATE,IS_RUNNING INT)";
                    CE.ExecuteNonQuery(sql);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: could not create CALCULATION_SCHEDULE");
            }
         }
Joe Joe Joe
  • 79
  • 1
  • 15
-1

Try using a datatable :

            DataTable calculationSchedule = new DataTable();
            calculationSchedule.Columns.Add("CALCULATION_SCHEDULE_UID", typeof(int));
            calculationSchedule.Columns.Add("SERVER_NAME", typeof(string));
            calculationSchedule.Columns.Add("DATABASE_NAME", typeof(string));
            calculationSchedule.Columns.Add("LAST_CHECK_DATE", typeof(DateTime));
            calculationSchedule.Columns.Add("IS_RUNNING", typeof(int));
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thanks, just need to convert the following code to C# now: using (CommandExecutor cex = dif.GetCommandExecutor()) { calculationSchedule.BuildTable(cex); }. It will make more sense having it all in C# but it still won't solve my problem though. – Joe Joe Joe Jan 24 '19 at 11:30