1. what error did you get?2. >>>> string connectionString = "server=\'localhost\'; user id=\'sa\'; password=\'fing\'; Database=\'test\'";is sa and fing valid login/password for the sql server? you do not need "'": string connectionString = "server=localhost; uid=sa; pwd=fing; Database=test;"; 3. why System.Data.DataSet MyQueryMethod() is inside Page_Load()?try<%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server"> void Page_Load(object sender, EventArgs e) {
改成
DataGrid1.DataSource=rs.Tables[0].DefaultView;
string connectionString = "server=localhost; uid=sa; pwd=fing; Database=test;";
3. why System.Data.DataSet MyQueryMethod() is inside Page_Load()?try<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server"> void Page_Load(object sender, EventArgs e) {
DataSet rs=MyQueryMethos();
DataGrid1.DataSource=rs.Tables[0].DefaultView;
DataGrid1.DataBind();
} System.Data.DataSet MyQueryMethod() { string connectionString = "server=localhost; uid=sa; pwd=fing; Database=test;";
string queryString = "SELECT [test].[id], [test].[username], [test].[password] FROM [test]";
SqlDataAdapter dataAdapter = new SqlDataAdapter(queryString, connectionString); DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Simple Data Report
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="DataGrid1" runat="server" CellSpacing="1" GridLines="None" CellPadding="3" BackColor="White" ForeColor="Black" EnableViewState="False">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</form>
</body>
</html>