使脚本组件根据文本文件中的值动态分配数据类型
To make a Script Component to dynamically assign a data type based on the value from a text file
我的任务是使用 SSIS 中的脚本组件将数据从 .txt 文件加载到 SQL 服务器 table。我已经使用脚本成功加载了它。
public void Main()
{
//Declare Variables
string SourceFolderPath = Dts.Variables["$Project::Landing_Zone"].Value.ToString();
string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
string ArchiveFolder = Dts.Variables["User::ArchiveFolder"].Value.ToString();
string ColumnsDataType = Dts.Variables["User::ColumnsDataType"].Value.ToString();
string SchemaName = Dts.Variables["$Project::SchemaName"].Value.ToString();
//Reading file names one by one
string[] fileEntries = Directory.GetFiles(SourceFolderPath, "*" + FileExtension);
foreach (string fileName in fileEntries)
{
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);
//Writing Data of File Into Table
string TableName = "";
int counter = 0;
string line;
string ColumnList = "";
//MessageBox.Show(fileName);
System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]";
TableName = (((fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "")).Replace("\", ""));
string CreateTableStatement = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[" + SchemaName + "].";
CreateTableStatement += "[" + TableName + "]')";
CreateTableStatement += " AND type in (N'U'))DROP TABLE [" + SchemaName + "].";
CreateTableStatement += "[" + TableName + "] Create Table " + SchemaName + ".[" + TableName + "]";
CreateTableStatement += "([" + line.Replace(FileDelimiter, "] " + ColumnsDataType + ",[") + "] " + ColumnsDataType + ")";
SqlCommand CreateTableCmd = new SqlCommand(CreateTableStatement, myADONETConnection);
CreateTableCmd.ExecuteNonQuery();
//MessageBox.Show(CreateTableStatement);
}
else
{
string query = "Insert into " + SchemaName + ".[" + TableName + "] (" + ColumnList + ") ";
query += "VALUES('" + line.Replace(FileDelimiter, "','") + "')";
// MessageBox.Show(query.ToString());
SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
myCommand1.ExecuteNonQuery();
}
counter++;
}
SourceFile.Close();
}
}
包变量详细信息
但问题是我只能使用单一数据类型将所有值从文本文件加载到 table(我使用 nvarchar(200) 作为所有值的通用数据类型)。我已将 nvarchar(200) 作为包变量之一的值传递,并在脚本中调用了它。
我希望脚本组件通过理解文本文件中可用的值来为列分配数据类型。
示例:我加载到 table 中的文本文件具有以下数据。
Id,Name,Age
1,Arun,25
2,Ramesh,26
3,Anish,28
因此,创建了一个名为 dbo.File_1 的 table,其中包含 Id、Name 和 Age 列。但是所有这些列都是使用与前面提到的相同的数据类型 nvarchar(200) 创建的。
但我的要求是脚本组件应该根据值分配数据类型,例如,Column Id应该分配数据类型为int,Name应该分配数据类型为nvarchar(50)等等。同样,脚本应根据任何类型的值(如十进制、日期等)分配数据类型
有什么方法可以实现吗?提前致谢。
附加提示:
此脚本通常在执行期间每次删除并创建一个新的 table。 table 名称基于文件名。
这是从臀部开始拍摄的,但可能使用了 tryparse:
col += int.TryParse(ID, out num)? "int" : "nvarchar(200)";
我的任务是使用 SSIS 中的脚本组件将数据从 .txt 文件加载到 SQL 服务器 table。我已经使用脚本成功加载了它。
public void Main()
{
//Declare Variables
string SourceFolderPath = Dts.Variables["$Project::Landing_Zone"].Value.ToString();
string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
string ArchiveFolder = Dts.Variables["User::ArchiveFolder"].Value.ToString();
string ColumnsDataType = Dts.Variables["User::ColumnsDataType"].Value.ToString();
string SchemaName = Dts.Variables["$Project::SchemaName"].Value.ToString();
//Reading file names one by one
string[] fileEntries = Directory.GetFiles(SourceFolderPath, "*" + FileExtension);
foreach (string fileName in fileEntries)
{
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);
//Writing Data of File Into Table
string TableName = "";
int counter = 0;
string line;
string ColumnList = "";
//MessageBox.Show(fileName);
System.IO.StreamReader SourceFile =
new System.IO.StreamReader(fileName);
while ((line = SourceFile.ReadLine()) != null)
{
if (counter == 0)
{
ColumnList = "[" + line.Replace(FileDelimiter, "],[") + "]";
TableName = (((fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "")).Replace("\", ""));
string CreateTableStatement = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[" + SchemaName + "].";
CreateTableStatement += "[" + TableName + "]')";
CreateTableStatement += " AND type in (N'U'))DROP TABLE [" + SchemaName + "].";
CreateTableStatement += "[" + TableName + "] Create Table " + SchemaName + ".[" + TableName + "]";
CreateTableStatement += "([" + line.Replace(FileDelimiter, "] " + ColumnsDataType + ",[") + "] " + ColumnsDataType + ")";
SqlCommand CreateTableCmd = new SqlCommand(CreateTableStatement, myADONETConnection);
CreateTableCmd.ExecuteNonQuery();
//MessageBox.Show(CreateTableStatement);
}
else
{
string query = "Insert into " + SchemaName + ".[" + TableName + "] (" + ColumnList + ") ";
query += "VALUES('" + line.Replace(FileDelimiter, "','") + "')";
// MessageBox.Show(query.ToString());
SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
myCommand1.ExecuteNonQuery();
}
counter++;
}
SourceFile.Close();
}
}
包变量详细信息
但问题是我只能使用单一数据类型将所有值从文本文件加载到 table(我使用 nvarchar(200) 作为所有值的通用数据类型)。我已将 nvarchar(200) 作为包变量之一的值传递,并在脚本中调用了它。
我希望脚本组件通过理解文本文件中可用的值来为列分配数据类型。
示例:我加载到 table 中的文本文件具有以下数据。
Id,Name,Age
1,Arun,25
2,Ramesh,26
3,Anish,28
因此,创建了一个名为 dbo.File_1 的 table,其中包含 Id、Name 和 Age 列。但是所有这些列都是使用与前面提到的相同的数据类型 nvarchar(200) 创建的。
但我的要求是脚本组件应该根据值分配数据类型,例如,Column Id应该分配数据类型为int,Name应该分配数据类型为nvarchar(50)等等。同样,脚本应根据任何类型的值(如十进制、日期等)分配数据类型
有什么方法可以实现吗?提前致谢。
附加提示:
此脚本通常在执行期间每次删除并创建一个新的 table。 table 名称基于文件名。
这是从臀部开始拍摄的,但可能使用了 tryparse:
col += int.TryParse(ID, out num)? "int" : "nvarchar(200)";