ASP.Net/ADO.net Database/OleDbConnection
Содержание
Read data from OleDbConnection (VB.net)
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>
<script language="vb" runat="server">
Sub Page_Load()
Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
strConnection += "Data Source=Northwind.mdb"
data_src.text = strConnection
Dim strSQL as string = "SELECT FirstName, LastName FROM Employees"
Dim strResultsHolder as string
Dim objConnection as New OledbConnection(strConnection)
Dim objCommand as New OledbCommand(strSQL, objConnection)
Dim objDataReader as OledbDataReader
try
objConnection.Open()
con_open.text="Connection opened successfully.<br />"
objDataReader = objCommand.ExecuteREader()
Do While objDataReader.Read()=True
strResultsHolder +=objDataREader("FirstName")
strResultsHolder +=" "
strResultsHolder +=objDataREader("LastName")
strResultsHolder +="<br>"
Loop
objDataReader.Close()
objConnection.Close()
con_close.text="Connection closed.<br>"
divListEmployees.innerHTML = strResultsHolder
catch e as Exception
con_open.text="Connection failed to open successfully.<br>"
con_close.text=e.ToString()
end try
end Sub
</script>
<html>
<body>
<h4>Reading data from the connection
<asp:label id="data_src" runat="server"/> with the DataReader object.</h4>
<asp:label id="con_open" runat="server"/><br>
<div id="divListEmployees" runat="server">list will go here</div>
<asp:label id="con_close" runat="server"/><br>
</body>
</html>
Reading data from the OleDbConnection in C#
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script Language="c#" runat="server">
void Page_Load()
{
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source="+MapPath("EmployeeDatabase.mdb");
data_src.Text = strConnection;
string strSQL = "SELECT FirstName, LastName FROM Employee";
DataSet objDataSet = new DataSet();
OleDbConnection objConnection = new OleDbConnection(strConnection);
OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, objConnection);
objAdapter.Fill(objDataSet, "Employees");
DataView objDataView = new DataView(objDataSet.Tables["Employees"]);
dgNameList.DataSource=objDataView;
dgNameList.DataBind();
}
</script>
<html>
<body>
<h4>Reading data from the connection
<asp:label id="data_src" runat="server"/> to the DataGrid control.</h4>
<asp:datagrid id="dgNameList" runat="server" /><br />
</body>
</html>
<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>
Try to connect to access database through OleDbConnection (VB.net)
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Sub Page_Load()
Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
strConnection += "Data Source=C:\NonExistent.mdb"
data_src.text = strConnection
Dim objConnection as New OledbConnection(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>
Working with Transactions with OleDbConnection (VB.net)
<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OLEDB" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
Dim DBConn as OleDbConnection
Dim DBDelete As New OleDbCommand
Dim DBTrans As OleDbTransaction
DBConn = New OleDbConnection( _
"PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
& "DATA SOURCE=" _
& Server.MapPath("EmployeeDatabase.mdb;"))
DBConn.Open()
DBTrans = DBConn.BeginTransaction()
DBDelete.Connection = DBConn
DBDelete.Transaction = DBTrans
DBDelete.rumandText = "Delete From Employee " _
& "Where ID = 1"
DBDelete.ExecuteNonQuery()
DBDelete.rumandText = "Delete From Employee " _
& "Where ID = 2"
DBDelete.ExecuteNonQuery()
DBTrans.RollBack()
lblMessage.Text = "No action was taken."
"DBTrans.rumit()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Working with Transactions with an Access Database</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<BR><BR>
<asp:label
id="lblMessage"
runat="Server"
/>
</form>
</BODY>
</HTML>
<A href="http://www.nfex.ru/Code/ASPDownload/EmployeeDatabase.zip">EmployeeDatabase.zip( 10 k)</a>