Академический Документы
Профессиональный Документы
Культура Документы
0
Aids in database connectivity
Role of Microsoft.Net
It has provided classes that aid in the database connectivity. All these classes are located in the System.Data namespace. These classes perform the following tasks Communication with the database Storing the data in the database Fetching the data in the database Updating the data in the database These classes makes the core data access objects in .NET Framework These are collectively known as ADO.NET
Required NameSpaces
ADO.NET ships with four database client namespaces
System.Data.SqlClient System.Data.Oledb System.Data.Odbc System.Data.OracleClient
Shared Classes
ADO.NET contains a number of classes that are used irrespective of the namespace you are using to fetch data
Connection
A representation of an open connection to the database It does not perform the following tasks Doesnt fetch or update data Doesnt execute queries A Connection object is A pipeline that commands & queries use to send data & retrieve results It is a place to set the connection string
Connection
Command & Queries Connection Object Information from the data source & Returned data Database
DataSet
Stores one or more tables Is a disconnected, cached set of records that are retrieved from the database
Connection Object
Each Connection Object contains the same base properties, methods & events that are inherited from the System.Data.Common.DbConnection class Properties
Connection Properties
Name ConnectionString Description Gets or Set the String to open the connection
ConnectionTimeout Gets the time to wait while establishing the connection before generating an error Database Gets the name of current database after connection is open or the database name specified in the connection string before the connection is opened Gets the name of database server to which it is connected Gets a string that represents the version of the server to which the object is connected Gets a string that describes the state of the connection
Commands
A command is, in its simplest form, a string of text containing SQL statements that is to be issued to the database. A command could also be a stored procedure, or the name of a table that will return all columns and all rows from that table
Using Command
string source = @"Data Source=user\sqlexpress;Initial Catalog=AdventureWorks;Integrated Security=True"; string select = "SELECT ContactName,CompanyName FROM Customers"; SqlConnection conn = new SqlConnection(source); conn.Open(); SqlCommand cmd = new SqlCommand(select, conn);
CommandType
<provider>Command classes have a property called CommandType, which is used to define the type of command
CommandType Example
Text (default) String select = "SELECT ContactName FROM Customers"; SqlCommand cmd = new SqlCommand(select , conn);
StoredProcedure SqlCommand cmd = new SqlCommand("CustOrderHist", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@CustomerI D", "QUICK"); TableDirect OleDbCommand cmd = new OleDbCommand("Categories", conn); cmd.CommandType = CommandType.TableDirect;
Executing Commands
ExecuteNonQuery() Executes the command but does not return any output ExecuteReader() Executes the command and returns a typed IDataReader ExecuteScalar() Executes the command and returns a single value In addition to these methods, the SqlCommand class exposes the following method: ExecuteXmlReader() Executes the command and returns an XmlReader object, which can be used to traverse the XML fragment returned from the database.
ExecteNonQuery
Used for UPDATE, INSERT, or DELETE statements, where the only returned value is the number of records affected. This method can, however, return results if you call a stored procedure that has output parameters string select = "string select = "UPDATE Customers " + "SET ContactName = 'Bob' " + "WHERE ContactName = 'Bill'"; SqlCommand cmd = new SqlCommand(select, conn); int rowsReturned = cmd.ExecuteNonQuery(); Console.WriteLine("{0} rows returned.", rowsReturned);
ExecuteReader
This method executes the command and returns a typed data reader object, depending on the provider in use. The object returned can be used to iterate through the record(s) returned string select = "SELECT ContactName,CompanyName FROM Customers"; SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { Console.WriteLine("Contact : {0,-20} Company : {1}" , reader[0] , reader[1]); }
ExecuteScalar
Used to return a single result from a SQL statement, such as the count of records in a given table
string select = "SELECT COUNT(*) FROM Customers"; SqlCommand cmd = new SqlCommand(select, conn); object o = cmd.ExecuteScalar(); Console.WriteLine ( o ) ;
Stored Procedure
The following stored procedure can be created in SQL Server 2005 This procedure updates the data in the table CREATE PROCEDURE RegionUpdate (@RegionID INTEGER, @RegionDescription NCHAR(50)) AS SET NOCOUNT OFF UPDATE Region SET RegionDescription = @RegionDescription WHERE RegionID = @RegionID GO