所以我有一个设置为导入CSV文件的应用程序。它同时创建数据表和临时sql表。该代码读取CSV并将其放入数据表中,然后将其批量导入到临时表中,到目前为止,临时表仅将其复制到生产表中即可。就我的目的而言,我需要这样做,以便在导入.csv文件时有空字符串被跳过,并且不会在生产数据库中插入或更新,而仅插入/更新csv中的内容(除非有空字符串课程)。

例如,说我第一次导入时,我只是简单地导入了以下站点信息:


  PDW0033B50D3,North Western Office,246 Western Blvd


然后稍后说,我想更新位置的地址,因此我仅使用必要的字段重新导入了一个csv文件。


  PDW0033B50D3,290西大街


现在,代码为空时,代码将在数据库的第二行中放置一个空字符串,因此Northeast办公室将在数据库中将其转换为空白/空字符串。我如何做到这一点,以便在csv中有空字符串时,通过我的sql查询将其跳过?

这是我的代码:

       public void btnBulkSite_click(object sender, EventArgs e)
    {
            //Save File
        if (FileUpLoad1.HasFile)
        {
            FileUpLoad1.SaveAs(@"C:\temp\" + FileUpLoad1.FileName);
            btnBulkSite.Text = "File Uploaded: " + FileUpLoad1.FileName;
        }
        else
        {
            btnBulkSite.Text = "No File Uploaded.";
        }

            //Create Temporary Table

        string TmpTable = "CREATE TABLE #TMPBULK (SERVER_ID VARCHAR(13), SITE_NAME VARCHAR(50), SITE_ADDRESS VARCHAR(50))";
            //Create DataTable
        DataTable tblcsv = new DataTable();

        tblcsv.Columns.Add(new DataColumn("SERVER_ID", typeof(string)));
        tblcsv.Columns.Add(new DataColumn("SITE_NAME", typeof(string)));
        tblcsv.Columns.Add(new DataColumn("SITE_ADDRESS", typeof(string)));



            //Read in CSV File
        System.IO.StreamReader stream = new System.IO.StreamReader(FileUpLoad1.PostedFile.InputStream);

        string ReadCSV = stream.ReadToEnd();
        foreach (string csvRow in ReadCSV.Split('\n'))
            {

                if (!string.IsNullOrEmpty(csvRow))
                {
                tblcsv.Rows.Add();
                int count = 0;

                foreach (string FileRec in csvRow.Split(','))
                {
                    tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;

                    count++;
                }
            }
        }

            //SQL CON
        string conString = ConfigurationManager.ConnectionStrings["css"].ConnectionString;
        using (SqlConnection con2 = new SqlConnection(conString))
        {
            con2.Open();

            //Execute the command to make a temp table
            SqlCommand cmd = new SqlCommand(TmpTable, con2);
            cmd.ExecuteNonQuery();
            using (BulkOperation bulk = new BulkOperation(con2))

            {


                bulk.DestinationTableName = "#TMPBULK";

                bulk.BulkInsert(tblcsv);
            }

            //Copy Data from Temp Table to Prod Table
            string mergeSql = "merge into SITE_INFO as Target " +
              "using #TMPBULK as Source " +
              "on " +
              "Target.SERVER_ID=Source.SERVER_ID " +
              "and Target.SITE_NAME = Source.SITE_NAME " +
              "when matched then " +
              "update set Target.SITE_ADDRESS=Source.SITE_ADDRESS " +
              "when not matched then " +
              "insert (SERVER_ID,SITE_NAME,SITE_ADDRESS) values (Source.SERVER_ID,Source.SITE_NAME,Source.SITE_ADDRESS);";






            cmd.CommandText = mergeSql;
            cmd.ExecuteNonQuery();
            cmd.CommandText = "drop table #TMPBULK";
            cmd.ExecuteNonQuery();
        }
    }

最佳答案

尝试从更改您的SET

update set Target.SITE_ADDRESS=Source.SITE_ADDRESS




update set Target.SITE_ADDRESS=IF(Source.SITE_ADDRESS='',Target.SITE_ADDRESS,Source.SITE_ADDRESS)


同样适用于SITE_NAME。

关于c# - MySQL跳过空字符串查询?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36728109/

10-12 17:45