//---------------------------------------------------------- //函式名稱: DB_Connect //說明: Oracle DB Connection //參數: 主機名稱,OWNER名稱,密碼 //回傳值: OleDbConnection //---------------------------------------------------------- public static OracleConnection DB_Connect(string host, string owner, string pwd) { OracleConnectionStringBuilder Csbuilder = new OracleConnectionStringBuilder(); try { //如果使用OleDbConnection才用下列這個連線字串 //設定連線字串參考(oracle)Provider=MSDAORA;Data Source=xxxxx;Persist Security Info=True;User ID=xxxx //Csbuilder.Provider = "MSDAORA"; Csbuilder.DataSource = host; Csbuilder.Add("User ID", owner); //加入使用者帳號 Csbuilder.Add("Password", pwd); //加入密碼帳號 Conn.ConnectionString = Csbuilder.ConnectionString; //Conn = new OleDbConnection(Csbuilder.ConnectionString); //設定Connection的連線字串。 Conn.Open(); return(Conn); } catch (Exception ex) { throw ex; //return null; } finally { Csbuilder = null; } }
public static string makeConnectionString(string DBUserName, string DBUserPassword, string DBName) { OracleConnectionStringBuilder b = new OracleConnectionStringBuilder(); b.Add("User Id", DBUserName); b.Add("Password", DBUserPassword); b.Add("Data Source", DBName); b.Add("Pooling", false); return(b.ConnectionString); }
protected override string BuildConnectionString(UrlInfo url) { SqlHelper.ValidateConnectionUrl(url); ArgumentValidator.EnsureArgumentNotNullOrEmpty(url.Resource, "url.Resource"); var builder = new OracleConnectionStringBuilder(); // host, port, database if (!string.IsNullOrEmpty(url.Host)) { var port = url.Port != 0 ? url.Port : DefaultPort; builder.DataSource = string.Format(DataSourceFormat, url.Host, port, url.Resource); } else { builder.DataSource = url.Resource; // Plain TNS name } // user, password if (!string.IsNullOrEmpty(url.User)) { builder.UserID = url.User; builder.Password = url.Password; } else { builder.UserID = "/"; } // custom options foreach (var parameter in url.Params) { builder.Add(parameter.Key, parameter.Value); } return(builder.ToString()); }
static void Main(string[] args) { string connString = "user id=scott;password=tiger;Data source=oracle;"; bool bRet = false; // Create an instance of OracleConnectionStringBuilder OracleConnectionStringBuilder connStrBuilder = new OracleConnectionStringBuilder(connString); // Add a new key/value to the connection string connStrBuilder.Add("pooling", false); // Modify the existing value connStrBuilder["Data source"] = "inst1"; // Remove an entry from the connection string bRet = connStrBuilder.Remove("pooling"); //ContainsKey indicates whether or not the specific key exist //returns true even if the user has not specified it explicitly Console.WriteLine("Enlist exist: " + connStrBuilder.ContainsKey("Enlist")); //returns false connStrBuilder.ContainsKey("Invalid"); // ShouldSerialize indicates whether or not a specific key // exists in connection string inherited from DbConnectionStringBuilder. // returns true if the key is explicitly added the user otherwise false; // this will return false as this key doesn't exists. connStrBuilder.ShouldSerialize("user"); // returns false because this key is nott added by user explicitly. connStrBuilder.ShouldSerialize("Enlist"); // IsFixedSize [read-only property] Console.WriteLine("Connection String is fixed size only: " + connStrBuilder.IsFixedSize); Console.WriteLine("Key/Value Pair Count: " + connStrBuilder.Count); //adding a new key which is not supported by the provider //is not allowed. try { //this will throw an exception. connStrBuilder.Add("NewKey", "newValue"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Key/Value Pair Count: " + connStrBuilder.Count); //modifying a existing key is allowed. connStrBuilder.Add("Enlist", false); Console.WriteLine("Key/Value Pair Count: " + connStrBuilder.Count); // Get all the keys and values supported by the provider. ICollection keyCollection = connStrBuilder.Keys; int cnt = keyCollection.Count; string[] keyAry = new string[cnt]; keyCollection.CopyTo(keyAry, 0); Array.Sort(keyAry); foreach (string key in keyAry) { Console.WriteLine("Key: {0} Value: {1} \n" , key, connStrBuilder[key]); } }