Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Document of Ado.net

Document of Ado.net

Ratings: (0)|Views: 60|Likes:
Published by Hari Kishan Reddy

More info:

Published by: Hari Kishan Reddy on Mar 05, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Ado.NetHari K Reddy1
ASP.NET & Databases
: Part 1Introduction: No matter what I try, getting your head around a dataset it not easy, there are so many methods and properties! This series will hopefully provide you with some knowledge of ASP.NET's workings with databases.I'm not going to cover everything as I could probably do a book on the subject, but I'll get you into it. This serieswill span several parts that I will release over a period of days, It will cover -Managed ProvidersDatasetsViewing dataXMLManaged Providers (?)If your new, your probably asking - What's a 'Managed Provider'?Well they are simply the a way to connect and retrieve data, like the ADODB Connection, Commandand all that, but a lot more. Managed Providers come in two flavors - Vanilla and Chocolate (OLEDB and SQLServer (version 7 and above)), I like chocolate the best.The reason behind having two separate providers is that Microsoft thinks that its SQL Server can domuch better with its own provider and connection (the provider uses the tabular-data format which SQL Server uses), there has been hinting of other companies developing their own for ASP.NET but I haven't seen it, so their stuck with OLEDB (which is better than I give it credit for.For the purpose of these demo's I'll do it in OLEDB (it's not that different, and I've already got a SQL onesomewhere), but in real life, I'd be using SQL.  Namespaces For all the demo's you'll need the following namespaces -<%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.Oledb" %>Connection: To connect to a database, you now use the OleDbConnection like this -Dim objConn as New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0; DataSource=e:\sff\site\db\users.mdb")As you can see it takes the connection string as a parameter, this is quite simple. Of course, you then have toopen it with the open() method.objConn.Open()Command: Once you've got a connection to the database, you can then send commands to it. TheOleDbCommand object allows you to send commands to the database. You enter a SQL statement anddepending on how you execute it (it has several execute commands) it can do almost anything with the data.Dim objCmd as New OleDbCommand("SELECT * From users", objConn)As you can see, its simple, it takes in a SQL String and the name of a connection object to use. You can also do itthis way -
Ado.NetHari K Reddy2
Dim objCmd as New OleDbCommand()'Later onobjCmd.Connection = objConnobjCmd.CommandText = "SELECT * FROM users"'You could even do it like this -Dim objCmd as New OleDbCommand(SQL String, connection string)We won't worry about these, they do exactly the same thing with the last one you provide a connection stringinstead of a connection object.At this stage, we haven't executed the statement yet and there are numerous ways to do this.ExecuteNonQuery: This is the way you execute when the string isn't going to return any data, like an INSERT or UPDATE SQL string.objCmd.ExecuteNoQueryIt does its job and that's it.ExecuteReader: If you've got a data reader Dim objRd as OleDbDataReader objRd = objCmd.ExeuteReader ExecuteScalar: Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from adatabase.I've been using the OleDbCommand to insert things into a database, it's quite easy actually.Data Reader: The OleDbDataReader is a way to read data, not write or update, but read. It streams the data(unlike a dataset) so is a bit limited to use, but for very simple data reading (i.e. display of data) its perfect. Dim objReader as OleDbDataReader objReader = objCmd.ExecuteReader While objReader.ReadResponse.Write(objReader.GetString(0) & "<br>")End WhileThe top one shows you what we've already done before - Using the Command to fill the Data Reader.The bottom one displays the data for us. The Read method keeps on reading each line of the results and stopswhen there are no more. objReader.GetString(0) tells it to get the string version of the first column of the currentrecord (columns start a 0).Here is a list of the Get[thing](index).GetBoolean(x)GetByte(x) - Returns it as a Byte valueGetBytes(x) - Returns it as a Byte arrayGetChar(x) - Returns char valueGetChars(x) - Returns char array
Ado.NetHari K Reddy3
GetDataTypeName(x) - Returns the data type of the specified columnGetDateTime(x)GetDecimal(x)GetDefaultStream(x) - Returns a Stream objectGetDouble(x)GetFieldType(x) - Get the Type that is the data type of the objectGetFloat(x)GetGuid(x) - Retunrs the value as a Globally Unique Idetifier Value (GUID)GetInt16(x)GetInt32(x)GetInt64(x)GetName(x) - Returns the name of the columnGetOrdinal(name) - Returns the column index as given by the column nameGetString(x)GetTimeSpan(x)GetValue(x) - Returns the data in its native formatGetValues(values()) - Returns all attributes for the column and places them in values() Now that is a lot of methods just for getting a value, they take up about 90% of all of the Data Reader's methods.I've put a small explanation on the ones that aren't that obvious. Data Adapter: The OleDbDataAdapter is the thing that takes your data and provides an interface between thedata and the dataset. You create it like all of the others - Dim objAdapter as New OleDbDataAdapter("SELECT * FROM users", objConn)Seems like the OleDbCommand but its not. You can use this Data Adapter for things like Filling a dataset,Updating the data source with data from the dataset and using SQL commands on the dataset. I'll take youthrough some of these now. Dim ds as Dataset = New DataSet()objAdapter.Fill(ds, "users")This method (fill) takes in the name of the dataset and the table in the dataset to put it into. The table is created if it doesn't exist and then populates it with the data that it got from the database using the query it sent.Mappings: Mapping allows you to create an alias for the names of columns in the dataset. The names don't affectthe data source and when sending through the DataAdapter are automatically converted. objAdapter.TableMappings.Add("adbtable", "users")With objAdapter.TableMappings(0).ColumnMappings.Add("PID", "ID").Add("LastName", "LName").Add("StreetAddress", "Addy")End WithobjAdapter.Fill(ds) Now when you call the dataset you don't have to use ds.Tables("adbtable")

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->