To access the database, provide a database Connection class. In C#, this is done through the Connection class.
Four types of connection modes
SQLConnection ADOConnection
OractleConnection ODBCConnection
SQLConnection to the database:
SQL Server Database
Windows identity authentication
Steps:
Using system.data. SqlClient;
Declare the concatenation method in the value string
Creating a Connection object
A method is called
To save system resources and improve system performance, it is best to close the connection after using the database.
In the c # language because of the existence of GC (garbage collection) to release resources at some time in the future, it is a decisive, not sure when this process occurs, when forget to close the database can be using statements to ensure that the objects exit immediately be released, so as to close the database, and another by the try.. catch.. final.. The e statement controls the shutdown of the connected database to improve performance.
Code implementation is as follows:
using System;
using System.Data.SqlClient; // Introduce the namespace
namespace Csharpone
{
class Program
{
static void Main(string[] args)
{
// Windows id verify that csharp01 is a new database name
string constr = "Server.; integrated security=SSPI; Initial Catalog=csharp01";
SqlConnection mysqlCon = new SqlConnection(constr); / / instantiate
mysqlCon.Open(); // Open the database
Console.WriteLine("Database open"); // If it is printed correctly, it is ok, otherwise an exception will be thrown
PWD is password. Csharp02 is database name
string constr1 = "Server.; user=name; pwd=mima; database=csharp02";
SqlConnection mysqlCon1 = new SqlConnection(constr1); / / instantiate
mysqlCon1.Open(); // Open the database
Console.WriteLine(Open database in SQL mode);
Using (mysqlCon1) {mysqlcon1.open (); Console.WriteLine(" data opened successfully "); // Close immediately} // Pass the try.. catch.. finally.. try { mysqlCon.Open(); Console.WriteLine(" database closed "); } catch { } finally { mysqlCon.Close(); Console.WriteLine(" close database "); } * /
// Use the above two methods together to ensure that the occupied database resources are released
try
{
using (mysqlCon)
{
mysqlCon.Open();
Console.WriteLine("Open database");
}
}
catch
{
}
finally
{
mysqlCon.Close();
Console.WriteLine("Close database"); } Console.Read(); }}}Copy the code
MySQL database code:
using System;
using MySql.Data.MySqlClient; // Import the reference and add the namespace
namespace CSharpThe connectionMysql
{
class Program
{
static void Main(string[] args)
{
string connectStr = "server=localhost; port=3306; database=czhenya01; user=root; password=123456;";
// No database connection has been established
MySqlConnection conn = new MySqlConnection(connectStr);
try
{
conn.Open(); // Set up the connection and open the database
Console.WriteLine("Database opened successfully");
}catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close(); // Close the connection} Console.ReadKey(); }}}Copy the code