ADO.NET is an object-oriented set of libraries that allows to interact with the data sources.
Data source are mostly data based but it could be an excel sheet, text file or xml file.
Examples of Data providers
The sequence of operations occur in the llfe of sqlConnection are as follows :
Connection string parameters
Integrated security is secure when we are on a single machine doing development.
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
Sql server security
Data source are mostly data based but it could be an excel sheet, text file or xml file.
Data providers
- ADO.NET provides a common way to interact with the data sources.
- It comes in different sets of libraries and these libraries are called Data Providers.
Examples of Data providers
- ODBC Data provider -- used in older databases
- OleDB Data provider - used in MS Access or Excel
- Oracle Data provider - used for oracle databases
- SQL Data provider - used for Microsoft sql server
- Borland Data provider - Generic access to many databases such as interbase, Sql Server, IBM DB2, and Oracle.
SqlConnection object
- SqlConnection object is used to create a connection with the database which can be located over the LAN or over the internet.
- Sql connection object enable other ADO.NET code to work with the database.
- Other ADO.NET objects us connection object as parameter.
The sequence of operations occur in the llfe of sqlConnection are as follows :
- Instantiate the sql connection
- Open the connection
- pass the connection to other ADO.NET objects.
- Perform the database operations
- Close the connection.
Connection string
- Connection string is a key value pairs for specifying how to make a database connection.
- It includes the location, Name of the database and security credentials.
Connection string parameters
- Data source - Identifies the server
- Initial Catalog - Database name
- Integrated security - set to sspi to make connection with user's windows login
- User ID - Name of user configured in sql server
- Password - Password matching SQL Server User Id
Integrated security
Integrated security is secure when we are on a single machine doing development.
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
Sql server security
Sql server security is secure when database located on a different machine, over a LAN, or over the internet. User ID and password replace the integrated security parameters
SqlConnection conn = new SqlConnection(
"Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword");
Example :
using System;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
static void Main()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlDataReader rdr = null;
try
{
// 2. Open the connection
conn.Open();
// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from Customers", conn);
//
// 4. Use the connection
//
// get query results
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
}
SqlConnection conn = new SqlConnection(
"Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword");
Example :
using System;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
static void Main()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlDataReader rdr = null;
try
{
// 2. Open the connection
conn.Open();
// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from Customers", conn);
//
// 4. Use the connection
//
// get query results
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// 5. Close the connection
if (conn != null)
{
conn.Close();
}
}
}
}
No comments:
Post a Comment