Datagrid数据连接问题
想将SQL的数据添加进datagrid中请问有几种方法,控件,对象。有原码最好,我功力太差了。 问题点数:20、回复次数:1Top
1 楼heriying(羽梦轻邻)回复于 2005-12-08 16:27:56 得分 20
可在查阅MSDN,,这里里面现在的@_@:
[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
// Create a connection to the "pubs" SQL database located on the
// local computer.
SqlConnection myConnection = new SqlConnection("server=localhost;" +
"database=pubs;Trusted_Connection=Yes");
// Connect to the SQL database using a SQL SELECT query to get all
// the data from the "Authors" table.
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT " +
" * FROM Authors", myConnection);
// Create and fill a DataSet.
DataSet ds = new DataSet();
myCommand.Fill(ds);
// Bind MyDataGrid to the DataSet. MyDataGrid is the ID for the
// DataGrid control in the HTML section.
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source ;
MyDataGrid.DataBind();
}
</script>
<body>
<%-- Display the DataGrid information in the body of the page. --%>
<h3><font face="Verdana">Simple SELECT to a DataGrid Control
</font></h3>
<%-- Display the data in a DataGrid. --%>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/>
</body>
</html>
Top




