Beispiel #1
0
        /// <summary>
        /// Constructs a test database instance along with the underlying
        /// database.
        /// </summary>
        /// <param name="database">The database name.</param>
        /// <remarks>
        /// If a database with this name already exists on the server then it
        /// will be deleted.  Then an empty database with this name will be
        /// created.
        /// </remarks>
        private SqlTestDatabase(string database)
        {
            SqlContext ctx;
            SqlCommand cmd;
            DataTable  dt;

            this.database = database;

            // Connect the the database server and the MASTER database to
            // handle the creation of the database.

            conInfo          = new SqlConnectionInfo(conString);
            conInfo.Database = "MASTER";

            ctx = new SqlContext(conInfo);
            ctx.Open();

            try
            {
                // Delete any existing database with this name

                cmd = ctx.CreateCommand("exec sp_databases");
                dt  = ctx.ExecuteTable(cmd);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (String.Compare(SqlHelper.AsString(dt.Rows[i]["DATABASE_NAME"]), database, true) == 0)
                    {
                        OdbcConnection.ReleaseObjectPool(); // This necessary because any cached connections
                        SqlConnection.ClearAllPools();      // to this database will prevent us from
                                                            // dropping the database.

                        cmd = ctx.CreateCommand("drop database [{0}]", database);
                        ctx.Execute(cmd);
                        break;
                    }
                }

                // Create the database

                cmd = ctx.CreateCommand("create database [{0}]", database);
                ctx.Execute(cmd);
                dbExists = true;

                // Set the connection information to reference the database.

                conInfo.Database = database;
            }
            finally
            {
                ctx.Close();
            }
        }
Beispiel #2
0
 /// <summary>
 /// Normalizes the SQL connection string passed.
 /// </summary>
 /// <param name="conString">The input string.</param>
 /// <returns>The normalized version of the input..</returns>
 public static string Normalize(string conString)
 {
     return(SqlConnectionInfo.Parse(conString).ToString());
 }