数据库查询各种方法
namespace SubScribe
![]() ![]() {
![]() /**//**//**//// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class SubScribe : System.Web.UI.Page
![]() {
protected System.Web.UI.WebControls.DataGrid dataGrid;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
![]() {
// 在此处放置用户代码以初始化页面
BindData();
}
public void BindData()
![]() {
try
![]() {
string cnString=ConfigurationSettings.AppSettings["Northwind"];
SqlConnection cn=new SqlConnection(cnString);
cn.Open();
![]()
string strQuery = "select CustomerID,CompanyName,Phone from Customers ";
SqlCommand cmd=new SqlCommand(strQuery,cn);
![]()
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read ())
![]() {
Label1.Text=Label1.Text+reader["CustomerID"].ToString().Trim ()+"****";
}
![]()
int i;
string cnString=ConfigurationSettings.AppSettings["Northwind"];
SqlConnection cn=new SqlConnection(cnString);
cn.Open();
![]()
string strQuery = "select count(*) from Customers ";
SqlCommand cmd=new SqlCommand(strQuery,cn);
![]()
i=Convert.ToInt32(cmd.ExecuteScalar());
Label1.Text=Convert.ToString(i);
cn.Close();
![]()
![]()
string cnString=ConfigurationSettings.AppSettings["Northwind"];
SqlConnection cn=new SqlConnection(cnString);
![]() /**//**//**///// cn.Open();
![]()
string strQuery = "select CustomerID,CompanyName,ContactName,Phone from Customers ";
SqlDataAdapter adapter=new SqlDataAdapter(strQuery,cn);
![]()
DataSet MyDataSet=new DataSet();
adapter.Fill(MyDataSet,"Customers");
DataView source=new DataView(MyDataSet.Tables ["Customers"]);
dataGrid.DataSource=source;
dataGrid.DataBind();
cn.Close();
![]()
![]()
}
catch
![]() {
Response.Write("<Script>window.alert('查询数据库失败!')</Script>");
}
}
![]()
![]()
![]() Web 窗体设计器生成的代码Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![]() {
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit (e);
}
![]() /**//**//**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![]() {
this.dataGrid.SelectedIndexChanged += new System.EventHandler (this.dataGrid_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
![]()
}
#endregion
![]()
private void dataGrid_SelectedIndexChanged(object sender, System.EventArgs e)
![]() {
}
}
}
![]()
posted on
2006-08-25 22:31
^Eric^
阅读( 190)
评论()
收藏
举报
|