2012-03-16 26 views
0

我需要一種方法來檢查,如果一個表在我的數據庫從我的C#類存在,如果該表得到了行所需。 如果它不存在或某些行缺失,我需要添加這些。檢查SQL Server表和錶行,如果空創建

我用這個方法,但不知道如何獲得丟失的功能在裏面。 (Check if a SQL table exists

我與SQL Server的工作方法和C#

+1

您給出的鏈接接受的答案顯示瞭如何檢查它是否存在。如果它不存在,請使用[SqlCommand上的ExecuteNonQuery](http://www.java2s.com/Code/CSharp/Database-ADO.net/CreatetablethroughSqlConnection.htm)來創建它。或者將其封裝在Stored-Procedure中,並通過將[Command's Type設置爲StoredProcedure](http://msdn.microsoft.com/en-us/library/system.data.commandtype.aspx)進行調用。 – 2012-03-16 09:10:12

+0

感謝@TimSchmelter ^^,你有辦法檢查表是否有特定的行,如「用戶名」? – 2012-03-16 09:12:33

+0

這是第二步。 「行」不包含數據,但它是字段。因此,向我們展示一些您想要檢查存在的示例數據。 – 2012-03-16 09:17:25

回答

2

我要在這裏附上一個腳本,將轉儲所有對象和列在一個不是Temptable的對象。我在與之比較的數據庫上運行相同的腳本,並檢查哪些對象不存在,哪些列不存在,哪些列已更改。我使用Delphi應用程序很早就則「升級」我的DB的

我運行的主數據庫上運行此代碼。

If Exists(Select 1 from sysobjects where name = 'CheckTables') 
    Drop Table CheckTables 
GO 
Select o.id oid, o.name oname, c.colid cid, c.name cname, t.name ctype, c.xusertype, c.[length] tlength, c.prec cprec, c.scale cscale, isnullable 
into CheckTables 
from sysobjects o 
inner join syscolumns c on c.id = o.id 
inner join systypes t on t.xusertype = c.xusertype 
where o.name not like '%dt_%' and o.category <> 2 and o.type = 'U' 
order by o.id, c.colid 

Delete CheckTables where oname = 'CheckTables' 

然後我BCP數據到一個平面文件 當我跑我升級,我與相同結構的升級DB創建一個表,BCP在那裏的主數據庫的數據。

然後我用這個腳本,然後在我的Delphi應用程序檢查什麼改變。

Select oname, cname, ctype, IsNull(tlength, 0), IsNull(cprec, 0), IsNull(cscale, 0), ctype, isnullable from CheckTables hr 
where cname not in (Select name from syscolumns where id = object_id(oname) 
and length = hr.tlength 
and xusertype = hr.xusertype 
and isnullable = hr.isnullable) 
order by oname 

這應該讓你去。

如果你需要在它的C#部分的更多信息,我可以給你一些代碼。

下面是C#代碼,讓你去。有些東西,你將不得不添加自己,但如果你努力,讓我知道。

private void UpgradeDB() 
    { 
     SqlConnection conn = new SqlConnection("Some Connection String"); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.Connection = conn; 
     conn.Open(); 
     cmd.CommandText = "If 1 = (Select 1 from sysobjects where id = object_id('CheckTables'))\r\n" + 
          " Drop Table CheckTables\r\n" + 
          "Create Table CheckTables\r\n" + 
          "(oid int,\r\n" + 
          "oname varchar(50),\r\n" + 
          "colid int,\r\n" + 
          "cname varchar(50),\r\n" + 
          "ctype varchar(50),\r\n" + 
          "cxtype int,\r\n" + 
          "tlength int,\r\n" + 
          "cPrec int,\r\n" + 
          "cScale int,\r\n" + 
          "isnullable int"; 
     cmd.ExecuteNonQuery(); 

     //BCP your data from MASTER TABLE into the CheckTables of the UpgradeDB 

     cmd.CommandText = "Select oname, cname, ctype, IsNull(tlength, 0), IsNull(cprec, 0), IsNull(cscale, 0), isnullable from CheckTables hr\r\n" + 
          "where cname not in (Select name from syscolumns where id = object_id(oname)\r\n" + 
          "and length = hr.tlength\r\n" + 
          "and xusertype = hr.xusertype\r\n" + 
          "and isnullable = hr.isnullable)\r\n" + 
          "order by oname"; 
     SqlDataReader read = cmd.ExecuteReader(); 
     string LastTable = ""; 
     bool TableExists = false; 
     bool ColumnExists = false; 
     while(read.Read()) 
     { 
      if(LastTable != read[0].ToString()) 
      { 
       LastTable = read[0].ToString(); 
       TableExists = false; 
       if (!CheckIfTableExist(LastTable)) 
        TableExists = CreateTable(LastTable); 
       else 
        TableExists = true; 
      } 
      if (TableExists) 
      { 
       if (!CheckIfColumnExists(read[0].ToString(), read[1].ToString())) 
       { 
        CreateColumn(read[0].ToString(), read[1].ToString(), read[2].ToString(),  
         Convert.ToInt32(read[3].ToString()), Convert.ToInt32(read[4].ToString()), 
         Convert.ToInt32(read[5].ToString()), Convert.ToBoolean(read[6].ToString())); 
        ColumnExists = false; //You don't want to alter the column if you just created it 
       } 
       else 
        ColumnExists = true; 

       if(ColumnExists) 
       { 
        AlterColumn(read[0].ToString(), read[1].ToString(), read[2].ToString(), 
         Convert.ToInt32(read[3].ToString()), Convert.ToInt32(read[4].ToString()), 
         Convert.ToInt32(read[5].ToString()), Convert.ToBoolean(read[6].ToString())); 
       } 
      } 
     } 
     read.Close(); 
     read.Dispose(); 
     conn.Close(); 
     cmd.Dispose(); 
     conn.Dispose(); 

    } 

    private bool CheckIfTableExist(string TableName) 
    { 
     SqlConnection conn = new SqlConnection("Connection String"); 
     SqlCommand cmd = new SqlCommand(); 
     conn.Open(); 
     cmd.Connection = conn; 
     cmd.CommandText = "Select IsNull(object_id('" + TableName + "'), 0)"; 
     Int64 check = Convert.ToInt64(cmd.ExecuteScalar()); 
     conn.Close(); 
     cmd.Dispose(); 
     conn.Dispose(); 
     return check != 0; 
    } 

    private bool CreateTable(string TableName) 
    { 
     try 
     { 
      //Write your code here to create your table 
      return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 

    private bool CheckIfColumnExists(string TableName, string ColName) 
    { 
     SqlConnection conn = new SqlConnection("Connection String"); 
     SqlCommand cmd = new SqlCommand(); 
     conn.Open(); 
     cmd.Connection = conn; 
     cmd.CommandText = "Select IsNull(id, 0) from syscolumns where id = object_id('" + TableName + "') and name = '" + ColName + "'"; 
     Int64 check = Convert.ToInt64(cmd.ExecuteScalar()); 
     conn.Close(); 
     cmd.Dispose(); 
     conn.Dispose(); 
     return check != 0; 
    } 

    private void CreateColumn(string TableName, string ColName, string ColType, int Length, int Precision, int Scale, bool Nullable) 
    { 
     //Write your code here to create your column 

    } 

    private void AlterColumn(string TableName, string ColName, string ColType, int Length, int Precision, int Scale, bool Nullable) 
    { 
     //Write your code here to alter your column 
    }