我想使用SSIS中的脚本任务将csv文件加载到SQL表中,这些脚本任务具有相似的列名但不完全相同,列的数量也各不相同。
我目前正在使用这个有用的blog下面的脚本来检查确切的列名是否存在,如果存在,则将其加载到表中,但是如果列名不存在则会失败。有没有办法使用LIKE运算符从sql表中的csv文件中搜索列名?如果找到它,将数据加载到表中,如果找不到,则忽略该列。
脚本:
public void Main()
{
string delimiter = Dts.Variables["$Package::Delimiter"].Value.ToString();
string TableName = Dts.Variables["$Package::TableName"].Value.ToString();
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)
(Dts.Connections["ADOConn"].AcquireConnection(Dts.Transaction) as SqlConnection);
//Reading file names one by one
string SourceDirectory = Dts.Variables["$Package::SourceFolder"].Value.ToString();
string[] fileEntries = Directory.GetFiles(SourceDirectory);
foreach (string fileName in fileEntries)
{
// MessageBox.Show(fileName);
string columname = "";
//Reading first line of each file and assign to variable
System.IO.StreamReader file2 = new System.IO.StreamReader(fileName);
//Writing Data of File Into Table
int counter = 0;
string line;
System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
columname = line.ToString();
columname = "" + columname.Replace(delimiter, ",");
//MessageBox.Show(columname);
}
else
{
// MessageBox.Show("Inside ELSE");
string query = "Insert into " + TableName +
"(" + columname + ") VALUES('" + line.Replace(delimiter, "','") + "')";
//MessageBox.Show(query.ToString());
SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
myCommand1.ExecuteNonQuery();
}
counter++;
}
SourceFile.Close();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
谢谢, Ĵ