Mega Code Archive

 
Categories / ASP.Net Tutorial / ADO Net Database
 

Execuate select command by using the SqlCommand

<%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server">     void Page_Load(object sender, EventArgs e) {         string ConnectionString = ConfigurationSettings.AppSettings["MSDEConnectString"];         string CommandText = "select PublisherID, PublisherName, PublisherCity, PublisherWebsite FROM Publisher ORDER BY PublisherID";              SqlConnection myConnection = new SqlConnection(ConnectionString);         SqlCommand myCommand = new SqlCommand(CommandText, myConnection);              myConnection.Open();              DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);         DataGrid1.DataBind();     } </script> <html> <head> </head> <body>     <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> File: Web.config <configuration>     <appSettings>         <add key="MSDEConnectString" value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />     </appSettings> </configuration>