繁体   English   中英

使用 C# 从 SQL 服务器获取数据

[英]Getting data from SQL Server with C#

我构建了一个 C# 应用程序,它从 SQL 服务器表中读取数据,然后使用此数据创建一个.csv文件。

    public static DataTable getData()
    {
        ConnectToHostCMA();
        SqlDataAdapter sda = null;
        DataTable dt = new DataTable();

        dt.TableName = "dataInfo";

        try
        {
            /**************** A modifier l'instance de Sage**************/
            string rq = string.Format(@"SELECT ID_CODART_0, ID_CODUNI_0, ID_CODSIT_0, ID_CODTVA_0, ID_CODGES_0, ST_DES_0,  ID_NUMDOC1_0,ST_TYPART_0,ST_MAT_0,DT_MAJ_0,ST_TXTCDE_0,ST_CODCOM_0,ST_CLAREA_0, NU_DEL_0,ST_AFF_0,ST_RIS_0, ST_INA_0,ID_CODUTI_0, CODLONFAM_0, ID_CODFOU_0, ST_REFFOU_0, PST_ART_SIT_0 FROM TESTTUN.YART");

            sda = new SqlDataAdapter(rq, cnSqlCMA);
            sda.Fill(dt);
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
            if (sda != null)
                sda.Dispose();

            closeConneHostCMA();
        }
        return dt;
    }

    public static void createFiles(DataTable data,  String pathImport)
    {
        try
        {
            string dateSys = DateTime.Now.ToString().Replace("/", "-").Replace(':','-');


            string nomFile = String.Format("ART-{0}", dateSys);
            string path = String.Format(@"{0}\{1}.csv", pathImport, nomFile);

            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    string ligne1 = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14};{15};{16};{17};{18};{19};{20};{21}", "ID_CODART", "ID_CODUNI", "ID_CODSIT", "ID_CODTVA", "ID_CODGES", "ST_DES", "ID_NUMDOC1", "ST_TYPART", "ST_MAT", "DT_MAJ", "ST_TXTCDE", "ST_CODCOM", "ST_CLAREA", "NU_DEL", "ST_AFF", "ST_RIS", "ST_INA", "ID_CODUTI", "CODLONFAM", "ID_CODFOU", "ST_REFFOU", "PST_ART_SIT");
                    sw.WriteLine(ligne1);
                    foreach (DataRow dr in data.Rows)
                    {
                        string ID_CODART = dr["ID_CODART_0"].ToString();
                        string ID_CODUNI = dr["ID_CODUNI_0"].ToString();
                        string ID_CODSIT = dr["ID_CODSIT_0"].ToString();
                        string ID_CODTVA = dr["ID_CODTVA_0"].ToString();
                        string ID_CODGES = dr["ID_CODGES_0"].ToString();
                        string ST_DES = dr["ST_DES_0"].ToString().Replace(';','-');
                        string ID_NUMDOC1 = dr["ID_NUMDOC1_0"].ToString();
                        string ST_TYPART = dr["ST_TYPART_0"].ToString().Replace("2", "STO").Replace("1", "HST");
                        string ST_MAT = dr["ST_MAT_0"].ToString();
                        string DT_MAJ = dr["DT_MAJ_0"].ToString().Replace("1753-01-01 00:00:00.000", "2016-03-02 00:00:00.000");
                        string ST_TXTCDE = dr["ST_TXTCDE_0"].ToString();
                        string ST_CODCOM = dr["ST_CODCOM_0"].ToString();
                        string ST_CLAREA = dr["ST_CLAREA_0"].ToString();
                        string NU_DEL = dr["NU_DEL_0"].ToString();
                        string ST_AFF = dr["ST_AFF_0"].ToString();
                        string ST_RIS = dr["ST_RIS_0"].ToString();
                        string ST_INA = dr["ST_INA_0"].ToString();
                        string ID_CODUTI = dr["ID_CODUTI_0"].ToString();
                        string CODLONFAM = dr["CODLONFAM_0"].ToString();
                        string ID_CODFOU = dr["ID_CODFOU_0"].ToString();
                        string ST_REFFOU = dr["ST_REFFOU_0"].ToString();
                        string PST_ART_SIT = dr["PST_ART_SIT_0"].ToString();

                        string ligne = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};{14};{15};{16};{17};{18};{19};{20};{21}", ID_CODART, ID_CODUNI, ID_CODSIT, ID_CODTVA, ID_CODGES, ST_DES, ID_NUMDOC1, ST_TYPART, ST_MAT, DT_MAJ, ST_TXTCDE, ST_CODCOM, ST_CLAREA, NU_DEL, ST_AFF, ST_RIS, ST_INA, ID_CODUTI, CODLONFAM, ID_CODFOU, ST_REFFOU, PST_ART_SIT);
                        sw.WriteLine(ligne);
                    }
                }
            }
        }
        catch (Exception ex)
        {

            throw;
        }
    }

我的 output 就像我想要的一样,但是在某些情况下,当没有数据时,function 会生成一个空的.csv文件。 我怎样才能获得非空数据,以便createFiles() function 不会生成空文件?

只需检查DataTable是否不为空

public static void createFiles(DataTable data, String pathImport)
{
    if(data.Rows.Count == 0)
    {
        return;
    }

    // ...
}

无需检查null ,您正在getData()中创建新实例。

在开始生成文件之前检查您的数据表是否不为空

if (dataTable1 .Rows.Count > 0)
{
 // your code goes here
}
else
{
    return;
}

根据您的问题,即

我怎样才能获得非空数据,以便 createFiles() function 不会生成空文件。

我了解您不想生成空文件。 这可以通过检查 DataTable 的总行数来实现。 查询应如下所示:

if (yourDT.Rows.Count <= 0) // Read if the query returns 0 rows
{
    // Do your code here, for example: create a new data or just ignore it and don't generate a file
}

您可以检查 function "createFiles" 中的 DataTable "data" 是否有行。 例如:

if(data != null && data.Rows > 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM