void Page_Load(Object sender, EventArgs e)
{
// declare a string to hold the results as an HTML table
string strResult = "<table>"; // get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.JetConnectionString;
outConnect.InnerText = strConnect; // and display it // specify the SELECT statement to extract the data
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'";
outSelect.InnerText = strSelect; // and display it try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect); // open the connection to the database
objConnect.Open(); // create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect); // declare a variable to hold a DataReader object
OleDbDataReader objDataReader; // execute the SQL statement against the command to fill the DataReader
objDataReader = objCommand.ExecuteReader(); // iterate through the records in the DataReader getting field values
// the Read method returns False when there are no more records
while (objDataReader.Read())
{
strResult += "<tr><td>" + objDataReader["ISBN"] + "</td><td> "
+ objDataReader["Title"] + "</td><td> "
+ objDataReader["PublicationDate"] + "</td></tr>";
} // close the DataReader and Connection
objDataReader.Close();
objConnect.Close();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
} // add closing table tag and display the results
strResult += "</table>";
outResult.InnerHtml = strResult; }web.config: <appSettings>
<add key="DsnNorthwind" value="server=.\NetSDK; database=Northwind; Trusted_Connection=yes"/>
<add key="DsnPubs" value="server=.\NetSDK; database=pubs; Trusted_Connection=yes"/>
<add key="DsnIBA" value="server=.\NetSDK; database=IBuyAdventure; Trusted_Connection=yes"/>
<add key="DsnIBAOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=IBuyAdventure; Trusted_Connection=yes"/>
<add key="DsnWroxBooksSql" value="data source=killerwc; initial catalog=me; uid=me; pwd=sophie.wang"/>
<add key="DsnWroxBooksOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=WroxBooks; Trusted_Connection=yes"/>
<add key="DsnWroxBooksJet" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="/>
</appSettings>
{
// declare a string to hold the results as an HTML table
string strResult = "<table>"; // get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.JetConnectionString;
outConnect.InnerText = strConnect; // and display it // specify the SELECT statement to extract the data
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'";
outSelect.InnerText = strSelect; // and display it try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect); // open the connection to the database
objConnect.Open(); // create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect); // declare a variable to hold a DataReader object
OleDbDataReader objDataReader; // execute the SQL statement against the command to fill the DataReader
objDataReader = objCommand.ExecuteReader(); // iterate through the records in the DataReader getting field values
// the Read method returns False when there are no more records
while (objDataReader.Read())
{
strResult += "<tr><td>" + objDataReader["ISBN"] + "</td><td> "
+ objDataReader["Title"] + "</td><td> "
+ objDataReader["PublicationDate"] + "</td></tr>";
} // close the DataReader and Connection
objDataReader.Close();
objConnect.Close();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
} // add closing table tag and display the results
strResult += "</table>";
outResult.InnerHtml = strResult; }web.config: <appSettings>
<add key="DsnNorthwind" value="server=.\NetSDK; database=Northwind; Trusted_Connection=yes"/>
<add key="DsnPubs" value="server=.\NetSDK; database=pubs; Trusted_Connection=yes"/>
<add key="DsnIBA" value="server=.\NetSDK; database=IBuyAdventure; Trusted_Connection=yes"/>
<add key="DsnIBAOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=IBuyAdventure; Trusted_Connection=yes"/>
<add key="DsnWroxBooksSql" value="data source=killerwc; initial catalog=me; uid=me; pwd=sophie.wang"/>
<add key="DsnWroxBooksOleDb" value="provider=SQLOLEDB.1;data source=.\NetSDK;initial catalog=WroxBooks; Trusted_Connection=yes"/>
<add key="DsnWroxBooksJet" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="/>
</appSettings>
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = sample.mdb " ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
myConn.Open ( ) ;
string strDele = "DELETE FROM books WHERE bookid= " + t_bookid.Text ;
OleDbCommand myCommand = new OleDbCommand ( strDele , myConn ) ;
//从数据库中删除指定记录
myCommand.ExecuteNonQuery ( ) ;
//从DataSet中删除指定记录信息
myDataSet.Tables [ "books" ] . Rows [ myBind.Position ] . Delete ( ) ;
myDataSet.Tables [ "books" ] . AcceptChanges ( ) ;
myConn.Close ( ) ;
//从数据库中修改指定记录
string strUpdt = " UPDATE books SET booktitle = '"
+ t_booktitle.Text + "' , bookauthor = '"
+ t_bookauthor.Text + "' , bookprice = "
+ t_bookprice.Text + " , bookstock = "
+ t_bookstock.Text + " WHERE bookid = " + t_bookid.Text ;OleDbCommand myCommand = new OleDbCommand ( strUpdt , myConn ) ;
myCommand.ExecuteNonQuery ( ) ;
myConn.Close ( ) ;