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