使用C#仅将部分字符串插入SQL数据库

时间:2016-06-16 14:12:43

标签: c# sql-server

我是一名新的C#编码员,我也是Microsoft SQL Server的新手。我编写的代码将使用filehelpers解析文件中的数据,然后将该数据放在SQL表中。我的问题是它是否可以解析数据,但只将字符串的一部分放在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的值是Business Date:2016年6月14日。有没有办法让它只将日期放在Date_of_Report中(即摆脱“营业日期:”

1 个答案:

答案 0 :(得分:1)

试试这个:

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

您是将其存储为SQL日期时间吗?您可能希望以后转换它。

相关小提琴:https://dotnetfiddle.net/ql40kl