数据表在课外不被认可?

时间:2017-05-22 16:02:21

标签: c#

我有以下代码将datagrid的内容写入文本文件:

        string FSYear = "FS_" + DateTime.Now.Year.ToString() + "_Grids";

        string sql2 = "SELECT DISTINCT [ProductCode] FROM " + FSYear;
        DataTable Temp = SqlConnect(sql2);

        //create txt file of codes
        string path = @"C:\Users\user\Temp\Images.txt";
        if (!File.Exists(path))
        {
            FileStream fs = File.Create(path);
        }

        //Empty text file
        System.IO.File.WriteAllText(path, string.Empty);

        //use results from query
        List1.Items.Add("Writing " + Temp.Rows.Count + " lines...");
        foreach (DataRow row in Temp.Rows)
        {
            string productCode = row["ProductCode"].ToString();
            if (File.Exists(path))
            {

                using (StreamWriter outputFile = new StreamWriter(path, true))
                {
                    outputFile.WriteLine(productCode);


                }

            }
        }
        List1.Items.Add("Finished");

我希望它只是在“ProductCode”是一个数字时写入,然后在Temp.Rows.Count中反映这些数量。

这是可能的吗?如果是这样,我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

首先,要检查您是否有号码,您可以这样做:

  int result;
      if (int.TryParse(productCode, out result))
     { 
     //Number , count?
     }
    else
     {
    // Not number
     }

现在,在您的临时数据表中,您拥有所有值,为什么不在数字时计算?