在Windows应用程序.net C#中将记录写入txt文件的问题 [英] issue writing record to txt file in windows application .net C#

查看:60
本文介绍了在Windows应用程序.net C#中将记录写入txt文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有,


我想将数据库记录写入txt文件....我不想在我关闭应用程序并运行时覆盖文件,然后记录不覆盖txt文件,而仅在旧记录之后写入

我的代码如下..

Dear all ,


i want to write database record into txt file ....i dont want to overwrite the file when i close application and run then record not overwrite in txt file but only write after old record

my code is given below..

string physicalPath = "D:\\a.txt";
           
            count = 0;
            SqlConnection ConnCMS = new SqlConnection();
            string SqlConnectionString;
            SqlConnectionString = @"Data Source=localhost;Initial Catalog=CMS;User Id=sa;Password=samesame;";
            ConnCMS.ConnectionString = SqlConnectionString;
            SqlCommand com = new SqlCommand("select EmployeeNo,EmployeeName from EmployeeMaster  where EmployeeNo='5412'", ConnCMS);
          
            ConnCMS.Open();
            StreamWriter tw = File.AppendText(physicalPath);
             SqlDataReader reader = com.ExecuteReader();
            if (!File.Exists(physicalPath))
            {
            while (reader.Read())
            {
                tw.Write(reader["EmployeeNo"].ToString());
                tw.Write(", " + reader["EmployeeName"].ToString());
                tw.WriteLine("---------------------------");

            }
             count++;
            tw.Close();
            reader.Close();
            ConnCMS.Close();
                 
               }                  
               
            }

请帮助我......

please help me ......

推荐答案

通过将If Condition更改为此:-
try by changing If Condition to this:-
if (File.Exists(physicalPath))
 {
     // same code..
 }



希望它能工作..



hope it works..


这篇关于在Windows应用程序.net C#中将记录写入txt文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆