2017-01-26 35 views

回答

0

您正在要求我們爲您提供一些代碼。你還沒有嘗試過。

使用filereader,將每行添加到一個字符串。使用executablecalar或ExecuteNonquery或其他方法將字符串添加到數據庫中。

如果你給了我們一些代碼,它會更容易幫助你。現在,我什麼都做不了。谷歌我告訴你的功能,你會發現同樣的問題足夠的話題。

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 
+1

[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該**不**連接在一起您的SQL語句 - 使用**參數化查詢**來代替以避免SQL注入 –

0

我使用SSIS腳本任務,來讀取文本文件中的行。這使我可以讀取每個循環的6行組。 變量被帶入包含插入的SQL任務。我需要一些幫助,因爲它將所有文本行插入到同一個SQL行,它不是附加的,下面你會看到插入符號。

公共無效的主要(){

 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 TAKS」 INSERT INTO dbo.scores(imageName,bestGuess,firstScore,firsTag,secondGuess,secondScore,secondTag,thirdGuess,thirdScore ,第三標籤,第四標籤,第四標籤,第四標籤,第五標籤,第五標籤,第五標籤)VALUES(?,?,0,0,?,0,0,?,0,0,?,0,0,?,0,0)