获取一个表的列属性以进行复制

时间:2018-06-01 15:50:30

标签: c# sqlite

我问过如何复制一张桌子,我有建议在旧帖子上阅读一篇关于现有答案的帖子。 我已经完成了,因为我无法将新代码添加为注释,我将之前的代码更改为最后一个代码,并且它非常适合我的需要。

遵循两个简短的方法:1st从源表获取表结构,并在新表中设置相同的结构。第二种方法将值从源表复制到目标表。每种方法都会丢弃列ID。

private void copyTable(Label objName, String table)
 {
     //Copy Table Structure
     var dbConnection = new SQLiteConnection("Data Source=" + objName.Text + ";");
     string fullPath = Path.GetDirectoryName(objName.Text);
     fullPath = fullPath + "\\output";
     Directory.CreateDirectory(fullPath);
     fullPath = fullPath + "\\source_" + Path.GetFileName(objName.Text);

     if (File.Exists(fullPath) == false)
     {
  SQLiteConnection.CreateFile(fullPath);
     }

     var dbConnection2 = new SQLiteConnection("Data Source=" + fullPath + ";");

     try
     {
  dbConnection.Open();
  dbConnection2.Open();

  SQLiteCommand cmd = new SQLiteCommand(dbConnection);
  cmd.CommandText = "select * from " + table;
  string columnNames = "";
  string columnDataType = "";

  //create table structure
  using (SQLiteDataReader reader = cmd.ExecuteReader())
  {
      string columnValues = ""; //this value will be passed to copyTableValues method
      for (var i = 0; i < reader.FieldCount; i++)
      {
   if (i == 0)
   {
columnValues = " (" + reader.GetName(i) + " " + reader.GetDataTypeName(i);
if (reader.GetName(i) != "ID")
{
    columnNames = reader.GetName(i);
    columnDataType = reader.GetDataTypeName(i);
}
   }
   else
   {
columnValues = columnValues + ", " + reader.GetName(i) + " " + reader.GetDataTypeName(i);
if (columnNames != "")
{
    columnNames = columnNames + "," + reader.GetName(i);
    columnDataType = columnDataType + "," + reader.GetDataTypeName(i);
}
else
{
    columnNames = reader.GetName(i);
    columnDataType = reader.GetDataTypeName(i);
}

if (i == reader.FieldCount - 1)
{
    columnValues = columnValues + ", primary key('ID'))"; //cmd example = "create table " + table + " (ID int, Customers text, CustomerID int, primary key('ID'))";
    string sql = "create table " + table + columnValues;
    SQLiteCommand cmd2 = new SQLiteCommand(sql, dbConnection2);
    cmd2.ExecuteNonQuery();
}
   }
      }
  }
  dbConnection.Close();
  dbConnection2.Close();

  //Run Table Copy
  copyTableValues(dbConnection, dbConnection2, table, columnNames, columnDataType);
     }
     catch (Exception)
     {
  //throw;
     }
 }

 private void copyTableValues(SQLiteConnection dbConnection, SQLiteConnection dbConnection2, String table, string columnNames, string columnDataType) //private void copyTableValues(Label objName, String table)
 {
     try
     {
  dbConnection.Open();
  SQLiteCommand cmd = new SQLiteCommand(dbConnection);
  cmd.CommandText = "select * from " + table;

  dbConnection2.Open();
  SQLiteCommand cmd2 = new SQLiteCommand(dbConnection2);

  using (SQLiteDataReader reader = cmd.ExecuteReader())
  {
      while (reader.Read())
      {
   string[] columnArr = columnNames.Split(',');
   string[] columnArr2 = columnDataType.Split(',');
   string tableValue = "";
   string columnName = "";
   string dataSeparator = "";

   for (var i = 0; i < columnArr.Length; i++)
   {
if (reader[columnArr[i]].ToString() != "")
{
    if (columnArr2[i] == "INTEGER")
    {
 dataSeparator = "";
    }
    if (columnArr2[i] == "TEXT")
    {
 dataSeparator = "'";
    }

    if (tableValue == "")
    {
 columnName = columnArr[i];
 tableValue = dataSeparator + reader[columnArr[i]].ToString() + dataSeparator; // 'Paul' so separator will be ' = dataSeparator+Paul+dataSeparator; 555 so separator will be empty=  dataSeparator555dataSeparator
    }
    else
    {
 columnName = columnName + "," + columnArr[i];
 tableValue = tableValue + ", " + dataSeparator + reader[columnArr[i]].ToString() + dataSeparator;
    }
}

if (i == columnArr.Length - 1)
{
    //insert into company (id,name,age,address,expence) values (1, 'Pippo', 30, 'Italy', 100.00);
    string sql = "insert into " + table + " (" + columnName + ") values (" + tableValue + ")";
    cmd2.CommandText = sql;
    cmd2.ExecuteNonQuery();
}
   }
      }

  }

  dbConnection.Close();
  dbConnection2.Close();
     }

     catch (Exception)
     {
  //throw;
     }
 }

0 个答案:

没有答案