C#/.NET项目开发过程中,特别是中大型的项目,跟数据库打交道那是再正常不过的事情了。数据库可以帮助我们保存和管理数据。C#/.NET的项目中,我们也可以使用不同的数据库。那么,我们今天就来总结整理一下C#/.NET的连接各类主流数据库(SQL Server,MySQL,Microsoft Access,DB2,Oracle)的连接字符串。
一、C#/.NET连接Microsoft SQL Server
1. 使用ODBC数据库连接// ODBC标准连接 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Uid=UserName; Pwd=password"; conn.Open(); // ODBC受信任的连接 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Uid=admin; Pwd=password"; conn.Open(); // 或者 OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Trusted_Connection=Yes;"; conn.Open();2.使用OLEDB
// OleDb标准连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB; Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;"; conn.Open(); // OleDb受信任的连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=SQLOLEDB; Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;"; conn.Open();3.使用.Net DataProvider
// .NET DataProvider标准连接 using System.Data.SqlClient; SqlConnection conn = new SqlDbConnection(); conn.ConnectionString ="Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;"; conn.Open(); // .NET DataProvider受信任连接 using System.Data.SqlClient; SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;"; conn.Open();
二、C#/.NET连接Microsoft Access
1.使用ODBC连接// ODBC标准连接 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)}; Dbq=c:\myPath\myDb.mdb; Uid=Admin; Pwd=;password"; conn.Open(); // ODBC工作组(Workgroup)连接 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)}; Dbq=c:\myPath\myDb.mdb; SystemDb=c:\myPath\myDb.mdw;"; conn.Open();2.使用OLEDB连接
// 使用OleDb的Jet标准安全连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\mypath\myDb.mdb; User id=admin;Password=password"; conn.Open(); // 使用OleDb的Jet System Database连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\mypath\myDb.mdb; System Database=c:\mypath\myDb.mdw"; conn.Open();
三、C#/.NET连接MySQL数据库
1.使用ODBC数据源连接// ODBC使用MyODBC Driver的远程数据库连接 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={MySql}; Server=192.168.0.10; Option=131072; Port=3306; Stmt=; DataBase=DataBaseName; Uid=UserName; Pwd=password;" ; conn.Open();2.使用OLEDB连接
// OleDb标准连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Provider=MySqlProv; Data Source=ServerName; User id=UserName; Password=Password"; conn.Open();
四、C#/.NET连接DB2数据库
1.使用ODBC连接// 不带DSN的ODBC连接 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={IBM DB2 ODBC DRIVER};DataBase=DataBaseName; HostName=ServerName; Protocol=TCPIP;Port=PortNumber;Uid=UserName;Pwd=Secret"; conn.Open();2.使用OLEDB连接
// OleDb微软官方驱动 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=DB2OLEDB; Network Transport Library=TCPIP; Network Address=xxx.xxx.xxx.xxx; Package Collection=CollectionName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;"; conn.Open(); // OleDb IBM 驱动 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=IBMDADB2; DataBase=DataBaseName; HostName=ServerName; Protocol=TCPIP; Port=PortNumber; Uid=UserName; Pwd=Secret;"; conn.Open();3.使用.Net DataProvider第三方驱动
// 来自IBM的.NET DataProvider using IBM.Data.DB2; Db2Connection conn = new Db2Connection(); conn.ConnectionString = "DataBase=DataBaseName;Uid=UserName;Pwd=Secret"; conn.Open();
五、C#/.NET连接Oracle数据库
1.使用ODBC连接// ODBC 微软的官方驱动 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;Uid=UserName;Pwd=Secret;"; conn.Open(); // ODBC Oracle的驱动 using System.Data.Odbc; OdbcConnection conn = new OdbcConnection(); conn.ConnectionString = "Driver={Oracle ODBC Driver};Dbq=myDataBase;Uid=UserName;Pwd=Secret;"; conn.Open();2.使用OLEDB连接
// OleDb:Oracle驱动的标准连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=ServerName;User id=UserName;Password=Secret;"; conn.Open(); // OleD:Oracle驱动的受信任连接 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=ServerName;OSAuthent=1;"; conn.Open(); // 或者写成 using System.Data.OleDb; OleDbConnection conn = new OleDbConnection(); conn.ConnectionString = "Driver=OraOLEDB.Oracle;Data Source=ServerName;User id=admin;Password=pwd"; conn.Open();
版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。
发表评论
登录用户才能发表评论, 请 登 录 或者 注册