databind()

sunshinebailin 2008-06-26 10:14:56
简单阐述一下用途,给一个简单易懂的例子。
...全文
287 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lishunli86 2009-08-13
  • 打赏
  • 举报
回复
ws_hgo:
w=wo
s=shi
h=hao
go=?(人)
xfanben 2009-06-09
  • 打赏
  • 举报
回复
wsg_hgo好人。。很显然。
sunshinebailin 2008-06-27
  • 打赏
  • 举报
回复
小弟谦虚接受大家的意见,其实英文是我的强项,wsg_hgo的解释很明白,在此谢谢了,欢迎大家做朋友,我正在学asp.net,
以后大家可以互相帮助。
ws_hgo 2008-06-26
  • 打赏
  • 举报
回复
如果这你都看不懂的话
我就不知道怎么跟你说了.....
LZ可结贴了..............
^_^
ws_hgo 2008-06-26
  • 打赏
  • 举报
回复
 if(!IsPostBack)
{

// sql语句查询信息
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";
//调用GetDate方法(将sql语句作为参数传进去),返回一个DataSet
DataSet ds = GetData(queryString);
//如果ds中存在信息的话
if (ds.Tables.Count > 0)
{
//就把数据给GridView这个控件,就是给这个空间指定数据源
AuthorsGridView.DataSource = ds;
//指定了数据源之后,就是要将信息显示出来的
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}

}
DataSet GetData(String queryString)
{
//这个是webconfig中的配置与数据库连接相等于(server=localhost;uid=sa;pwd=sa;database=数据库名)
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;

DataSet ds = new DataSet();

try
{
//与数据库建立连接
SqlConnection connection = new SqlConnection(connectionString);
创建一个暑假适配器(支持增加,删除,查询,修改操作)
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
//此例的目的是查询信息,将查询的信息给ds
// Fill the DataSet.
adapter.Fill(ds);

}
catch(Exception ex)
{

// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";

}

return ds;

}
ws_hgo 2008-06-26
  • 打赏
  • 举报
回复
4楼说的
很好啊!
一看就懂的啊!
LZ
需要更加努力啊!
我姓区不姓区 2008-06-26
  • 打赏
  • 举报
回复
上面不是有注释么……
sunshinebailin 2008-06-26
  • 打赏
  • 举报
回复
我知道它是一个绑定数据源的方法,但是具体如何使用,我不清楚,谁能给我一个简单的例子,楼上的代码那么多,关键地方可以
帮我注释一下吗?理解起来不费劲。
我姓区不姓区 2008-06-26
  • 打赏
  • 举报
回复

<%@ Page language="C#" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

void Page_Load(Object sender, EventArgs e)
{

// This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the
// page is first loaded. Thereafter, the values are
// stored in view state.
if(!IsPostBack)
{

// Declare the query string.
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";

// Run the query and bind the resulting DataSet
// to the GridView control.
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}

}

}

DataSet GetData(String queryString)
{

// Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;

DataSet ds = new DataSet();

try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

// Fill the DataSet.
adapter.Fill(ds);

}
catch(Exception ex)
{

// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";

}

return ds;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView DataBind Example</title>
</head>
<body>
<form id="form1" runat="server">

<h3>GridView DataBind Example</h3>

<asp:label id="Message"
forecolor="Red"
runat="server"/>

<br/>

<asp:gridview id="AuthorsGridView"
autogeneratecolumns="true"
runat="server">
</asp:gridview>

</form>
</body>
</html>
amandag 2008-06-26
  • 打赏
  • 举报
回复
楼主的描述过于简略了

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧