您的位置:首页 > 编程语言

典型的服务器连接数据读取代码

2008-01-16 14:06 531 查看

using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


using System.Data.SqlClient;




public partial class _Default : System.Web.UI.Page




...{


SqlConnection sqlcon; //建立数据库连接变量


string strCon = "Data Source=.; database=db_04;uid=sa;pwd=hotsren"; //建立数据库连接字符变量


protected void Page_Load(object sender, EventArgs e)




...{




sqlcon = new SqlConnection(strCon); //连接数据库


string sqlstr = "select top 9* from tb_Member"; //建立数据库查询变量(查询最新的9条信息,在tb_Member表中


SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon); //建立数据库读取变量


DataSet myds = new DataSet(); //创建DataSet数据库变量


sqlcon.Open(); //打开数据库


myda.Fill(myds, "tb_Member"); //读取数据库,填充给myds,表tb_Member里的数值


DataList1.DataSource = myds; //指定Datalist的数据库连接为 dataset


DataList1.DataBind(); //绑定数据库


DataRowView mydrv = myds.Tables["tb_Member"].DefaultView[0]; //得到数据行


for (int i = 0; i <= DataList1.Items.Count - 1; i++) //循环




...{


Label lab = (Label)DataList1.Items[i].FindControl("Label1");//将DataList1中的Label1控件赋给前面的lab


DateTime dt = Convert.ToDateTime(mydrv["date"]);//将数据库中的时间赋给DateTime变量


lab.Text = dt.ToLongDateString();//将时间变量格式化后赋给lab


Label Name = (Label)DataList1.Items[i].FindControl("LabelName");//将DataList1中的Label1控件赋给前面的Name


string N = Convert.ToString(mydrv["name"]); //将数据库中的时间赋给N变量


Name.Text = N.ToString();//将姓名变量格式化后赋给Name


}


sqlcon.Close(); //关闭数据库


}


}

个人编写的两种放下如下:

第一种 利用DataAdapter实现
以下是DB.CS类中的数据连接参数


public static SqlConnection createcon()




...{


return new SqlConnection("server=(local);database=db_04; uid=sa;pwd=hotsren");


}

以下是主页中的代码:


protected void Page_Load(object sender, EventArgs e)




...{


if (!IsPostBack)




...{


SqlConnection con = DB.createcon(); //创建数据库连接变量


string cmd = "select * from tb_Member"; //创建数据库查找变量


DataSet mydb = new DataSet(); //创建本地数据库


con.Open(); //打开链接


SqlDataAdapter sqlAd = new SqlDataAdapter(cmd, con); //查询数据库表


sqlAd.Fill(mydb, "tb_Member"); //将表赋值于mydb


this.DropDownList1.DataSource = mydb; //指定数据库


this.DropDownList1.DataTextField = "photo";


this.DropDownList1.DataBind();


this.Image1.ImageUrl = DropDownList1.SelectedValue;


}




}


protected void DropDownList1_TextChanged(object sender, EventArgs e)




...{


this.Image1.ImageUrl = DropDownList1.SelectedValue;


}

第二种:利用datareader实现

以下是主页中的代码:


protected void Page_Load(object sender, EventArgs e)




...{


if (!IsPostBack)




...{


SqlConnection con = DB.createConnection(); //从类中导出数据库连接字符


con.Open(); //打开数据库


SqlCommand cmd = new SqlCommand("select * from tb_Member", con); //搜索数据库


SqlDataReader mydb = cmd.ExecuteReader();


this.DropDownList1.DataSource = mydb; //指定数据库


this.DropDownList1.DataTextField = "photo"; //制定值photo


this.DropDownList1.DataValueField = "photo"; //制定值photo


this.DropDownList1.DataBind(); //绑定


this.Image1.ImageUrl = DropDownList1.SelectedValue;


con.Close();


}


}


protected void DropDownList1_TextChanged(object sender, EventArgs e)




...{


this.Image1.ImageUrl = DropDownList1.SelectedValue;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: