我正在使用SSIS,正在导入多个(30)txt文件。我使用txt文件的文件名动态创建表,并根据txt文件的第一行创建列-所有这些工作。当其中一个字段中有一个撇号'时,我的代码停止工作。
使用|分隔文件。
我的代码:
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["xxxxxxxxxxxxxx"].AcquireConnection(Dts.Transaction) as SqlConnection);
string line1 = "";
//Reading file names one by one
string SourceDirectory = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string[] fileEntries = Directory.GetFiles(SourceDirectory);
foreach (string fileName in fileEntries)
{
// do something with fileName
string columname = "";
//Reading first line of each file and assign to variable
System.IO.StreamReader file2 =
new System.IO.StreamReader(fileName);
string filenameonly = ((((fileName.Replace(SourceDirectory, "")).Replace(".txt", "")).Replace("\\", "")).Replace("-", "_"));
line1 = (" IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]." + filenameonly + "') AND type in (N'U'))DROP TABLE [dbo]." + filenameonly + " Create Table dbo." + filenameonly + "([" + file2.ReadLine().Replace("|", "] NVARCHAR(500),[") + "] NVARCHAR(500))").Replace(".txt", "");
file2.Close();
SqlCommand myCommand = new SqlCommand(line1, myADONETConnection);
myCommand.ExecuteNonQuery();
MessageBox.Show("TABLE IS CREATED");
//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("|", "],[") + "]";
}
else
{
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','") + "')";
SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
myCommand1.ExecuteNonQuery();
}
counter++;
}
SourceFile.Close();
违规行是:
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','") + "')";
我尝试对以下内容进行修改,以将单引号替换为无效:
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','") + line.Replace("'''", "") + "')";
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("'", "") + "')";
也无法嵌套替换:
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace(line.Replace("'''", ""),"|" ) + "')";
以下内容不会导入任何内容:
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','").Replace("'", "") + "')";
更改为以下内容将全部导入,但随后在撇号行处进行故障转移:
string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','").Replace("'''", "") + "')";
我想念什么?
答案 0 :(得分:1)
与其像第一次尝试那样连接两个REPLACE()函数,不如将它们嵌套。
Replace(
Replace({arguments to remove apostrophe character}),
{arguments to remove pipe character}
)
如果要保留string.Replace()
的C#使用率,则应嵌套如下:
line.Replace({arguments to replace apostrophe).Replace({arguments to replace pipe})
或者您可以在两个单独的语句中这样做:
line = line.Replace({arguments to replace apostrophe});
line = line.Replace({arguments to replace pipe});