附加到文件导致数据丢失

时间:2015-11-24 15:14:49

标签: c# file data-loss

我有一个脚本连接到多个数据库,并将每个数据库的查询写入文本文件。但是当我运行它时,我没有得到预期的结果。经过一些交叉检查后,看起来它是从第一个数据库查询中写入相同的结果,而不是从下一个连接中查找新结果。我插入了一个IP字符串来验证for循环是否正在抓取IP,但似乎我需要一些方法来清除阅读器?

for (int z=0; z<2;z++) {
    using (OleDbConnection connLocal = new OleDbConnection("Provider=SAOLEDB;LINKS=tcpip(host=" + ips[z] + ",PORT=2638);ServerName=EAGLESOFT;Integrated Security = True; User ID = dba; PWD = sql"))
    try
    {
        connLocal.Open();
        using (OleDbCommand cmdLocal = new OleDbCommand("SELECT tran_num, '" + ips[z] + "', provider_id, amount, tran_date, collections_go_to, impacts, type, '" + clinics[z] + "' AS Clinic FROM transactions WHERE tran_date LIKE '2015-11-23%'", connLocal))
       using (StreamWriter sqlWriter = File.AppendText(@"C:\Users\Administrator\Desktop\Clinic.txt"))
        {

            using (OleDbDataReader readLocal = cmdLocal.ExecuteReader())
            {
                while (readLocal.Read())
                {
                    sqlWriter.WriteLine("{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}",
                    readLocal.GetValue(0).ToString(),
                    readLocal.GetValue(1).ToString(),
                    readLocal.GetValue(2).ToString(),
                    readLocal.GetValue(3).ToString(),
                    readLocal.GetValue(4).ToString(),
                    readLocal.GetValue(5).ToString(),
                    readLocal.GetValue(6).ToString(),
                    readLocal.GetValue(7).ToString(),
                    readLocal.GetValue(8).ToString());
                }
                readLocal.Close();
            }
            sqlWriter.Close();
            connLocal.Close();
        }
    }
    catch (Exception connerr) { Debug.WriteLine(connerr.Message); }
}

一如既往,非常感谢任何见解!

0 个答案:

没有答案