2016-06-16 99 views
-1

我是新的C#編碼器,我也是Microsoft SQL Server的新成員。我編寫的代碼將使用filehelpers解析來自文件的數據,然後將該數據放入SQL表中。我的問題是它是否可以解析數據,但只將部分字符串放在SQL服務器中。例如,這裏是我的代碼:使用C將僅字符串的一部分插入到SQL數據庫中#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using FileHelpers; 
using System.Data.SqlClient; 
using System.IO; 

namespace OnQ_prototype 
{ 
    class Report 
    { 
     [DelimitedRecord("\t")] 
     [IgnoreEmptyLines()] 



     public class ColumnReportNames 
     { 
      public String textbox22; 
      public String textbox29; 
      public String textbox24; 
      public String textbox23; 
      public String PSFullAcct; 
      public String AccountDescription; 
      public String BusDateAmount; 
      public String ThisPTDAmount; 
      public String LastPTDAmount; 
      public String ThisYTDAmount; 
      public String LastYTDAmount; 
      public String BusDatePctAvail; 
      public String ThisPTDPctAvail; 
      public String LastPTDPctAvail; 
      public String ThisYTDPctAvail; 
      public String LastYTDPctAvail; 


     } 

     static void ProcessFilesCSVFiles(string originalPath, string destinationPath) 
     { 

      foreach (var GenesisDirectory in Directory.GetDirectories(originalPath)) 
      { 
       foreach (var inputFile in Directory.GetFiles(GenesisDirectory, "*.csv")) 
       { 

        string lines = inputFile; 

        FileHelperEngine engine = new FileHelperEngine(typeof(ColumnReportNames)); 
        var records = engine.ReadFile(lines) as ColumnReportNames[]; 
        foreach (var record in records) 
        { 



         SqlCommand cmd; 
         SqlConnection conn; 
         conn = new SqlConnection("Data Source=hureports01;Initial Catalog=hureports;Integrated Security=True"); 
         conn.Open(); 
         var sqlCommand = string.Format(@"MERGE [OnQReport] AS target USING (select @Property as Property, @Date_of_Report as Date_of_Report, @Percent_Occupancy_PAR as Percent_Occupancy_PAR, @val as {0}) AS source ON (target.Date_of_Report = source.Date_of_Report) WHEN MATCHED THEN UPDATE SET {0}= source.{0} WHEN NOT MATCHED THEN INSERT (Property, Date_of_Report, Percent_Occupancy_PAR, {0}) VALUES (source.Property, source.Date_of_Report, Percent_Occupancy_PAR, source.{0});", column); 
         cmd = new SqlCommand(sqlCommand, conn); 
         cmd.Parameters.AddWithValue("@Property", record.textbox24); 
         cmd.Parameters.AddWithValue("@Date_of_Report", record.textbox23); 
         cmd.Parameters.AddWithValue("@Percent_Occupancy_PAR", amount2); 
         cmd.Parameters.AddWithValue("@val", amount); 
         cmd.ExecuteNonQuery(); 
         conn.Close(); 

        } 

所以我添加的一個值是Date_of_Report它位於textbox23。但是,textbox23的價值是商業日期:6/14/2016。有沒有辦法爲它只是把日期Date_of_Report(即擺脫「商務時間」的

+1

歡迎堆棧溢出,請閱讀[如何創建一個最小的,完整的,並且可驗證的示例](http://stackoverflow.com/help/mcve) –

+0

是的,這是一個簡單的編程問題,請goog le「C#字符串解析示例」並嘗試找到的示例。如果遇到您不明白的錯誤,請嘗試將其作爲問題發佈。 –

+0

謝謝。我對編碼非常陌生,但是這對我有很大的幫助 – user6451557

回答

1

試試這個:

string[] getDate = textBoxVal.Split(':'); 
string dateOfReport = getDate[1]; 

你保存這是一個SQL日期時間,您可能希望後來將其轉換

相關小提琴:!https://dotnetfiddle.net/ql40kl

+0

這很好。謝謝 – user6451557

相關問題