.NET平台下使用C#连接各种数据库

在.NET平台下,通常我们需要连接不同的数据库,这就需要我们配置连接字符串以及提供常用的class进行数据存取。

1.C#连接Access

@"Provider=Microsoft.ACE.OLEDB.12.0;data source='F:\Code\codebase\introspection\introspection\Data\OriginalData.accdb'"

public class OleDbDataSourceProvider : DbDataSourceProvider

    {

        /// <summary>

        /// connection string is just like "Provider=Microsoft.ACE.OLEDB.12.0;data source='F:\Code\codebase\introspection\introspection\Data\OriginalData.accdb'"

        /// </summary>

        /// <param name="connectionString"></param>

        public OleDbDataSourceProvider(string connectionString)

            : base(connectionString)

        { }

 

        public override DbConnection GetDbConnection()

        {

            return new OleDbConnection(this.ConnectionString);

        }

 

        public override DataAdapter CreateAdapter(string query, DbConnection connection)

        {

            return new OleDbDataAdapter(query, connection as OleDbConnection);

        }

    }

 2.C#连接SQL Server 

@"Data Source=WSWINCNHZ0823;Initial Catalog=D:\SITES\TMS\SERVICES\DataService\APP_DATA\TMS.MDF;Integrated Security=True";

public class SqlDataSourceProvider : DbDataSourceProvider

    {

        /// <summary>

        /// the connections string is just like @"Data Source=WSWINCNHZ0823;Initial Catalog=D:\SITES\TMS\SERVICES\DataService\APP_DATA\TMS.MDF;Integrated Security=True";

        /// </summary>

        /// <param name="connectionString"></param>

        public SqlDataSourceProvider(string connectionString)

            : base(connectionString)

        {

        }

 

        public override DbConnection GetDbConnection()

        {

            var connection = new SqlConnection(this.ConnectionString);

           

            return connection;

        }

 

        public override DataAdapter CreateAdapter(string query, DbConnection connection)

        {

            return new SqlDataAdapter(query, connection as SqlConnection);

        }

    }

3.C#连接Oracle

public class OracleDataSourceProvider : DbDataSourceProvider

    {

        public OracleDataSourceProvider(string connectionString)

            : base(connectionString)

        { }

 

        public override DbConnection GetDbConnection()

        {

            return new OracleConnection(this.ConnectionString);

        }

 

        public override DataAdapter CreateAdapter(string query, DbConnection connection)

        {

            return new OracleDataAdapter(query, connection as OracleConnection);

        }

    }

4.C#连接MySQL

Based on MySQLDriverCS

@"Data Source=test;Password=***;User ID=root;Location=localhost;Port=3306;Extended Properties="""

public class MySqlDataSourceProvider : DbDataSourceProvider

    {

        public MySqlDataSourceProvider(string connectionString)

            : base(connectionString)

        { }

 

        public override DbConnection GetDbConnection()

        {

            return new MySQLConnection(this.ConnectionString);

        }

 

        public override DataAdapter CreateAdapter(string query, DbConnection connection)

        {

            return new MySQLDataAdapter(query, connection as MySQLConnection);

        }

    }

Based on net connector

@"SERVER=localhost;DATABASE=sakila;UID=root;PASSWORD=626912";

public class MySqlDataSourceProviderBasedOnNetConnector : DbDataSourceProvider

    {

        /// <summary>

        /// this is based on net connector, connection string is just like @"SERVER=localhost;DATABASE=sakila;UID=root;PASSWORD=***"

        /// </summary>

        /// <param name="connectionString"></param>

        public MySqlDataSourceProviderBasedOnNetConnector(string connectionString)

            : base(connectionString)

        { }

 

        public override DbConnection GetDbConnection()

        {

            return new MySqlConnection(this.ConnectionString);

        }

 

        public override DataAdapter CreateAdapter(string query, DbConnection connection)

        {

            return new MySqlDataAdapter(query, connection as MySqlConnection);

        }

    }

 

5.C#连接IBM DB2

 OleDbConnection1.Open(); 
//打开数据库连接 
OleDbDataAdapter1.Fill(dataSet1,"Address"); 
//将得来的数据填入dataSet 
DataGrid1.DataBind(); 
//绑定数据 
OleDbConnection1.Close(); 
//关闭连接 
//增加数据库数据

在Web Form上新增对应字段数量个数的TextBox,及一个button,为该按键增加Click响应事件代码如下:

this.OleDbInsertCommand1.CommandText = "INSERTsintosADDRESS(NAME, 
EMAIL, AGE, ADDRESS) VALUES 
('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')"; 
OleDbInsertCommand1.Connection.Open(); 
//打开连接 
OleDbInsertCommand1.ExecuteNonQuery(); 
//执行该SQL语句 
OleDbInsertCommand1.Connection.Close(); 
//关闭连接

6.C#连接SyBase

 Provider=Sybase.ASEOLEDBProvider.2;Initial Catalog=数据库名;User ID=用户名;Data Source=数据源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000;

 7.C#连接Express

"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TMS.mdf;Integrated Security=True;User Instance=True"

public class SqlServerExpressDataSourceProvider : DbDataSourceProvider

    {

        /// <summary>

        /// connection string is just like @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TMS.mdf;Integrated Security=True;User Instance=True"

        /// </summary>

        /// <param name="connectionString"></param>

        public SqlServerExpressDataSourceProvider(string connectionString)

            : base(connectionString)

        { }

 

        public override DbConnection GetDbConnection()

        {

            return new SqlConnection(this.ConnectionString);

        }

 

        public override DataAdapter CreateAdapter(string query, DbConnection connection)

        {

            return new SqlDataAdapter(query, connection as SqlConnection);

        }

    }

你可能感兴趣的:(.net)