In fact, in most parts of SQL Server and Access, in addition to the connection statement, other definition statements are the difference between SQL ×× and OLEDB ××
Also, if the Access database is in the beginning of the ASPX file, you need to include the following statement: Use% @ import code to separate with% @ IMPORT code.
<% @ Import namespace = "system.data"%> <% @ import namespace = "system.data.oledb"%>
First look at the connection database of Access and open;
objConnection = new OleDbConnection (Provider = Microsoft.Jet.OleDb.4.0; Data Source = " Server.MapPath (" * mdb ").) // * is the name of the database OleDbConnection objConnection = new OleDbConnection (strConnection); objConnection.Open ();
Learning purpose: learn to read databases
Some netizens said, don't C #, in Asp.net, I choose VB.NET, I don't object, but I think it is a new thing, don't be in the intentions. In fact, C # is a general operation, just case sensitive and more than one semicolon, which is almost different from VB.NET. In the ASP, we read the data directly to use the Recordset, and then insert <% =%> in the HTML code, it can be displayed, but asp.net pays attention to code separation, if it is still displayed, it cannot code. So we have two ways: If you are reading a recorded data or not much data, we use DataReader to collect data, then assign the value to the Text property of the Label control; if you read a lot of data, we use DataGrid.
Today, let's talk about DataReader:
string strConnection = "Provider = Microsoft.Jet.OleDb.4.0; Data Source ="; strConnection = Server.MapPath (strDb); OleDbConnection objConnection = new OleDbConnection (strConnection); OleDbCommand objCommand = new OleDbCommand ( "SQL statement here" objConnection); objConnection.Open (); OleDbDataReader objDataReader = objCommand.ExecuteReader (); if (objDataReader.Read ()) {oicq.Text = Convert.ToString (objDataReader [ "useroicq"]); homesite.Text = Convert.ToString (ObjDataReader ["UserSite"]); face.selectedItem.text = Convert.TOString (ObjDataReader ["Userface"]);} You can see that we first connect the database and open, for the select command, we declare an OLEDBCommand Execute it, then discover an OLEDBDataReader to read data, use executeReader (), objDataReader.Read () begins to read, when the output, we have to pay attention to the Text property can only be a string, so We have to convert read data into strings. Learning purposes: Preliminarily master the use of DataGrid
Previously in ASP, we show that large quantity data often uses a loop read record set, inserted into the code in the table, the loop is
First look at the properties of DataGrid's style backImageurl = "" Background Image Cellspacing = "" Unit Ports CELLPADDING = "Unit Fill CSSClass =" "CSS Style used
DataGrid can be placed in the displayed head in the table in the table to represent the meaning of the representatives of each cell, with ShowHeader = "true / false" to control whether it is displayed, in most cases we don't need This feature, because the field name in our database is mostly in English, and we want most of the list of page outputs.
Let's take a look at all records in the database, a few lines of code: