日期:2014-05-18  浏览次数:20828 次

winfrom连接数据取值生成TXT文件
紧急啊 各位大大 不会winfrom 求教 winfrom怎么连接数据库取值在把放到TXT文件中 在生成TXT文件 现在公司要该个项目 我看先前的项目 完全看不懂 他的连接是手动输进去的 我现在在后台自己写一个 要怎么写怎么调用 跪求~~~

------解决方案--------------------
把你的数据写到DataTable里面,然后再写入txt,写入txt方法如下:
FileStream f=new FileStream("1.txt",FileMode.Create);
StreamWriter m=new StreamWriter(f);
m.Write("数据信息");
------解决方案--------------------
C# code
   /// <summary>
        /// 写入日志文件
        /// </summary>
        /// <param name="message">日志内容</param>
        public static void CreateFile(string message)
        {           
            string filepath = Application.StartupPath;
            if (!Directory.Exists(filepath + "/ErrorLog"))
            {
                Directory.CreateDirectory(filepath + "/ErrorLog");
            }
            if (!File.Exists(filepath + "/ErrorLog/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"))
            {
                FileStream FsCreate = new FileStream(filepath + "/ErrorLog/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Create);
                FsCreate.Close();
            }
            FileStream fs = new FileStream(filepath + "/ErrorLog/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(DateTime.Now.ToString() + "    " + message);
            sw.Close();
            fs.Close();
        }
    }

------解决方案--------------------
这个应该分解为两个问题:
1、连接数据库,把你需要的数据写到DataTable里。
2、循环DataTable,把数据写入txt
要用到的都是很基础的知识,不是很难......
要注意的是写入txt的格式,可识别、整齐规范