c#读写txt文本文档

写入文档的例子

class WriteTextFile
    {
        static void Main()
        {
            //如果文件不存在,则创建;存在则覆盖
            //该方法写入字符数组换行显示
            string[] lines = { "first line", "second line", "third line","第四行" };
            System.IO.File.WriteAllLines(@"C:\testDir\test.txt", lines, Encoding.UTF8);

            //如果文件不存在,则创建;存在则覆盖
            string strTest = "该例子测试一个字符串写入文本文件。";
            System.IO.File.WriteAllText(@"C:\testDir\test1.txt", strTest, Encoding.UTF8);

            //在将文本写入文件前,处理文本行
            //StreamWriter一个参数默认覆盖
            //StreamWriter第二个参数为false覆盖现有文件,为true则把文本追加到文件末尾
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\testDir\test2.txt",true))
            {
                foreach (string line in lines)
                {
                    if (!line.Contains("second"))
                    {
                        file.Write(line);//直接追加文件末尾,不换行
                        file.WriteLine(line);// 直接追加文件末尾,换行   
                    }
                }
            }
        }
    }

 自己写的在 windows mobile 6.1里面测试

        #region 插入txt\
        string filename = "test.txt";

        private void writetxt(EPC txtepc)
        {
            using (StreamWriter sw = new StreamWriter(filename, true))//为false替换文本里的内容 true另起一行
            {
                sw.WriteLine(txtepc.id, txtepc.count);
            }
        }



        #endregion

  读取txt文本文档里的内同

    class ReadTextFile
    {
        static void Main()
        {
            //直接读取出字符串
            string text = System.IO.File.ReadAllText(@"C:\testDir\test1.txt");
            Console.WriteLine(text);

            //按行读取为字符串数组
            string[] lines = System.IO.File.ReadAllLines(@"C:\testDir\test.txt");
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }

            //从头到尾以流的方式读出文本文件
            //该方法会一行一行读出文本
            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\testDir\test.txt"))
            {
                string str;
                while ((str = sr.ReadLine()) != null)
                {
                    Console.WriteLine(str);
                }
            }
            Console.Read();
        }
    }

吧文本文档里的提交交到数据库里面

  private void tj_Click(object sender, EventArgs e)
        {
            using (StreamReader reader = new StreamReader(filename))
            {

                using (SqlConnection con = new SqlConnection("server=192.168.1.247;user id=sa;pwd=guanke2802;database=seuic"))//建立连接实例
                {

                           con.Open();
                           SqlCommand cmd = new SqlCommand();
                            string str;
                            while ((str = reader.ReadLine()) != null)
                            {


                                StringBuilder strsql = new StringBuilder();//定义对象实例

                                strsql.Append("insert into biaoqian(id)"); //定义插入语句
                                strsql.Append("values(");
                                strsql.Append("'" + str + "'");
                                //strsql.Append("'" + count.ToString() + "'");
                                strsql.Append(")");


                                //string sql = string.Format("Select top 1 * from biaoqian where id='{0}'", id);


                                


                               
                                cmd.CommandText = strsql.ToString();
                                cmd.Connection = con;

                                cmd.ExecuteNonQuery();//执行sql语句   







                            }
                  }  


            }


        }

 

 

 

转载于:https://www.cnblogs.com/luckystar2013/archive/2013/04/22/3036755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值