public void ConnectionStringConstructorTest ()
		{
			OdbcConnectionStringBuilder builder;

			builder = new OdbcConnectionStringBuilder ("Driver={SQL Server};Server=(local);Database=AdventureWorks;Uid=ab;Pwd=pass@word1");
			Assert.AreEqual ("AdventureWorks", builder ["Database"],"#A1");
			Assert.AreEqual ("pass@word1", builder ["Pwd"], "#A2");
			Assert.AreEqual ("ab", builder ["Uid"], "#A3");
			Assert.AreEqual ("{SQL Server}", builder ["Driver"], "#A4");
			Assert.AreEqual ("(local)", builder ["Server"],"#A5");
			Assert.AreEqual ("Driver={SQL Server};server=(local);database=AdventureWorks;uid=ab;pwd=pass@word1", builder.ConnectionString,"#A5");

			builder = new OdbcConnectionStringBuilder ("Driver=");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#B");

			builder = new OdbcConnectionStringBuilder ("Dsn=");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#C");

			builder = new OdbcConnectionStringBuilder (null);
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#D"); 

			builder = new OdbcConnectionStringBuilder (string.Empty);
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#E");

			builder = new OdbcConnectionStringBuilder ("Driver=SQL {Server;Dsn=Adventu{re");
			Assert.AreEqual ("SQL {Server", builder ["Driver"], "#F1");
			Assert.AreEqual ("SQL {Server", builder.Driver, "#F2");
			Assert.AreEqual ("Adventu{re", builder ["Dsn"], "#F3");
			Assert.AreEqual ("Adventu{re", builder.Dsn, "#F4");
			Assert.AreEqual ("Dsn=Adventu{re;Driver={SQL {Server}", builder.ConnectionString, "#F5");
		}
Ejemplo n.º 2
0
        /// <summary>
        /// Converts ODBC connection string to the SQL Client (.NET).
        /// </summary>
        public static string OdbcToSqlClient(this string odbcString) {
            if (string.IsNullOrEmpty(odbcString)) {
                return null;
            }
            try {
                var odbc = new OdbcConnectionStringBuilder(odbcString);
                var server= odbc.GetValue(OdbcServerKey);
                var database = odbc.GetValue(OdbcDatabaseKey);
                if (!string.IsNullOrWhiteSpace(server) && !string.IsNullOrWhiteSpace(database)) {
                    var sql = new SqlConnectionStringBuilder();
                    sql.DataSource = server;
                    sql.InitialCatalog = database;

                    if (odbc.ContainsKey(OdbcUidKey)) {
                        //Standard Connection
                        sql.IntegratedSecurity = false;
                        sql.UserID = odbc.GetValue(OdbcUidKey);
                        sql.Password = odbc.GetValue(OdbcPasswordKey);
                    } else {
                        //Trusted Connection
                        sql.IntegratedSecurity = true;
                    }
                    return sql.ConnectionString;
                }
            } catch(ArgumentException) { }
            return null;
        }
Ejemplo n.º 3
0
        public static DbConnectionStringBuilder CreateConnectionStringBuilder(DataProvider dataProvider)
        {
            DbConnectionStringBuilder dbConnectionStringBuilder;

            switch (dataProvider)
            {
                case DataProvider.Odbc:
                    dbConnectionStringBuilder = new OdbcConnectionStringBuilder();
                    break;

                case DataProvider.OleDB:
                    dbConnectionStringBuilder = new OleDbConnectionStringBuilder();
                    break;

                case DataProvider.SqlServer:
                    dbConnectionStringBuilder = new SqlConnectionStringBuilder();
                    break;

                default:
                    dbConnectionStringBuilder = null;
                    break;
            }

            return dbConnectionStringBuilder;
        }
		public void Add ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder.Add ("driverid", "420");
			builder.Add ("driverid", "560");
			builder.Add ("DriverID", "840");
			Assert.AreEqual ("840", builder ["driverId"], "#A1");
			Assert.IsTrue (builder.ContainsKey ("driverId"), "#A2");
			builder.Add ("Driver", "OdbcDriver");
			Assert.AreEqual ("OdbcDriver", builder.Driver, "#B1");
			Assert.AreEqual ("OdbcDriver", builder ["Driver"], "#B2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#B3");
			builder.Add ("Driver", "{OdbcDriver");
			Assert.AreEqual ("{OdbcDriver", builder.Driver, "#C1");
			Assert.AreEqual ("{OdbcDriver", builder ["Driver"], "#C2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#C3");
			builder.Add ("Dsn", "MyDsn");
			Assert.AreEqual ("MyDsn", builder.Dsn, "#D1");
			Assert.AreEqual ("MyDsn", builder ["Dsn"], "#D2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#D3");
			builder.Add ("dsN", "MyDsn2");
			Assert.AreEqual ("MyDsn2", builder.Dsn, "#E1");
			Assert.AreEqual ("MyDsn2", builder ["Dsn"], "#E2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#E3");
		}
Ejemplo n.º 5
0
 private static OdbcConnection OpenConnection(string connectionString)
 {
     OdbcConnectionStringBuilder csb = new OdbcConnectionStringBuilder();
     csb.ConnectionString = connectionString;
     OdbcConnection conn = new OdbcConnection(csb.ConnectionString);
     conn.Open();
     return conn;
 }
        private string CreateOdbcConnectionString(string userName, string password, string dataSource, string databaseName)
        {
            string serverName = GetServerName(dataSource);

            OdbcConnectionStringBuilder connectionStringBuilder = new OdbcConnectionStringBuilder
            {
                Driver = "SQL Server Native Client 11.0",
            };
            connectionStringBuilder["Server"] = "tcp:" + dataSource;
            connectionStringBuilder["Database"] = databaseName;
            connectionStringBuilder["Uid"] = userName + "@" + serverName;
            connectionStringBuilder["Pwd"] = password;
            return connectionStringBuilder.ConnectionString;
        }
Ejemplo n.º 7
0
Archivo: test.cs Proyecto: mono/gert
	static string CreateOdbcConnectionString ()
	{
#if NET_2_0
		OdbcConnectionStringBuilder csb = new OdbcConnectionStringBuilder ();
		csb.Driver = "SQL Server";
#else
		StringBuilder sb = new StringBuilder ();
		sb.Append ("Driver={SQL Server};");
#endif

		string serverName = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_HOST");
		if (serverName == null)
			throw CreateEnvironmentVariableNotSetException ("MONO_TESTS_SQL_HOST");
#if NET_2_0
		csb.Add ("Server", serverName);
#else
		sb.AppendFormat ("Server={0};", serverName);
#endif

		string dbName = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_DB");
		if (dbName == null)
			throw CreateEnvironmentVariableNotSetException ("MONO_TESTS_SQL_DB");
#if NET_2_0
		csb.Add ("Database", dbName);
#else
		sb.AppendFormat ("Database={0};", dbName);
#endif

		string userName = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_USER");
		if (userName != null)
#if NET_2_0
			csb.Add ("Uid", userName);
#else
			sb.AppendFormat ("Uid={0};", userName);
#endif

		string pwd = Environment.GetEnvironmentVariable ("MONO_TESTS_SQL_PWD");
		if (pwd != null)
#if NET_2_0
			csb.Add ("Pwd", pwd);
#else
			sb.AppendFormat ("Pwd={0};", pwd);
#endif

#if NET_2_0
		return csb.ToString ();
#else
		return sb.ToString ();
#endif
	}
Ejemplo n.º 8
0
 override public object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == null)
     {
         throw ADP.ArgumentNull("destinationType");
     }
     if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType)
     {
         OdbcConnectionStringBuilder obj = (value as OdbcConnectionStringBuilder);
         if (null != obj)
         {
             return(ConvertToInstanceDescriptor(obj));
         }
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
Ejemplo n.º 9
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == null)
     {
         throw ADP.ArgumentNull("destinationType");
     }
     if (typeof(InstanceDescriptor) == destinationType)
     {
         OdbcConnectionStringBuilder options = value as OdbcConnectionStringBuilder;
         if (options != null)
         {
             return(this.ConvertToInstanceDescriptor(options));
         }
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
       /// <summary>
       /// Create a new drive.  Create a connection to the database file and set
       /// the Connection property in the PSDriveInfo.
       /// </summary>
       /// <param name="drive">
       /// Information describing the drive to add.
       /// </param>
       /// <returns>The added drive.</returns>
       protected override PSDriveInfo NewDrive(PSDriveInfo drive)
       {
           // check if drive object is null
           if (drive == null)
           {
               WriteError(new ErrorRecord(
                   new ArgumentNullException("drive"),
                   "NullDrive",
                   ErrorCategory.InvalidArgument,
                   null)
               );

               return null;
           }

           // check if drive root is not null or empty
           // and if its an existing file
           if (String.IsNullOrEmpty(drive.Root) || (File.Exists(drive.Root) == false))
           {
               WriteError(new ErrorRecord(
                   new ArgumentException("drive.Root"),
                   "NoRoot",
                   ErrorCategory.InvalidArgument,
                   drive)
               );

               return null;
           }


           // create a new drive and create an ODBC connection to the new drive
           AccessDBPSDriveInfo accessDBPSDriveInfo = new AccessDBPSDriveInfo(drive);

           OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

           builder.Driver = "Microsoft Access Driver (*.mdb)";
           builder.Add("DBQ", drive.Root);

           OdbcConnection conn = new OdbcConnection(builder.ConnectionString);
           conn.Open();
           accessDBPSDriveInfo.Connection = conn;

           return accessDBPSDriveInfo;
       } // NewDrive
Ejemplo n.º 11
0
 /// <summary>
 /// Converts SQL Client (.NET) connection string to the ODBC connection string.
 /// </summary>
 public static string SqlClientToOdbc(this string sqlClientString) {
     if(string.IsNullOrEmpty(sqlClientString)) {
         return null;
     }
     try {
         var sql = new SqlConnectionStringBuilder(sqlClientString);
         var odbc = new OdbcConnectionStringBuilder();
         odbc[OdbcDriverKey] = OdbcSqlDriver;
         odbc[OdbcServerKey] = sql.DataSource;
         odbc[OdbcDatabaseKey] = sql.InitialCatalog;
         if (sql.IntegratedSecurity) {
             odbc[OdbcTrustedConnectionKey]  = "yes";
         } else {
             odbc[OdbcUidKey] = sql.UserID;
             odbc[OdbcPasswordKey] = sql.Password;
         }
         return odbc.ConnectionString;
     } catch (ArgumentException) { }
     return null;
 }
Ejemplo n.º 12
0
        /// <summary>Damの選択</summary>
        private void cmbDataProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // 初期は、無効にして、ODP.NET、HiRDB選択時のみ有効にする。
                this.nudNumOfBind.Enabled = false;

                if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_SQL)
                {
                    //sqlClient
                    this._dam = new DamSqlSvr();

                    //接続文字列のサンプルを設定する(空の場合)。
                    SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();

                    csb.DataSource = this._ip;
                    csb.InitialCatalog = "Northwind";
                    csb.UserID = this._uid;
                    csb.Password = this._pwd;

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_SQL_CREATED");

                }
                else if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_OLE)
                {
                    //OLEDB.NET
                    this._dam = new DamOLEDB();

                    //接続文字列のサンプルを設定する(空の場合)。
                    OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

                    csb.Provider = "Provider";
                    csb.DataSource = "DataSourceName";
                    csb.FileName = "FileName";

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 活性
                    this.nudNumOfBind.Enabled = true;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_OLE_CREATED");

                }
                if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_ODB)
                {
                    //ODBC.NET
                    this._dam = new DamODBC();

                    //接続文字列のサンプルを設定する(空の場合)。
                    OdbcConnectionStringBuilder csb = new OdbcConnectionStringBuilder();

                    csb.Driver = "DriverName";
                    csb.Dsn = "DataSourceName";

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 活性
                    this.nudNumOfBind.Enabled = true;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_ODB_CREATED");

                }
                if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_ORA)
                {
                    //Oracle Client
                    this._dam = new DamOraClient();

                    //接続文字列のサンプルを設定する(空の場合)。
                    System.Data.OracleClient.OracleConnectionStringBuilder csb = new System.Data.OracleClient.OracleConnectionStringBuilder();

                    csb.DataSource = this._ip + "/orcl";
                    csb.UserID = this._uid;
                    csb.Password = this._pwd;

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 活性
                    this.nudNumOfBind.Enabled = true;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_ODP_CREATED");

                } 
                if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_ODP)
                {
                    //ODP.NET
                    this._dam = new DamOraOdp();

                    //接続文字列のサンプルを設定する(空の場合)。
                    Oracle.DataAccess.Client.OracleConnectionStringBuilder csb = new Oracle.DataAccess.Client.OracleConnectionStringBuilder();

                    csb.DataSource = this._ip + "/orcl";
                    csb.UserID = this._uid;
                    csb.Password = this._pwd;

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 活性
                    this.nudNumOfBind.Enabled = true;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_ODP_CREATED");

                }
                else if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_DB2)
                {
                    //DB2.NET
                    this._dam = new DamDB2();

                    //接続文字列のサンプルを設定する(空の場合)。
                    DB2ConnectionStringBuilder csb = new DB2ConnectionStringBuilder();

                    //csb.Server = this._ip + ":50000";
                    csb.Database = "SAMPLE";
                    csb.UserID = this._uid;
                    csb.Password = this._pwd;

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_DB2_CREATED");

                }
                //else if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_HiRDB)
                //{
                //    //HiRDBデータ プロバイダ
                //    this._dam = new DamHiRDB();

                //    //接続文字列のサンプルを設定する(空の場合)。
                //    //HiRDBデータ プロバイダは、ConnectionStringBuilderがない。
                //    string csb = "";
                //    csb += "DataSource=C:\\Windows\\HiRDB.ini;";
                //    csb += "UID=" + this._uid + ";";
                //    csb += "PWD=" + this._pwd + ";";

                //    this.txtCnnStr.Text = csb;

                //    // 活性
                //    this.nudNumOfBind.Enabled = true;

                //    // 状態
                //    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = Literal.STATUS_HRD_CREATED;
                //}
                else if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_MySQL)
                {
                    //MySQL Connector/NET
                    this._dam = new DamMySQL();

                    //接続文字列のサンプルを設定する(空の場合)。
                    MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();

                    csb.Server = this._ip;
                    csb.Database = "test";
                    csb.UserID = this._uid;
                    csb.Password = this._pwd;

                    this.txtCnnStr.Text = csb.ConnectionString;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_MSL_CREATED");

                }
                else if (this.cmbDataProvider.SelectedItem.ToString() == Literal.DAP_PstgrS)
                {
                    //Npgsql
                    this._dam = new DamPstGrS();

                    //接続文字列のサンプルを設定する(空の場合)。
                    //NpgsqlConnectionStringBuilderは、冗長な接続文字列を返すので使わない。

                    //NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder();
                    string csb = "";

                    //csb.Host = this._ip;
                    //csb.Database = "postgres";
                    //csb.UserName = this._uid;
                    //csb.Password = this._pwd;
                    csb += "HOST=" + this._ip + ";";
                    csb += "DATABASE=postgres;";
                    csb += "UID=" + this._uid + ";";
                    csb += "PWD=" + this._pwd + ";";

                    this.txtCnnStr.Text = csb;

                    // 状態
                    ((ToolStripStatusLabel)this.statBar.Items[0]).Text = this.RM_GetString("STATUS_PGS_CREATED");
                }
                else
                {
                    //ありえない
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 private InstanceDescriptor ConvertToInstanceDescriptor(OdbcConnectionStringBuilder options)
 {
     Type[] types = new Type[] { typeof(string) };
     return new InstanceDescriptor(typeof(OdbcConnectionStringBuilder).GetConstructor(types), new object[] { options.ConnectionString });
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Base constructor that takes a connection-string and initializes the ConnectionStringBuilder with it.
 /// </summary>
 /// <param name="ConnectionString">Connection-string with the needed specifications and arguments to set up a connection.</param>
 public DBClassOdbc(string ConnectionString)
 {
     ConnStringBuild = new OdbcConnectionStringBuilder(ConnectionString);
 }
		public void Indexer_Dsn_Null ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder ["Dsn"] = null;
			Assert.AreEqual (string.Empty, builder.Dsn, "#A1");
			Assert.AreEqual (string.Empty, builder ["Dsn"], "#A2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#A3");
			builder.Dsn = "X";
			Assert.AreEqual ("X", builder.Dsn, "#B1");
			Assert.AreEqual ("X", builder ["Dsn"], "#B2");
			Assert.AreEqual ("X", builder ["dsN"], "#B3");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#B4");
			Assert.IsTrue (builder.ContainsKey ("drivEr"), "#B5");
			builder ["Dsn"] = null;
			Assert.AreEqual (string.Empty, builder.Dsn, "#C1");
			Assert.AreEqual (string.Empty, builder ["Dsn"], "#C2");
			builder ["Dsn"] = "A";
			Assert.AreEqual ("A", builder.Dsn, "#D1");
			Assert.AreEqual ("A", builder ["Dsn"], "#D2");
			builder ["Dsn"] = null;
			Assert.AreEqual (string.Empty, builder.Dsn, "#E1");
			Assert.AreEqual (string.Empty, builder ["Dsn"], "#E2");
		}
		private static DbConnectionStringBuilder GetConnectionStringBuilder(LastDirectConnectionString connectionString)
		{
			if (connectionString.IsODBC)
			{
				return new OdbcConnectionStringBuilder(connectionString.ConnectionString);
			}

			DbConnectionStringBuilder builder;
			QuerySource               dbType;

			if (Enum.TryParse(connectionString.DataBaseType, out dbType))
			{
				switch (dbType)
				{
					case QuerySource.MSSQL:
						builder = new SqlConnectionStringBuilder();
						break;
					case QuerySource.SQLite:
						// ODBC connection string builder is used
						// for SQLite internal connection
						// since it handles special connection string adequately
						builder = new OdbcConnectionStringBuilder();
						break;
					default:
						builder = new DbConnectionStringBuilder();
						break;
				}
			}
			else
			{
				builder = new DbConnectionStringBuilder();
			}

			builder.ConnectionString = connectionString.ConnectionString;
			return builder;
		}
		public void Indexer_Value_Null ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder ["DriverID"] = null;
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#A1");
			try {
				object value = builder ["DriverID"];
				Assert.Fail ("#A2:" + value);
			} catch (ArgumentException ex) {
				// Keyword not supported: 'DriverID'
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A3");
				Assert.IsNull (ex.InnerException, "#A4");
				Assert.IsNotNull (ex.Message, "#A5");
				Assert.IsTrue (ex.Message.IndexOf ("'DriverID'") != -1, "#A6:" + ex.Message);
				Assert.IsNull (ex.ParamName, "#A7");
			}
			Assert.IsFalse (builder.ContainsKey ("DriverID"), "#A8");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#A9");

			builder ["DriverID"] = "A";
			Assert.AreEqual ("DriverID=A", builder.ConnectionString, "#B1");
			builder ["DriverID"] = null;
			Assert.IsFalse (builder.ContainsKey ("DriverID"), "#B2");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#B3");
		}
Ejemplo n.º 18
0
 private InstanceDescriptor ConvertToInstanceDescriptor(OdbcConnectionStringBuilder options)
 {
     Type[] types = new Type[] { typeof(string) };
     return(new InstanceDescriptor(typeof(OdbcConnectionStringBuilder).GetConstructor(types), new object[] { options.ConnectionString }));
 }
		protected static DbConnectionStringBuilder GetConnectionStringBuilder(
			QuerySource source,
			string      connectionString,
			bool        isOdbc
		)
		{
			if (isOdbc)
			{
				return new OdbcConnectionStringBuilder(connectionString);
			}

			DbConnectionStringBuilder builder;

			switch (source)
			{
				case QuerySource.MSSQL:
					builder = new SqlConnectionStringBuilder();
					break;

				case QuerySource.SQLite:
					// ODBC connection string builder is used
					// for SQLite internal connection
					// since it handles special connection string adequately
					builder = new OdbcConnectionStringBuilder();
					break;

				default:
					builder = new DbConnectionStringBuilder();
					break;
			}

			builder.ConnectionString = connectionString;

			return builder;
		}
		public void TryGetValue ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			object value = null;

			builder ["DriverID"] = "790";
			builder.Add ("Server", "C:\\");
			Assert.IsTrue (builder.TryGetValue ("DriverID", out value), "#A1");
			Assert.AreEqual ("790", value, "#A2");
			Assert.IsTrue (builder.TryGetValue ("SERVER", out value), "#B1");
			Assert.AreEqual ("C:\\", value, "#B2");
			Assert.IsFalse (builder.TryGetValue (string.Empty, out value), "#C1");
			Assert.IsNull (value, "#C2");
			Assert.IsFalse (builder.TryGetValue ("a;", out value), "#D1");
			Assert.IsNull (value, "#D2");
			Assert.IsFalse (builder.TryGetValue ("\r", out value), "#E1");
			Assert.IsNull (value, "#E2");
			Assert.IsFalse (builder.TryGetValue (" ", out value), "#F1");
			Assert.IsNull (value, "#F2");
			Assert.IsFalse (builder.TryGetValue ("doesnotexist", out value), "#G1");
			Assert.IsNull (value, "#G2");
			Assert.IsTrue (builder.TryGetValue ("Driver", out value), "#H1");
			Assert.AreEqual (string.Empty, value, "#H2");
			Assert.IsTrue (builder.TryGetValue ("Dsn", out value), "#I1");
			Assert.AreEqual (string.Empty, value, "#I2");
			builder ["Driver"] = "SQL Server";
			Assert.IsTrue (builder.TryGetValue ("Driver", out value), "#J1");
			Assert.AreEqual ("SQL Server", value, "#J2");
			builder.Dsn = "AdventureWorks";
			Assert.IsTrue (builder.TryGetValue ("Dsn", out value), "#K1");
			Assert.AreEqual ("AdventureWorks", value, "#K2");
		}
		public void TryGetValue_Keyword_Null ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			object value = null;
			try {
				builder.TryGetValue (null, out value);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("keyword", ex.ParamName, "#5");
			}
		}
		public void Dsn ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#A1");
			Assert.AreEqual (string.Empty, builder ["Dsn"], "#A2");
			Assert.AreEqual (string.Empty, builder.Dsn, "#A3");

			builder.Dsn = "myDsn";
			Assert.AreEqual ("Dsn=myDsn", builder.ConnectionString, "#B1");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#B2");
			Assert.AreEqual ("myDsn", builder ["Dsn"], "#B3");
			Assert.AreEqual ("myDsn", builder.Dsn, "#B4");

			builder.Clear ();

			builder ["Dsn"] = "myDsn";
			Assert.AreEqual ("Dsn=myDsn", builder.ConnectionString, "#C1");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#C2");
			Assert.AreEqual ("myDsn", builder ["Dsn"], "#C3");
			Assert.AreEqual ("myDsn", builder.Dsn, "#C4");
		}
		public void Remove ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			Assert.IsFalse (builder.Remove ("Dsn"), "#A1");
			Assert.IsFalse (builder.Remove ("Driver"), "#A2");
			builder.Add ("DriverID", "790");
			builder ["DefaultDir"] = "C:\\";
			Assert.IsTrue (builder.Remove ("DriverID"), "#B1");
			Assert.IsFalse (builder.ContainsKey ("DriverID"), "#B2");
			Assert.IsFalse (builder.Remove ("DriverID"), "#B3");
			Assert.IsFalse (builder.ContainsKey ("DriverID"), "#B4");
			Assert.IsTrue (builder.Remove ("defaulTdIr"), "#B5");
			Assert.IsFalse (builder.ContainsKey ("DefaultDir"), "#B6");
			Assert.IsFalse (builder.Remove ("defaulTdIr"), "#B7");
			Assert.IsFalse (builder.Remove ("userid"), "#B8");
			Assert.IsFalse (builder.Remove (string.Empty), "#B9");
			Assert.IsFalse (builder.Remove ("\r"), "#B10");
			Assert.IsFalse (builder.Remove ("a;"), "#B11");
			builder.Dsn = "myDsn";
			Assert.IsTrue (builder.Remove ("dSn"), "#C1");
			Assert.IsTrue (builder.ContainsKey ("dSn"), "#C2");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#C3");
			Assert.AreEqual (string.Empty, builder.Dsn, "#C4");
			Assert.IsFalse (builder.Remove ("Dsn"), "#C5");
			builder.Driver = "SQL Server";
			Assert.IsTrue (builder.Remove ("driVer"), "#D1");
			Assert.IsTrue (builder.ContainsKey ("driVer"), "#D2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#D3");
			Assert.AreEqual (string.Empty, builder.Driver, "#D4");
			Assert.IsFalse (builder.Remove ("Driver"), "#D5");
			builder ["Dsn"] = "myDsn";
			Assert.IsTrue (builder.Remove ("Dsn"), "#E1");
			Assert.IsTrue (builder.ContainsKey ("Dsn"), "#E2");
			Assert.AreEqual (string.Empty, builder.Dsn, "#E3");
			Assert.IsFalse (builder.Remove ("Dsn"), "#E4");
			builder ["Driver"] = "SQL Server";
			Assert.IsTrue (builder.Remove ("Driver"), "#F1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#F2");
			Assert.AreEqual (string.Empty, builder.Driver, "#F3");
			Assert.IsFalse (builder.Remove ("Driver"), "#F4");
		}
		public void Keys ()
		{
			OdbcConnectionStringBuilder builder;
			ICollection keys;
			object [] keylist;

			builder = new OdbcConnectionStringBuilder ();
			keys = builder.Keys;
			Assert.IsNotNull (keys, "#A1");
			Assert.AreEqual (2, keys.Count, "#A2");
			keylist = new object [keys.Count];
			keys.CopyTo (keylist, 0);
			Assert.AreEqual (2, keylist.Length, "#A3");
			Assert.AreEqual ("Dsn", keylist [0], "#A4");
			Assert.AreEqual ("Driver", keylist [1], "#A5");

			builder = new OdbcConnectionStringBuilder ("Database=test;Driver=SQL Server;dsn=AdventureWorks");
			keys = builder.Keys;
			Assert.IsNotNull (keys, "#B1");
			Assert.AreEqual (3, keys.Count, "#B2");
			keylist = new object [keys.Count];
			keys.CopyTo (keylist, 0);
			Assert.AreEqual (3, keylist.Length, "#B3");
			Assert.AreEqual ("Dsn", keylist [0], "#B4");
			Assert.AreEqual ("Driver", keylist [1], "#B5");
			Assert.AreEqual ("database", keylist [2], "#B6");

			builder = new OdbcConnectionStringBuilder ("Driver=SQL Server;dsn=AdventureWorks;Database=test;Port=");
			keys = builder.Keys;
			Assert.IsNotNull (keys, "#C1");
			Assert.AreEqual (3, keys.Count, "#C2");
			keylist = new object [keys.Count];
			keys.CopyTo (keylist, 0);
			Assert.AreEqual (3, keylist.Length, "#C3");
			Assert.AreEqual ("Dsn", keylist [0], "#C4");
			Assert.AreEqual ("Driver", keylist [1], "#C5");
			Assert.AreEqual ("database", keylist [2], "#C6");

			builder = new OdbcConnectionStringBuilder ();
			builder ["DataBase"] = "test";
			builder.Driver = "SQL Server";
			builder ["User"] = "******";
			builder ["porT"] = "25";
			keys = builder.Keys;
			Assert.IsNotNull (keys, "#D1");
			Assert.AreEqual (5, keys.Count, "#D2");
			keylist = new object [keys.Count];
			keys.CopyTo (keylist, 0);
			Assert.AreEqual (5, keylist.Length, "#D3");
			Assert.AreEqual ("Dsn", keylist [0], "#D4");
			Assert.AreEqual ("Driver", keylist [1], "#D5");
			Assert.AreEqual ("DataBase", keylist [2], "#D6");
			Assert.AreEqual ("User", keylist [3], "#D7");
			Assert.AreEqual ("porT", keylist [4], "#D8");

			builder.Clear ();

			keys = builder.Keys;
			Assert.IsNotNull (keys, "#E1");
			Assert.AreEqual (2, keys.Count, "#E2");
			keylist = new object [keys.Count];
			keys.CopyTo (keylist, 0);
			Assert.AreEqual (2, keylist.Length, "#E3");
			Assert.AreEqual ("Dsn", keylist [0], "#E4");
			Assert.AreEqual ("Driver", keylist [1], "#E5");
		}
		public void Indexer_Driver_Null ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			builder ["Driver"] = null;
			Assert.AreEqual (string.Empty, builder.Driver, "#A1");
			Assert.AreEqual (string.Empty, builder ["Driver"], "#A2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#A3");
			Assert.IsTrue (builder.ContainsKey ("drivEr"), "#A4");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#A5");
			builder.Driver = "X";
			Assert.AreEqual ("X", builder.Driver, "#B1");
			Assert.AreEqual ("X", builder ["Driver"], "#B2");
			Assert.AreEqual ("X", builder ["driVer"], "#B3");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#B4");
			Assert.IsTrue (builder.ContainsKey ("drivEr"), "#B5");
			Assert.AreEqual ("Driver={X}", builder.ConnectionString, "#B6");
			builder ["Driver"] = null;
			Assert.AreEqual (string.Empty, builder.Driver, "#C1");
			Assert.AreEqual (string.Empty, builder ["Driver"], "#C2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#C3");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#C4");
			builder ["Driver"] = "A";
			Assert.AreEqual ("A", builder.Driver, "#D1");
			Assert.AreEqual ("A", builder ["Driver"], "#D2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#D3");
			Assert.AreEqual ("Driver={A}", builder.ConnectionString, "#D4");
			builder ["Driver"] = null;
			Assert.AreEqual (string.Empty, builder.Driver, "#E1");
			Assert.AreEqual (string.Empty, builder ["Driver"], "#E2");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#E3");
			Assert.AreEqual (string.Empty, builder.ConnectionString, "#E4");
		}
 public IDatabaseConfigurationProperties WithConnectionString(OdbcConnectionStringBuilder builder)
 {
     return base.WithConnectionString(builder);
 }
Ejemplo n.º 27
0
 public static string GetValue(this string odbcString, string key) {
     var odbc = new OdbcConnectionStringBuilder(odbcString);
     return odbc.GetValue(key);
 }
        //***************************************************************************
        // 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
        }
		public void Driver ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#A1");
			Assert.AreEqual (string.Empty, builder ["Driver"], "#A2");
			Assert.AreEqual (string.Empty, builder.Driver, "#A3");

			builder.Driver = "SQL Server";
			Assert.AreEqual ("Driver={SQL Server}", builder.ConnectionString, "#B1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#B2");
			Assert.AreEqual ("SQL Server", builder ["Driver"], "#B3");
			Assert.AreEqual ("SQL Server", builder.Driver, "#B4");

			builder.Clear ();

			builder.Driver = "{SQL Server";
			Assert.AreEqual ("Driver={{SQL Server}", builder.ConnectionString, "#C1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#C2");
			Assert.AreEqual ("{SQL Server", builder ["Driver"], "#C3");
			Assert.AreEqual ("{SQL Server", builder.Driver, "#C4");

			builder.Clear ();

			builder.Driver = "{SQL Server}";
			Assert.AreEqual ("Driver={SQL Server}", builder.ConnectionString, "#D1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#D2");
			Assert.AreEqual ("{SQL Server}", builder ["Driver"], "#D3");
			Assert.AreEqual ("{SQL Server}", builder.Driver, "#D4");

			builder.Clear ();

			builder.Driver = string.Empty;
			Assert.AreEqual ("Driver=", builder.ConnectionString, "#E1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#E2");
			Assert.AreEqual (string.Empty, builder ["Driver"], "#E3");
			Assert.AreEqual (string.Empty, builder.Driver, "#E4");

			builder.Clear ();

			builder ["Driver"] = "SQL Server";
			Assert.AreEqual ("Driver={SQL Server}", builder.ConnectionString, "#F1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#F2");
			Assert.AreEqual ("SQL Server", builder ["Driver"], "#F3");
			Assert.AreEqual ("SQL Server", builder.Driver, "#F4");

			builder.Clear ();

			builder ["Driver"] = "{SQL Server";
			Assert.AreEqual ("Driver={{SQL Server}", builder.ConnectionString, "#G1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#G2");
			Assert.AreEqual ("{SQL Server", builder ["Driver"], "#G3");
			Assert.AreEqual ("{SQL Server", builder.Driver, "#G4");

			builder.Clear ();

			builder ["Driver"] = "{SQL Server}";
			Assert.AreEqual ("Driver={SQL Server}", builder.ConnectionString, "#H1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#H2");
			Assert.AreEqual ("{SQL Server}", builder ["Driver"], "#H3");
			Assert.AreEqual ("{SQL Server}", builder.Driver, "#H4");

			builder.Clear ();

			builder ["Driver"] = string.Empty;
			Assert.AreEqual ("Driver=", builder.ConnectionString, "#I1");
			Assert.IsTrue (builder.ContainsKey ("Driver"), "#I2");
			Assert.AreEqual (string.Empty, builder ["Driver"], "#I3");
			Assert.AreEqual (string.Empty, builder.Driver, "#I4");
		}
        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();
        }
Ejemplo n.º 31
0
 private System.ComponentModel.Design.Serialization.InstanceDescriptor ConvertToInstanceDescriptor(OdbcConnectionStringBuilder options)
 {
     Type[]   ctorParams = new Type[] { typeof(string) };
     object[] ctorValues = new object[] { options.ConnectionString };
     System.Reflection.ConstructorInfo ctor = typeof(OdbcConnectionStringBuilder).GetConstructor(ctorParams);
     return(new System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, ctorValues));
 }
 private System.ComponentModel.Design.Serialization.InstanceDescriptor ConvertToInstanceDescriptor(OdbcConnectionStringBuilder options) {
     Type[] ctorParams = new Type[] { typeof(string) };
     object[] ctorValues = new object[] { options.ConnectionString };
     System.Reflection.ConstructorInfo ctor = typeof(OdbcConnectionStringBuilder).GetConstructor(ctorParams);
     return new System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, ctorValues);
 }
Ejemplo n.º 33
0
 /// <summary>
 /// Base constructor that takes a ConnectionStringBuilder and initializes the ConnectionStringBuilder with it.
 /// </summary>
 /// <param name="ConnectionStringBuilder">ConnectionStringBuilder with a connectionstring (preferably) to connect to a database.</param>
 public DBClassOdbc(OdbcConnectionStringBuilder ConnectionStringBuilder)
 {
     ConnStringBuild = ConnectionStringBuilder;
 }
		public void Dsn_Value_Null ()
		{
			OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder ();
			try {
				builder.Dsn = null;
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("Dsn", ex.ParamName, "#5");
			}
		}