ASP.NET Tutorial/ADO.net Database/OleDbConnection

Материал из .Net Framework эксперт
Версия от 11:56, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Connecting to an Access Database

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>
<%@ Page debug="true" %>
<html>
  <head>
    <title>Connecting to an Access Database</title>
  </head>
  <body>
    <H3>Connecting to an Access Database</H3>
    <asp:DataGrid id="dgSuppliers" runat="server" />
  </body>
</html>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
  Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _
                        "data source=C:\northwind.mdb;"
  Dim objConnection As New OleDbConnection(strConnection)
  Dim strSQL As String = "SELECT SupplierID, CompanyName " & _
                         "FROM Suppliers;"
  Dim objCommand As New OleDbCommand(strSQL, objConnection)
  objConnection.Open()
  dgSuppliers.DataSource = objCommand.ExecuteReader()
  dgSuppliers.DataBind()
  objConnection.Close()
End Sub
</script>


Properties of OleDbConnection

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data.Odbc" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e) {
        OleDbConnection myOleDbConnection = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+ Server.MapPath("EmployeeDatabase.mdb;"));
        myOleDbConnection.Open();
    
        lblOleDbConnectionString.Text = myOleDbConnection.ConnectionString;
        lblOleDbConnectionTimeout.Text = Convert.ToString(myOleDbConnection.ConnectionTimeout);
        lblOleDbDatabase.Text = myOleDbConnection.Database;
        lblOleDbDataSource.Text = myOleDbConnection.DataSource;
        lblOleDbProvider.Text = myOleDbConnection.Provider;
        lblOleDbServerVersion.Text = myOleDbConnection.ServerVersion;
        lblOleDbState.Text = Convert.ToString(myOleDbConnection.State);
    
        myOleDbConnection.Close();
    }
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <table>
            <tbody>
                <tr>
                    <td>
                        ConnectionString: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbConnectionString" runat="server">ConnectionString</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        ConnectionTimeout: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbConnectionTimeout" runat="server">ConnectionTimeout</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        Database: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbDatabase" runat="server">Database</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        DataSource: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbDataSource" runat="server">DataSource</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        Provider: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbProvider" runat="server">Provider</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        ServerVersion: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbServerVersion" runat="server">ServerVersion</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        State: 
                    </td>
                    <td>
                        <asp:Label id="lblOleDbState" runat="server">State</asp:Label></td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>