日期:2014-05-20  浏览次数:20408 次

ASP.NET 连接数据库的问题
我是刚学ASP.NET,       请问如何从SQL2000的数据库表中取一条记录显示到页面。

我是用VS2005的   ASP.NET     C#.    

看的书是Programming   ASP.NET   第三版,       里面控件只是把整个表搬到页面,     但我不知道,     如只显示一条记录(何弱水三千,     只取一瓢)。

------解决方案--------------------
给你一个DATAGRID的例子

mouseOverRowColor.aspx页面代码
<%@ Page language= "c# " Codebehind= "mouseOverRowColor.aspx.cs " AutoEventWireup= "false " Inherits= "CommonFunction.mouseOverRowColor " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> mouseOverRowColor </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id= "Form1 " method= "post " runat= "server ">
<h2> 鼠标移到DataGrid的那行则更改那行的颜色 </h2>
<asp:DataGrid id= "dgMouseOver " style= "Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 48px "
runat= "server " BorderColor= "#CC9966 " BorderStyle= "None " BorderWidth= "1px " BackColor= "White "
CellPadding= "4 ">
<SelectedItemStyle Font-Bold= "True " ForeColor= "#663399 " BackColor= "#FFCC66 "> </SelectedItemStyle>
<ItemStyle ForeColor= "#330099 " BackColor= "White "> </ItemStyle>
<HeaderStyle Font-Bold= "True " ForeColor= "#FFFFCC " BackColor= "#990000 "> </HeaderStyle>
<FooterStyle ForeColor= "#330099 " BackColor= "#FFFFCC "> </FooterStyle>
<PagerStyle HorizontalAlign= "Center " ForeColor= "#330099 " BackColor= "#FFFFCC "> </PagerStyle>
</asp:DataGrid>
</form>
</body>
</HTML>

mouseOverRowColor.aspx.cs后台代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace CommonFunction
{
/// <summary>
/// mouseOverRowColor 的摘要说明。
/// </summary>
public class mouseOverRowColor : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMouseOver;

private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataGridDataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgMouseOver.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.changeRowColor);