ASP.Net/ADO.net Database/SqlConnection — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:30, 26 мая 2010

Catch data binding exception (VB.net)

<%@ 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>
        <!-- Insert content here -->
    </form>
</body>
</html>



Display of Data in a Table (Grid) Using SQL Objects (VB.net)

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<html>
<body>
<h2>Display of Data in a Table (Grid) Using SQL Objects</h2>
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>



Open Sql connection (VB.net)

  <%@ 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.<br />"
      objConnection.Close()
      con_close.text="Connection closed.<br />"
    catch e as Exception
      con_open.text="Connection failed to open.<br />"
      con_close.text=e.ToString()
    end try
  end Sub
</script>
<html>
  <body>
  <h4>Testing the data connection 
  <asp:label id="data_src" runat="server"/></h4>
  <asp:label id="con_open" runat="server"/><br />
  <asp:label id="con_close" runat="server"/><br />
  </body>
</html>



Setup SqlConnection (C#)

<%@ 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.<br />";
      objConnection.Close();
      con_close.Text="Connection closed.<br />";
    }
    catch (Exception e)
    {
      con_open.Text="Connection failed to open.<br />";
      con_close.Text=e.ToString();
    }
  }
</script>
<html>
  <body>
  <h4>Testing the data connection
  <asp:label id="data_src" runat="server"/></h4>
  <asp:label id="con_open" runat="server"/><br />
  <asp:label id="con_close" runat="server"/><br />
  </body>
</html>



Use Trace to debug (VB.net)

<%@ 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>
        <!-- Insert content here -->
    </form>
</body>
</html>