2012-03-16 7 views
0

私のC#クラスから自分のデータベースにテーブルが存在するかどうか、そしてそのテーブルに必要な行があるかどうかを調べる方法が必要です。 それが存在しないか、行のいくつかが不足している場合、それらを追加する必要があります。nullであればテーブル内のSQL Serverのテーブルと行を確認する

私はこの方法を使用しましたが、そこに欠けている機能を取得する方法はわかりません。 (Check if a SQL table exists

私はTempTableの内のオブジェクトのすべてのオブジェクトと列をダンプします、ここでスクリプトを添付するつもりですSQL ServerとC#

+1

あなたの指定されたリンクの回答は、存在するかどうかを確認する方法を示しています。存在しない場合は、[ExecuteNonQuery on SqlCommand](http://www.java2s.com/Code/CSharp/Database-ADO.net/CreatetablethroughSqlConnection.htm)を使用して作成します。または、これをStored-Procedureで囲み、[Command Type to StoredProcedure](http://msdn.microsoft.com/en-us/library/system.data.commandtype.aspx)を設定して呼び出します。 –

+0

ありがとう@TimSchmelter ^^、あなたはテーブルに "Username"のような特定の行があるかどうかをチェックする方法がありますか? –

+0

これは第2ステップです。 「行」にはデータは含まれませんが、フィールドです。したがって、存在を確認したいサンプルデータをいくつか見せてください。 –

答えて

2

で働いています。私は比較しているDBで同じスクリプトを実行し、存在しないオブジェクトと、存在しないテーブルの列と、変更された列を確認します。私は非常に前に、私のDBを "アップグレード"するために、デルファイアプリを使用しました

私はこのコードをMASTERデータベースで実行します。

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のテーブルを作成し、そこにマスター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#コードです。あなたが自分で追加しなければならないものがいくつかありますが、もしあなたが強く訴えるなら、私に知らせてください。

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 
    } 
関連する問題