DotNet,PHP,Java的資料庫連接代碼大全(帶示範代碼)

來源:互聯網
上載者:User

標籤:sqlserver   pdb   files   config   ssi   return   org   c#   trace   

C#資料庫連接字串

Web.config檔案

  <connectionStrings>    <!--SQLServer資料庫連接-->    <add name="conn_sqlserver" connectionString="server=127.0.0.1;uid=testuser;pwd=testPwd;database=db_test;pooling=true;min pool size=10;max pool size=1024;connection lifetime=300;packet size=1000" providerName="System.Data.SqlClient"/>    <!--MySQL資料庫連接-->    <add name="conn_mysql" connectionString="Data Source=127.0.0.1; Database=db_test; User Id=testuser;Password=testPwd; pooling=false; CharSet=utf8; Port=3306" providerName="MySql.Data.MySqlClient"/>    <!--SQLite資料庫連接-->    <add name="conn_sqlite" connectionString="Data Source=|DataDirectory|\db_test.db3; Pooling=true; FailIfMissing=false" providerName="System.Data.SQLite" />  </connectionStrings>  <!-- 解決DbProviderFactories.GetFactory(ProviderName)出現的異常:“找不到請求的 .Net Framework Data Provider。可能沒有安裝。” -->  <system.data>    <DbProviderFactories>      <remove invariant="System.Data.SQLite"/>      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.105.2, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />      <remove invariant="MySQL Data Provider"/>      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />    </DbProviderFactories>  </system.data>

下載: CSharpDbConnectionDemo(DotNet資料庫連接示範).zip

 

Java資料庫連接字串
// mysqldriver="com.mysql.jdbc.Driver";url = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF8";conn = getConnection(driver,url, "user", "password");System.out.println("與mysql資料庫連接" + (null == conn ? "失敗" : "成功"));// sqlite        driver="SQLite.JDBCDriver";url = "jdbc:sqlite:/C:/db_test.db3";conn = getConnection(driver,url);System.out.println("與sqlite資料庫連接" + (null == conn ? "失敗" : "成功"));// sqlserver        driver="com.mysql.jdbc.Driver";url = "jdbc:sqlserver://localhost:1433;DatabaseName=db_test";conn = getConnection(driver,url, "user", "password");System.out.println("與sql server資料庫連接" + (null == conn ? "失敗" : "成功"));// oracledriver="oracle.jdbc.driver.OracleDriver";url = "jdbc:oracle:thin:@localhost:1521:orcl";conn = getConnection(driver,url, "user", "password");System.out.println("與oracle資料庫連接" + (null == conn ? "失敗" : "成功"));

JDBC串連資料庫方法

public static Connection getConnection(String driver, String url, String user, String password) {    Connection conn = null;    try {        //初始化驅動包             Class.forName(driver);        conn = DriverManager.getConnection(url, user, password);    } catch (Exception e) {        e.printStackTrace();    }    return conn;}

下載:[jdbc_connection_demo(JDBC串連各種資料庫示範).zip]

 

PHP_PDO資料庫連接字串
// PDO串連MySQL的DSN字串 (需要開啟php設定檔中的"extension=php_pdo_mysql.dll"選項)mysql:dbname=db_test;host=127.0.0.1;port=3306;charset=utf8// PDO串連SQLite的DSN字串 (需要開啟php設定檔中的"extension=php_pdo_sqlite.dll"選項)sqlite:test_db.db3// PDO通過ODBC串連SQL Server的DSN字串 (需要開啟php設定檔中的"extension=php_pdo_odbc.dll"選項)odbc:Driver={SQL Server};Server=127.0.0.1,1433;Database=db_test;// PDO串連SQL Server的DSN字串(配置非常麻煩,建議使用ODBC方式。PHP5.3.6以上的版本中php_mssql.dll,php_pdo_mssql.dll都已經不見了,只有一個php_pdo_odbc.dll,所以最新最好的php串連mssql方法應該是使用ODBC的驅動模式。)sqlsrv:Database=db_test;Server=127.0.0.1,1433// PDO串連PostgreSQL的DSN字串 (需要開啟php設定檔中的"extension=php_pdo_pgsql.dll"選項)pgsql:dbname=db_test;host=127.0.0.1;port=5432// PDO串連Oracle的DSN字串 (需要開啟php設定檔中的"extension=php_pdo_oci.dll"選項)oci:dbname=//127.0.0.1:5432/db_test

下載:[pdo_dns_demo(PDO串連各種資料庫示範).zip]

PHP官方DSN說明:http://php.net/manual/en/ref.pdo-odbc.connection.php
更多DSN連接字串可以參考:http://www.connectionstrings.com/

著作權聲明:本文採用署名-非商業性使用-相同方式共用(CC BY-NC-SA 3.0 CN)國際許可協議進行許可,轉載請註明作者及出處。
本文標題:DotNet,PHP,Java的資料庫連接代碼大全(帶示範代碼)
本文連結:http://www.cnblogs.com/sochishun/p/7113193.html
本文SoChishun (郵箱:14507247#qq.com | 部落格:http://www.cnblogs.com/sochishun/)
發表日期:2017年7月3日

DotNet,PHP,Java的資料庫連接代碼大全(帶示範代碼)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.