比如aspx文件里是这样写的:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db_travlConnectionString %>" SelectCommand="SELECT * FROM [Places] where id=@id">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="pid" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
就是简单的用gridview显示数据,现在我想在.cs文件写该怎么样写呢??谢谢!!!
參考答案:SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[\"db_travlConnectionString\"].ConnectionString);
SqlCommand command = new SqlCommand(\"SELECT * FROM [Places] WHERE id=\'pid\'\", conn);
conn.Open();
Gridview1.DataSource = command.ExecuteReader();
Gridview1.DataBind();
conn.Close();
在.cs文件的头部写上,using System.Data.SqlClient;
引号显示的有点不正常,这个是可以用的。