Beispiel #1
0
        private string GetConnStr()
        {
            // Declare a generic ConnectionStringBuilder.
            DbConnectionStringBuilder connBldr = null;

            // Determine which type of actual ConnectionStringBuilder
            //   to instantiate.
            switch (this._dbType)
            {
            case AdoProviderType.OleProvider:
                connBldr = new OleDbConnectionStringBuilder();
                break;

            case AdoProviderType.OdbcProvider:
                connBldr = new OdbcConnectionStringBuilder();
                break;

            case AdoProviderType.SqlProvider:
                connBldr = new SqlConnectionStringBuilder();
                break;

            case AdoProviderType.DB2Provider:
                connBldr = new Db2ConnectionStringBuilder();
                break;

            default:
                // It *shouldn't* be possible to hit this line of code,
                //   unless I screwed up somewhere & allowed someone to
                //   specify AdoProviderType.Auto somewhere.
                throw new Exception("Unrecognized ADO provider type.");
            }

            // Load all the values from this instance into the
            //   ConnectionStringBuilder.
            for (int i = 0; i < this._vals.Count; i++)
            {
                connBldr.Add(this._vals.GetKey(i), this._vals[i]);
            }

            // Return the ConnectionStringBuilder's 'ToString' method result.
            return(connBldr.ToString());
        }
        private string GetConnStr()
        {
            // Declare a generic ConnectionStringBuilder.
            DbConnectionStringBuilder connBldr = null;

            // Determine which type of actual ConnectionStringBuilder
            //   to instantiate.
            switch (this._dbType)
            {
                case AdoProviderType.OleProvider:
                    connBldr = new OleDbConnectionStringBuilder();
                    break;
                case AdoProviderType.OdbcProvider:
                    connBldr = new OdbcConnectionStringBuilder();
                    break;
                case AdoProviderType.SqlProvider:
                    connBldr = new SqlConnectionStringBuilder();
                    break;
                case AdoProviderType.DB2Provider:
                    connBldr = new Db2ConnectionStringBuilder();
                    break;
                default:
                    // It *shouldn't* be possible to hit this line of code,
                    //   unless I screwed up somewhere & allowed someone to
                    //   specify AdoProviderType.Auto somewhere.
                    throw new Exception("Unrecognized ADO provider type.");
            }

            // Load all the values from this instance into the
            //   ConnectionStringBuilder.
            for (int i = 0; i < this._vals.Count; i++)
                connBldr.Add(this._vals.GetKey(i), this._vals[i]);

            // Return the ConnectionStringBuilder's 'ToString' method result.
            return connBldr.ToString();
        }
Beispiel #3
0
        //***************************************************************************
        // Static Methods
        //
        public static DbConnectionProperties FromString(AdoProviderType provid, string value)
        {
            if (provid == AdoProviderType.Auto)
            {
                throw new ArgumentException("Enumeration value \"Auto\" is not valid for this function.", "provid");
            }

            DbConnectionProperties retVal = new DbConnectionProperties();

            retVal._dbType = provid;

            DbConnectionStringBuilder connBldr = null;

            switch (provid)
            {
            case AdoProviderType.SqlProvider:
                connBldr = new SqlConnectionStringBuilder(value);
                break;

            case AdoProviderType.OleProvider:
                connBldr = new OleDbConnectionStringBuilder(value);
                break;

            case AdoProviderType.OdbcProvider:
                connBldr = new OdbcConnectionStringBuilder(value);
                break;

            case AdoProviderType.DB2Provider:
                connBldr = new Db2ConnectionStringBuilder(value);
                break;
            }
            string[] keys = new string[connBldr.Keys.Count];
            connBldr.Keys.CopyTo(keys, 0);
            string[] vals = new string[connBldr.Values.Count];
            connBldr.Values.CopyTo(vals, 0);
            for (int i = 0; i < keys.Length; i++)
            {
                retVal._vals.Add(vals[i], keys[i]);
            }

            return(retVal);

            #region Depreciated Code
            //AdoProviderType provid;
            //string ds = "", un = "", pw = "", ic = "", exAttr = "";

            //// Parse the provider type.
            //if (value.ToUpper().Contains("PROVIDER="))
            //    provid = AdoProviderType.OleProvider;
            //else if (value.ToUpper().Contains("DRIVER="))
            //    provid = AdoProviderType.OdbcProvider;
            //else
            //    provid = AdoProviderType.SqlProvider;

            //// Parse the datasource.
            //int dsIdx = value.ToUpper().IndexOf("DATA SOURCE=");
            //if (dsIdx > -1)
            //{
            //    int dsLen = value.IndexOf(';', dsIdx) - dsIdx;
            //    string dsStr = value.Substring(dsIdx, dsLen);
            //    ds = dsStr.Substring(dsStr.IndexOf('=') + 1);
            //}

            //// Parse the username.
            //int unIdx = value.ToUpper().IndexOf("USER ID=");
            //if (unIdx > -1)
            //{
            //    int unLen = value.IndexOf(';', unIdx) - unIdx;
            //    string unStr = value.Substring(unIdx, unLen);
            //    un = unStr.Substring(unStr.IndexOf('=') + 1);
            //}

            //// Parse the password.
            //int pwIdx = value.ToUpper().IndexOf("PASSWORD="******"INITIAL CATALOG=");
            //if (icIdx > -1)
            //{
            //    int icLen = value.IndexOf(';', icIdx) - icIdx;
            //    string icStr = value.Substring(icIdx, icLen);
            //    ic = icStr.Substring(icStr.IndexOf('=') + 1);
            //}

            //// Create a new object based on the captured values.
            //DbConnectionProperties dbProp = new DbConnectionProperties(provid, ds, un, pw, ic);

            //// Check for any extended properties
            //int epIdx = value.ToUpper().IndexOf("EXTENDED PROPERTIES=");
            //if (epIdx > -1)
            //{
            //    int epLen = value.IndexOf(';', epIdx) - epIdx;
            //    string epStr = value.Substring(epIdx, epLen);
            //    dbProp.ExtendedProperties = epStr;
            //}

            //// Return the new struct.
            //return dbProp;
            #endregion
        }
        //***************************************************************************
        // Static Methods
        // 
        public static DbConnectionProperties FromString(AdoProviderType provid, string value)
        {
            if (provid == AdoProviderType.Auto)
                throw new ArgumentException("Enumeration value \"Auto\" is not valid for this function.", "provid");

            DbConnectionProperties retVal = new DbConnectionProperties();
            retVal._dbType = provid;

            DbConnectionStringBuilder connBldr = null;
            switch (provid)
            {
                case AdoProviderType.SqlProvider:
                    connBldr = new SqlConnectionStringBuilder(value);
                    break;
                case AdoProviderType.OleProvider:
                    connBldr = new OleDbConnectionStringBuilder(value);
                    break;
                case AdoProviderType.OdbcProvider:
                    connBldr = new OdbcConnectionStringBuilder(value);
                    break;
                case AdoProviderType.DB2Provider:
                    connBldr = new Db2ConnectionStringBuilder(value);
                    break;
            }
            string[] keys = new string[connBldr.Keys.Count];
            connBldr.Keys.CopyTo(keys, 0);
            string[] vals = new string[connBldr.Values.Count];
            connBldr.Values.CopyTo(vals, 0);
            for (int i = 0; i < keys.Length; i++)
                retVal._vals.Add(vals[i], keys[i]);

            return retVal;

            #region Depreciated Code
            //AdoProviderType provid;
            //string ds = "", un = "", pw = "", ic = "", exAttr = "";

            //// Parse the provider type.
            //if (value.ToUpper().Contains("PROVIDER="))
            //    provid = AdoProviderType.OleProvider;
            //else if (value.ToUpper().Contains("DRIVER="))
            //    provid = AdoProviderType.OdbcProvider;
            //else
            //    provid = AdoProviderType.SqlProvider;

            //// Parse the datasource.
            //int dsIdx = value.ToUpper().IndexOf("DATA SOURCE=");
            //if (dsIdx > -1)
            //{
            //    int dsLen = value.IndexOf(';', dsIdx) - dsIdx;
            //    string dsStr = value.Substring(dsIdx, dsLen);
            //    ds = dsStr.Substring(dsStr.IndexOf('=') + 1);
            //}

            //// Parse the username.
            //int unIdx = value.ToUpper().IndexOf("USER ID=");
            //if (unIdx > -1)
            //{
            //    int unLen = value.IndexOf(';', unIdx) - unIdx;
            //    string unStr = value.Substring(unIdx, unLen);
            //    un = unStr.Substring(unStr.IndexOf('=') + 1);
            //}

            //// Parse the password.
            //int pwIdx = value.ToUpper().IndexOf("PASSWORD="******"INITIAL CATALOG=");
            //if (icIdx > -1)
            //{
            //    int icLen = value.IndexOf(';', icIdx) - icIdx;
            //    string icStr = value.Substring(icIdx, icLen);
            //    ic = icStr.Substring(icStr.IndexOf('=') + 1);
            //}

            //// Create a new object based on the captured values.
            //DbConnectionProperties dbProp = new DbConnectionProperties(provid, ds, un, pw, ic);

            //// Check for any extended properties
            //int epIdx = value.ToUpper().IndexOf("EXTENDED PROPERTIES=");
            //if (epIdx > -1)
            //{
            //    int epLen = value.IndexOf(';', epIdx) - epIdx;
            //    string epStr = value.Substring(epIdx, epLen);
            //    dbProp.ExtendedProperties = epStr;
            //}

            //// Return the new struct.
            //return dbProp;
            #endregion
        }