C# 从文本文件到SQL的数据转换

C# 从文本文件到SQL的数据转换,c#,ssis,C#,Ssis,您要求我们提供一些代码。你还没试过呢 使用文件读取器,将每一行添加到字符串中。使用executescalar或ExecuteOnQuery等将字符串添加到数据库中 如果你给我们一些代码,帮助你会更容易。现在,我什么都做不了。谷歌的功能,我告诉你,你会找到足够的主题与相同的问题 这是我一直在尝试的两种解决方案 解决方案1: //Create Connection to SQL Server SqlConnection SQLConnection = new SqlConnection(); SQL

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

使用文件读取器,将每一行添加到字符串中。使用executescalar或ExecuteOnQuery等将字符串添加到数据库中


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

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

解决方案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
Sub-Transpone()
a=1
`在此处输入代码`b=8
不要在外面说话。
当Contador<65000时执行此操作
康塔多=康塔多+1
如果范围(“A”&A)”,则
范围(“A”&A&“:A”&b)。选择
选择,复制
图纸(“已转换”)。选择
k=范围(“A”和Cells.Rows.Count)。结束(xlUp)。行+1
`在此处输入代码`Range(“A”&k)。选择
`在此处输入代码`Selection.PasteSpecial Paste:=xlPasteAll,操作:=xlNone,SkipBlanks:=_
False,转置:=True
表格(“标识”)。选择
a=a+8
b=b+8
其他的
compobar=False
退出Do
如果结束
环
循环直到Comprobar=False
端接头

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


我正在使用SSIS脚本任务从文本文件中读取行。这允许我读取每组6行的循环。 变量被带入包含insert的SQL任务中。我需要一些帮助,因为它正在同一SQL行上插入所有文本行,而不是追加,下面您将看到insert语句

公共图书馆 {

        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++;
        }
    }  
SqlConnection conn=newsqlconnection(“数据源=DESKTOP-QBDQ35H;初始目录=TensorFacts;集成安全性=True”);
int计数器=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”);
对于(int i=0;i
然后在SSIS中“执行SQL taks”
插入dbo.scores(imageName、bestGuess、FirstScores、firsTag、secondGuess、secondTag、thirdGuess、thirdScore、thirdTag、fourthGuess、FourthScores、fourthTag、fifthGuess、FifthScores、fifthTag)值(?、0,0,0,0,0,0)

请单击蓝色文本查看当前数据和所需状态请单击此蓝色文本阅读应如何提问,还有这个——您不应该将SQL语句连接在一起——使用参数化查询来避免SQL注入。我正在使用这个脚本从文本文件中获取行。这允许我每次读取6行的组。