ASP.Net/ADO.net Database/SqlConnection
Содержание
Catch data binding exception (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.Sqlclient" %> <script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs) Dim conn As New SqlConnection("Server=foo;Database=pubs;Trusted_Connection=true") Dim cmd As New SqlCommand("select * from authors", conn) Try Conn.Open() DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataBind() Catch ex As Exception Label1.Text = "Could not connect to the database - " & _ "please try again later." End Try End Sub
</script> <html> <head> </head> <body>
<form runat="server"> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button> <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid> <asp:Label id="Label1" runat="server">Label</asp:Label> </form>
</body> </html>
</source>
Display of Data in a Table (Grid) Using SQL Objects (VB.net)
<source lang="csharp">
<%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.SqlClient" %> <html> <body>
Display of Data in a Table (Grid) Using SQL Objects
Northwind Employees: <asp:datagrid id="dgrEmployees" runat="server" /> <script language="vb" runat="server">
Sub Page_Load() Dim strSQL as string = "SELECT FirstName,LastName FROM Employees;" Dim strConnection as String = "server=EWANB;database=Northwind; User Id=sa;password=;" Dim objDataSet As New DataSet() Dim objConnection As New SqlConnection(strConnection) Dim objDataAdapter As New SqlDataAdapter(strSQL, objConnection) objDataAdapter.Fill(objDataSet, "Employees") Dim objDataView As New DataView(objDataSet.Tables("Employees")) dgrEmployees.DataSource = objDataView dgrEmployees.DataBind() End Sub
</script> </body> </html>
</source>
Open Sql connection (VB.net)
<source lang="csharp"> <%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %> <script language="VB" runat="server">
Sub Page_Load() " Dim strConnection as String = "user id=sa;password=;" "strConnection += "database=northwind;server=EWANB;" "strConnection += "Connect Timeout=30" Dim strConnection as String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password=password;" data_src.text = strConnection Dim objConnection as New SqlConnection(strConnection) try objConnection.Open() con_open.text="Connection opened successfully.
" objConnection.Close() con_close.text="Connection closed.
" catch e as Exception con_open.text="Connection failed to open.
" con_close.text=e.ToString() end try end Sub
</script> <html>
<body>
Testing the data connection <asp:label id="data_src" runat="server"/>
<asp:label id="con_open" runat="server"/>
<asp:label id="con_close" runat="server"/>
</body>
</html>
</source>
Setup SqlConnection (C#)
<source lang="csharp">
<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <script Language="c#" runat="server">
void Page_Load() {
// string strConnection = "user id=sa;password=;";
// strConnection += "initial catalog=northwind;data source=EWANB;"; //strConnection += "Connect Timeout=30"; string strConnection = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=nfexuser;Password="password";"; data_src.Text = strConnection; SqlConnection objConnection = new SqlConnection(strConnection); try { objConnection.Open(); con_open.Text="Connection opened successfully.
"; objConnection.Close(); con_close.Text="Connection closed.
"; } catch (Exception e) { con_open.Text="Connection failed to open.
"; con_close.Text=e.ToString(); } }
</script> <html>
<body>
Testing the data connection <asp:label id="data_src" runat="server"/>
<asp:label id="con_open" runat="server"/>
<asp:label id="con_close" runat="server"/>
</body>
</html>
</source>
Use Trace to debug (VB.net)
<source lang="csharp">
<%@ Page Language="VB" Debug="true" Trace="true" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.Sqlclient" %> <script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs) Trace.Write ("Click", "Start") Dim conn As New _ SqlConnection("Server=foo;Database=pubs;Trusted_Connection=true") Dim cmd As New SqlCommand("select * from authors", conn) Try Trace.Write("Opening connection") Conn.Open() DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataBind() Catch ex As Exception Trace.Warn(ex.Message) Label1.Text = "Could not connect to the database - " & _ "please try again later." End Try End Sub
</script> <html> <head> </head> <body>
<form runat="server"> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button> <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid> <asp:Label id="Label1" runat="server">Label</asp:Label> </form>
</body> </html>
</source>