ASP.Net/ADO.net Database/SqlDataReader
Содержание
Bind SqlDataReader to GridView
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"> </asp:GridView>
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class Default_aspx : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { string connectionString = "Data Source=LocalHost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa_0001"; string commandString = "Select * from Customers"; SqlConnection conn = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(commandString); conn.Open(); command.Connection = conn; SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); GridView1.DataSource = reader; GridView1.DataBind(); }
}
</source>
Get data from SqlDataReader
<source lang="csharp">
<%@ Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html>
<title>Try-Catch-Finally Example</title> <head> <script runat="server"> Sub Page_Load() " Dim ConnStr As String = "Data Source=(local)\NetSDK;" & _ " "Initial Catalog=Pubs;Trusted_Connection=True;" Dim ConnStr As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";" Dim SQL As String = "SELECT ID, FirstName FROM Employee " & _ "WHERE ID IS NOT NULL" Dim PubsConn As New SqlConnection(ConnStr) Dim TitlesCmd As New SqlCommand(SQL, PubsConn) Dim Titles As SqlDataReader Try PubsConn.Open() Titles = TitlesCmd.ExecuteReader()Output.Text = "
" & Titles.GetString(0) & " | " " Output.Text &= "$" & _ " Format(Titles.GetDecimal(1), "##0.00") & " | " Output.Text &= "
Catch sqlEx As SqlException Response.Write("A SqlException has occurred.") Catch ex As Exception Response.Write("An Exception has occurred.") Finally If Not Titles Is Nothing Then If Not Titles.IsClosed Then Titles.Close() End If End If If PubsConn.State = ConnectionState.Open Then PubsConn.Close() End If End Try Response.Write("
The current connection state is: " & _ PubsConn.State.ToString() & ".") End Sub </script> </head>
<body>
SqlDataReader Example
<asp:label id="Output" runat="server"/>
</body> </html>
</source>
SqlDataReader Demo
<source lang="csharp">
<%@ Import namespace="System.Data" %> <%@ Import namespace="System.Data.SqlClient" %> <html>
<head> <title>Data Reader</title> </head> <body>
Data Reader
Total Products listed = <asp:DataGrid id="dgCount" runat="server" BorderWidth=0 ShowHeader=False />
<asp:DataGrid id="dgData" runat="server" /> </body>
</html> <script language="VB" runat="server"> Sub Page_Load(Source As Object, E As EventArgs)
Dim strConnection As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";" Dim objConnection As New SqlConnection(strConnection) objConnection.Open() Dim strSQLcount As String strSQLcount = "SELECT COUNT(FirstName) FROM Employee" Dim objCommand As New SqlCommand(strSQLcount, objConnection) Dim objReader As SqlDataReader = objCommand.ExecuteReader() dgCount.DataSource = objReader dgCount.DataBind() objReader.Close() Dim strProcName As String strProcName = "SELECT * FROM Employee" objCommand.rumandText = strProcName objReader = objCommand.ExecuteReader() dgData.DataSource = objReader dgData.DataBind() objConnection.Close()
End Sub </script>
</source>