生日之作-孤獨的程序員有數據相伴(數據庫的導入導出)

今天是我的生日,不過我沒有出去玩,在教室裏專心寫作業。今天我是特別的認真,因爲我已經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("數據表中沒有數據,沒有任何輸出的數據!");
 
                        }

                    }
                   
 
                }

            }
        }
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章