2011年5月26日 星期四

DotNET Connection Strings

Connection String
1. Access 2003
Provider=Microsoft.Jet.OLEDB.4.0; Data Source=myData.mdb;
2. Excel 2003
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyExcel.xls; Extended Properties=""Excel 8.0; HDR=Yes;IMEX=1""
3. SQL 2008 R2
Data Source=192.168.xxx.ooo\JOUSQL2008R2;Initial Catalog=HWC991;User ID=Web9912;Password=xxx9912x
4. Access 2007
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;
5. Excel 2007
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES";
6. MySQL
using MySql.Data.MySqlClient;
string strConn = "Server=192.168.xxx.ooo;Uid=Web9912;Pwd=HWC9912a;Database=test;";

ref: http://msdn.microsoft.com/zh-tw/library/system.data.odbc.odbcconnection.connectionstring(v=vs.80).aspx

ODBC Connection
7. SQL w/o testing
"Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"
8. Access 2003
"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"
9. Excel 2003
"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"

10 Example shows both of Odbc and OleDb connecting to Excel file while DataAdapter and DataReader were used to get data table and data row repectively as follows:
using System.Data;
using System.Data.Odbc;
using System.Data.OleDb;
class getxls
        {
            static public DataTable OdbcGetTable(string SQL, string path1)
            {
                DataSet DS = new DataSet();
                OdbcConnection conn = new OdbcConnection("Driver={Microsoft Excel Driver (*.xls)};DBQ=" + path1);
                conn.Open();
                OdbcDataAdapter apt = new OdbcDataAdapter(SQL, conn);
                apt.Fill(DS);
                conn.Close();
                return DS.Tables[0];
            }
            static public DataTable OleDbGetTable(string SQL, string path1)
            {
                DataSet DS = new DataSet();
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path1 + "; Extended Properties='Excel 8.0; HDR=Yes;IMEX=1'");
                conn.Open();
                OleDbDataAdapter apt = new OleDbDataAdapter(SQL, conn);
                apt.Fill(DS);
                conn.Close();
                return DS.Tables[0];
            }
            static public string OleDbGetRow(string SQL, string path1)
            {
                DataSet DS = new DataSet();
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path1 + "; Extended Properties='Excel 8.0; HDR=Yes;IMEX=1'");
                OleDbCommand cmd = new OleDbCommand(SQL, conn);
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
                //while (reader.Read())
                //{
                //    Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
                //}
                string out1 = "";
                while (reader.Read())
                {
                    int ct = reader.FieldCount;
                   
                    for (int i = 0; i < ct; i++)
                    {
                        out1 += reader.GetDouble(i).ToString() ;
                    }
                    out1 += ";";
                   
                }
                conn.Close();
                return out1 ;
            }
            static public string OdbcGetRow(string SQL, string path1)
            {
                DataSet DS = new DataSet();
                OdbcConnection conn = new OdbcConnection("Driver={Microsoft Excel Driver (*.xls)};DBQ=" + path1);
                OdbcCommand cmd = new OdbcCommand(SQL, conn);
                conn.Open();
                OdbcDataReader reader = cmd.ExecuteReader();
                //while (reader.Read())
                //{
                //    Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
                //}
                string out1 = "";
                while (reader.Read())
                {
                    int ct = reader.FieldCount;
                    for (int i = 0; i < ct; i++)
                    {
                        out1 += reader.GetDouble(i).ToString();
                    }
                    out1 += ";";
                }
                conn.Close();
                return out1;
            }
        }

沒有留言:

張貼留言