我想创建一个动态的SSIS包,该包将首先检查源中是否有任何架构更改?如果是,则相应地更改目标位置(不要删除然后创建表),然后进行数据传输。 我尝试使用“传输SQL Server对象任务”,但是此选项首先删除表。 谁能帮助我,在此先感谢。
我的脚本-
public string CompareDDL(DataRowCollection sourceArray, DataRowCollection destArray)
{
var upQuery = "";
var source = "";
var sourcetype = "";
var TableColumns = "";
if (destArray.Count == 0)
{
for (var i = 0; i < sourceArray.Count; i++)
{
var sourceItem = sourceArray[i].ItemArray;
source = sourceItem[1] + "." + sourceItem[2];
TableColumns += "[" + sourceItem[3] + "] " + getDataType(sourceItem) + ",";
}
upQuery = "Create table " + source + " (" + TableColumns.TrimEnd(',') + ")";
}
else if (sourceArray.Count > destArray.Count)
{
for (var i = destArray.Count; i < sourceArray.Count; i++)
{
var sourceItem = sourceArray[i].ItemArray;
sourcetype += sourceItem[3] + " " + getDataType(sourceItem) + ",";
source = sourceItem[1] + "." + sourceItem[2];
}
upQuery = "ALTER TABLE " + source + " ADD " + sourcetype.TrimEnd(',');
}
else
{
for (var i = 0; i < sourceArray.Count; i++)
{
var sourceItem = sourceArray[i].ItemArray;
var destItem = destArray[i].ItemArray;
if (sourceItem[5].ToString() == destItem[5].ToString())
{
if (sourceItem[6].ToString() == destItem[6].ToString())
{
if (sourceItem[0].ToString() == destItem[9].ToString())
{
}
}
else
{
upQuery += "ALTER TABLE " + sourceItem[1] + "." + sourceItem[2] + " ALTER COLUMN " + sourceItem[3] + " " + getDataType(sourceItem);
continue;
}
}
else
{
upQuery += "ALTER TABLE " + sourceItem[1] + "." + sourceItem[2] + " ALTER COLUMN " + sourceItem[3] + " " + getDataType(sourceItem);
continue;
}
}
}
return upQuery;
}
public string getDataType(object[] array)
{
string dataType = array[5].ToString();
if (dataType.ToLower() == "int" || dataType.ToLower() == "date")
{
return dataType;
}
else if (dataType.ToLower().Contains("char"))
{
return array[5].ToString() + " (" + array[7].ToString() + ")";
}
else if (dataType.ToLower() == "decimal")
{
return array[5].ToString() + " (" + array[6].ToString() + " ," + array[9].ToString() + ")";
}
return dataType;
}
public DataTable getDestination(SqlConnection DestinationConnection)
{
//Destinantion
//Read list of Tables with Schema from Database
string dQuery = "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE, NUMERIC_PRECISION, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE,DATETIME_PRECISION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Inventory'";
//MessageBox.Show(query.ToString());
SqlCommand dCmd = new SqlCommand(dQuery, DestinationConnection);
DestinationConnection.Open();
DataTable ddt = new DataTable();
ddt.Load(dCmd.ExecuteReader());
DestinationConnection.Close();
return ddt;
}
public DataTable getSource(SqlConnection myADONETConnection)
{
//Destinantion
//Read list of Tables with Schema from Database
string query = "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE, DATA_TYPE, NUMERIC_PRECISION, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE,DATETIME_PRECISION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Inventory'";
SqlCommand cmd = new SqlCommand(query, myADONETConnection);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
myADONETConnection.Close();
return dt;
}
public object getInsertQueryParam(object a)
{
string re = null;
// string dataType = array[5].ToString();
if (a.GetType() == typeof(int) || a.GetType() == typeof(decimal) || a.GetType().ToString().Contains("date"))
{
return a;
}
else if (a.GetType() == typeof(DBNull))
{
return null;
}
else
{
return "'" + a + "'";
}
}
public void getDestinationTableSchema()
{
//Source
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections[Dts.Variables["User::SourceLocal"].Value.ToString()].AcquireConnection(Dts.Transaction) as SqlConnection);
//Destinantion
SqlConnection DestinationConnection = new SqlConnection(Dts.Variables["User::SourceLocal"].Value.ToString());
DataTable dt = getSource(myADONETConnection);
DataTable ddt = getDestination(DestinationConnection);
var upQuery = CompareDDL(dt.Rows, ddt.Rows);
if (upQuery != "")
{
DestinationConnection.Open();
SqlCommand myCommand = new SqlCommand(upQuery, DestinationConnection);
myCommand.ExecuteNonQuery();
DestinationConnection.Close();
ddt = getDestination(DestinationConnection);
upQuery = CompareDDL(dt.Rows, ddt.Rows);
}
string SchemaName = "";
string DbName = "";
string TableName = "";
string TableColumns = "";
foreach (DataRow dt_row in dt.Rows)
{
string ColumnName = "";
string ColumnType = "";
string ColumnIsNull = "";
object[] array = dt_row.ItemArray;
SchemaName = array[1].ToString();
TableName = array[2].ToString();
ColumnName = array[3].ToString();
ColumnType = getDataType(array);
ColumnIsNull = array[0].ToString();
DbName = array[0].ToString();
}
string queryString = "SELECT * from " + SchemaName + "." + TableName;
SqlDataAdapter adapter = new SqlDataAdapter(queryString, myADONETConnection);
DataSet ds = new DataSet();
adapter.Fill(ds);
var command = "";
foreach (DataTable table in ds.Tables)
{
String sqlCommandInsert = "";
String sqlCommandValue = "";
var dindex = 0;
foreach (DataColumn dataColumn in table.Columns)
{
sqlCommandValue += dataColumn + "],[";
dindex++;
}
sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
sqlCommandInsert = "INSERT into [" + SchemaName + "].[" + TableName + "] (" + sqlCommandValue + ") VALUES";
int columnCount = table.Columns.Count;
var inQuery = "";
foreach (DataRow row in table.Rows)
{
string columnvalues = "";
for (int i = 0; i < columnCount; i++)
{
int index = table.Rows.IndexOf(row);
columnvalues += getInsertQueryParam(table.Rows[index].ItemArray[i]) + ",";
}
inQuery += "( "+ columnvalues.TrimEnd(',') + "),";
}
command = sqlCommandInsert + inQuery.TrimEnd(',');
}
myADONETConnection.Close();
var finalQuery = "";
if (upQuery == "")
{
finalQuery = command;
}
else
{
finalQuery = upQuery + " " + command;
}
DestinationConnection.Open();
SqlCommand myCommand1 = new SqlCommand(finalQuery, DestinationConnection);
myCommand1.ExecuteNonQuery();
DestinationConnection.Close();
}
public void Main()
{
// TODO: Add your code here
string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
try
{
getDestinationTableSchema();
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(Dts.Variables["User::FolderPath"].Value.ToString() + "\\" +
Dts.Variables["User::ExcelFileName"].Value.ToString() + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
答案 0 :(得分:0)
您可以编写脚本任务来执行此操作。