locked
Connecting via new connection string fails - strange behavior RRS feed

  • Question

  • Hi there :)

    I am trying to produce a little code, that checks, wheather my SQL server is connectable. And: if so, then it should check if the database is existing. Let me explain it via code.

    This is my main-method:

    private static void Main(string[] args)

            {

                Database.SetInitializer<MyDbContext>(new DropCreateDatabaseAlways<MyDbContext>());

                Console.WriteLine(CheckIfDatabaseExists("Data Source=127.0.0.1\\SQLEXPRESS2;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));

                Console.WriteLine(CheckIfDatabaseExists("Data Source=127.0.0.1\\SQLEXPRESS;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));

                Console.WriteLine(CheckIfDatabaseExists("Data Source=127.0.0.1\\SQLEXPRESS2;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));

            }

    As you can imagine: there is an existing database 'SQLTest' on my local SQL server 'SQLEXPRESS'. But: there is no server like 'SQLEXPRESS2' !

    Okay - more code. Here comes my checking method:

    public static DatabaseExistsStatus CheckIfDatabaseExists(String connString)

            {

              try

                {

                    using (var db = new MyDbContext(connString))

                    {

                        bool DbExists = db.Database.Exists();

                        if (DbExists)

                        {

                            // database is existing

                            return DatabaseExistsStatus.EXISTING;

                        }

                        else

                        {

                            // config is working, but database does not exist

                            return DatabaseExistsStatus.NO_DB;

                        }

                    }

                }

                catch (Exception)

                {

                    // no working config

                    return DatabaseExistsStatus.NO_CONNECTION;

                }

            }

            public enum DatabaseExistsStatus

            {

                EXISTING,

                NO_CONNECTION,

                NO_DB

            }

    Starting my app brings the following result:

    NO_CONNECTION

    EXISTING

    NO_DB

    I am confused! I expected "NO_CONNECTION, EXISTING, NO_CONNECTION" .

    And that's it!I have no idea what is happening in the background and how to get control.  What is happening there and how can I fix that? Anybody here who can help me?  Thx a lot. 

    Monday, April 23, 2012 7:09 AM

Answers

  • Hi DerFrix,

    Welcome!

    After running your code, please check whether the database has been created in the SQLEXPRESS2 instance.

    Unfortunately, I could repro your problem. here is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Database.SetInitializer<MyDbContext>(new DropCreateDatabaseAlways<MyDbContext>());
    
                //var db = new MyDbContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3");
    
               // db.Database.CreateIfNotExists();
                Console.WriteLine(CheckIfDatabaseExists(@"Data Source=ALANCHEN-PC;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));
    
                Console.WriteLine(CheckIfDatabaseExists(@"Data Source=.\SQLEXPRESS;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));
    
                Console.WriteLine(CheckIfDatabaseExists(@"Data Source=ALANCHEN-PC;Initial Catalog=SQLTest;Integrated Security=True;Connect Timeout=3"));
    
            }
            public static DatabaseExistsStatus CheckIfDatabaseExists(String connString)
            {
    
                try
                {
    
    
    
                    using (var db = new MyDbContext(connString))
                    {
    
                        bool DbExists = db.Database.Exists();
    
                        if (DbExists)
                        {
    
                            // database is existing
    
                            return DatabaseExistsStatus.EXISTING;
    
                        }
    
                        else
                        {
    
                            // config is working, but database does not exist
    
                            return DatabaseExistsStatus.NO_DB;
    
                        }
    
                    }
    
                }
    
                catch (Exception)
                {
    
                    // no working config
                    return DatabaseExistsStatus.NO_CONNECTION;
                }
            }
    
            public enum DatabaseExistsStatus
            {
    
                EXISTING,
    
                NO_CONNECTION,
    
                NO_DB
    
            }
    
        }
        public class Blog
        {
            public int ID { get; set; }
            public string  Name { get; set; }
        }
        public class MyDbContext:DbContext
        {
            public MyDbContext(string conn):base(conn)
            { }
            public DbSet<Blog> Blogs { get; set; }
        }
    }
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Alan_chen Tuesday, April 24, 2012 1:13 PM
    • Marked as answer by Alan_chen Monday, May 7, 2012 7:04 AM
    Tuesday, April 24, 2012 12:53 PM