从Text文件到SQL的数据转换

时间:2017-01-26 00:42:05

标签: c# ssis

4 个答案:

答案 0 :(得分:0)

您要求我们为您提供一些代码。你还没有尝试过。

使用文件阅读器,将每一行添加到字符串中。使用executioncalar或ExecuteNonquery或其他东西将字符串添加到数据库中。

如果您给我们一些代码,那么帮助您会更容易。现在,我什么也做不了。谷歌我告诉你的功能,你会找到相同问题的足够主题。

答案 1 :(得分:0)

这是我一直在尝试的两个解决方案

解决方案1:

//Create Connection to SQL Server
SqlConnection SQLConnection = new SqlConnection();
SQLConnection.ConnectionString = "Data Source = (local); Initial Catalog =TechBrothersIT; " + "Integrated Security=true;";

System.IO.StreamReader SourceFile = new System.IO.StreamReader(SourceFolder+SourceFileName);

string line = "";
Int32 counter = 0;

SQLConnection.Open();

while ((line = SourceFile.ReadLine()) != null)
{
    //skip the header row
    if (counter > 0)
    {
        //prepare insert query
        string query = "Insert into " + TableName +
                       " Values ('" + line.Replace(filedelimiter, "','") + "')";

       //execute sqlcommand to insert record
       SqlCommand myCommand = new SqlCommand(query, SQLConnection);
       myCommand.ExecuteNonQuery();
   }

   counter++;
}

SourceFile.Close();
SQLConnection.Close();

//Move the file to Archive folder
File.Move(SourceFolder+SourceFileName, ArchiveFodler + SourceFileName);              

解决方案2:

我最初使用Excel查询解决了这个问题:

Sub Transpone()
a = 1
   `enter code here` b = 8
    Do    ' Bucle externo.
    Do While Contador < 65000
        Contador = Contador + 1
        If Range("A" & a) <> "" Then
    Range("A" & a & ":A" & b).Select
    Selection.Copy
    Sheets("Converted").Select
    k = Range("A" & Cells.Rows.Count).End(xlUp).Row + 1
    `enter code here`Range("A" & k).Select
    `enter code here`Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Sheets("Identification").Select
    a = a + 8
    b = b + 8
        Else
    Comprobar = False
       Exit Do
        End If
       Loop
     Loop Until Comprobar = False
End Sub

答案 2 :(得分:0)

在SSIS中可以很容易地完成。 以下提到的链接可以帮助您。

https://blogs.msdn.microsoft.com/philoj/2007/11/10/transposing-rows-and-columns-in-sql-server-integration-services/

答案 3 :(得分:0)

我正在使用SSIS脚本任务,从文本文件中读取行。这允许我读取每个循环的6行组。 变量被带入包含插入的SQL任务。我需要一些帮助,因为它是在同一个SQL行上插入所有文本行,它没有附加,在下面你会看到插入声明。

public void Main()         {

        SqlConnection conn = new SqlConnection("Data Source=DESKTOP-QBDQ35H; Initial Catalog=TensorFacts; Integrated Security=True");
        int counter = 0;

        System.IO.StreamReader file = new System.IO.StreamReader("C:\\Imagine processing\\output.txt");
        string[] large = System.IO.File.ReadAllLines("C:\\Imagine processing\\output.txt");
        for (int i = 0; i < large.Length; i += 6)

        {

            Dts.Variables["User::imageName"].Value = (string)large[i];
            Dts.Variables["User::bestGuess"].Value = (string)large[i + 1];
            Dts.Variables["User::secondGuess"].Value = (string)large[i + 2];
            Dts.Variables["User::thirdGuess"].Value = (string)large[i + 3];
            Dts.Variables["User::fourthGuess"].Value = (string)large[i + 4];
            Dts.Variables["User::fifthGuess"].Value = (string)large[i + 5];

            //string line0 = large[i];
            //string line1 = large[i + 1];
            //string line2 = large[i + 2];
            //string line3 = large[i + 3];
            //string line4 = large[i + 4];
            //string line5 = large[i + 5];


            //MessageBox.Show(line0 + '\n' + line1 + '\n' + line2 + '\n' + line3 + '\n' + line4 + '\n' + line5 + '\n');

            //counter++;
        }
    }  

然后在SSIS&#34; EXECUTE SQL taks&#34; INSERT INTO dbo.scores(imageName,bestGuess,firstScore,firsTag,secondGuess,secondScore,secondTag,thirdGuess,thirdScore,thirdTag,fourthGuess,fourthScore,fourthTag,fifthGuess,fifthScore,FifthTag)VALUES(?,?,0,0,?, 0,0,?,0,0,?,0,0,?,0,0)