Ejemplo n.º 1
0
        static EmployeeProvider()
        {
            // Use LocalDB, and create a Database.mdf file in the
            // aplication folder if it does not exist.
            server           = "(LocalDB)\\MSSQLLocalDB";
            directory        = BaseDir;
            dbName           = "Database";
            connectionString = "Data Source=" + server + ";AttachDbFilename=\"" + directory + dbName + ".mdf\";Initial Catalog = " + MyDataContext.Generate_Identifier_Name(directory, dbName) + ";Integrated Security=True;";

            // If MDF file does not exist, create database
            if (!File.Exists(directory + dbName + ".mdf"))
            {
                using (MyDataContext db = new MyDataContext("Data Source=" + server + ";"))
                {
                    if (!db.CreateDatabase(directory, dbName))
                    {
                        throw new ApplicationException("Database creation error");
                    }
                }
            }
            // If Datatable does not exist, create and initialize it.
            using (MyDataContext db = new MyDataContext(connectionString))
            {
                if (!db.DatabaseExists())
                {
                    throw new ApplicationException("Database connection error. ConnectionString: " + connectionString);
                }
                if (!db.TableExists <EmployeeEntity>())
                {
                    db.CreateTable <EmployeeEntity>();
                    db.Employees.InsertOnSubmit(
                        new EmployeeEntity()
                    {
                        Id          = 1,
                        Name        = "Mark",
                        Gender      = "Male",
                        DateOfBirth = DateTime.Parse("10/10/1980")
                    });
                    db.Employees.InsertOnSubmit(
                        new EmployeeEntity()
                    {
                        Id          = 2,
                        Name        = "Mary",
                        Gender      = "Female",
                        DateOfBirth = DateTime.Parse("11/10/1981")
                    });
                    db.Employees.InsertOnSubmit(
                        new EmployeeEntity()
                    {
                        Id          = 3,
                        Name        = "John",
                        Gender      = "Male",
                        DateOfBirth = DateTime.Parse("8/10/1979")
                    });
                    db.SubmitChanges();
                }
            }
        }
Ejemplo n.º 2
0
        public static bool Test()
        {
            bool lTest;

            using (MyDataContext db = new MyDataContext(connectionString))
            {
                lTest = db.DatabaseExists();
            }
            return(lTest);
        }