生日之作-孤独的程序员有数据相伴(数据库的导入导出)

来源:互联网 发布:上能电气怎么样 知乎 编辑:程序博客网 时间:2024/06/05 18:53

今天是我的生日,不过我没有出去玩,在教室里专心写作业。今天我是特别的认真,因为我已经20周岁了,好好学习。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;

namespace ADO.NET1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string constr = "data source=.;initial catalog=UserBD1;User id=sa;password=yqq";
            using (SqlConnection con = new SqlConnection(constr))//数据库的链接链接对象
            {
                string sql = "select * from T_Users";
                using (SqlCommand cmd = new SqlCommand(sql, con))//数据库的操作对象
                {
                    con.Open();
                    using (SqlDataReader reader= cmd.ExecuteReader())
                    {
                        //判断是否查询数据
                        if (reader.HasRows)
                        {
                            //如果是真,有数据被查询出

                            //当有数据的时候就创建文本文件,并向其中写入数据
                            //用流的形式来创建
                            using (StreamWriter swf = new StreamWriter(@"E:\tblUser.txt"))
                            {
                                while (reader.Read())
                                {
                                    object objFuserName = reader.GetValue(1);
                                    object objFpassword = reader.GetValue(2);
                                    string line = string.Format("{0}--{1}", objFuserName, objFpassword);
                                    swf.WriteLine(line);
                                }
                                Response.Write("导出完毕!");

                            }


                        }
                        else
                        {
                            Response.Write("数据表中没有数据,没有任何输出的数据!");
 
                        }

                    }
                   
 
                }

            }
        }
    }
}

 

 

原创粉丝点击