【问题标题】:Data conversion from a Text file to SQL从文本文件到 SQL 的数据转换
【发布时间】:2017-01-26 00:42:04
【问题描述】:

【问题讨论】:

  • 请点击蓝色文字查看当前数据和想要的状态
  • 请点击此蓝色文字How to Ask 了解如何提问,以及此Tour

标签: c# ssis


【解决方案1】:

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

使用文件阅读器,将每一行添加到一个字符串中。使用 executescalar 或 ExecuteNonquery 之类的东西将字符串添加到您的数据库中。

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

【讨论】:

    【解决方案2】:

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

    解决方案 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
    

    【讨论】:

    • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入
    【解决方案3】:

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

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

    【讨论】:

    • 我正在使用此脚本从文本文件中读取行。这允许我每次读取 6 行的组。
    【解决方案4】:

    我正在使用 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 中“执行 SQL 任务” 插入 dbo.scores (imageName, bestGuess, firstScore,firsTag, secondGuess,secondScore, secondTag, thirdGuess,thirdScore,thirdTag,fourthGuess, FourthScore, FourthTag,fifthGuess,fifthScore, FifthTag) 值 (?,?,0,0,?, 0,0,?,0,0,?,0,0,?,0,0)

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2015-09-28
      • 2016-04-23
      • 2021-10-30
      相关资源
      最近更新 更多