Ejemplo n.º 1
0
        private void btnTesst_Click(object sender, EventArgs e)
        {
            SqlConnection conn = DBUtils.GetDBConnection_Master("master");

            try
            {
                conn.Open();

                string str = "CREATE DATABASE MyDatabase ON PRIMARY " +
                             "(NAME = MyDatabase_Data, " +
                             "FILENAME = 'D:\\MyDatabaseData.mdf', " +
                             "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
                             "LOG ON (NAME = MyDatabase_Log, " +
                             "FILENAME = 'D:\\MyDatabaseLog.ldf', " +
                             "SIZE = 1MB, " +
                             "MAXSIZE = 5MB, " +
                             "FILEGROWTH = 10%)";
                str = "CREATE DATABASE MyDatabase";
                DBUtils.ExecuteNonQuery(str, conn);
            }
            catch
            {
                MessageBox.Show("Không khởi tạo được CSDL");
            }
            finally
            {
                conn.Close();
            }
        }
Ejemplo n.º 2
0
        public void initDatabase(string nameDatabase)
        {
            SqlConnection conn_MAIN = DBUtils.GetDBConnection_Master("master");

            try
            {
                conn_MAIN.Open();

                string str = "CREATE DATABASE " + nameDatabase;
                DBUtils.ExecuteNonQuery(str, conn_MAIN);
                MessageBox.Show("Đã tạo thành công Database tên: " + nameDatabase + ", đang tiến hành khởi tạo dữ liệu.", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("Không khởi tạo được Database, vui lòng kiểm tra lại", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn_MAIN.Close();
            }

            SqlConnection conn = DBUtils.GetDBConnection_Master(nameDatabase);

            try
            {
                conn.Open();

                string _strFileName = Directory.GetCurrentDirectory() + @"\Co So Du Lieu\script.sql";
                string script       = File.ReadAllText(_strFileName);
                script = script.Replace("CoSoDuLieu2018_NguyenCongPhuc", nameDatabase);

                IEnumerable <string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                foreach (string commandString in commandStrings)
                {
                    if (commandString.Trim() != "")
                    {
                        new SqlCommand(commandString, conn).ExecuteNonQuery();
                    }
                }
                MessageBox.Show("Đã khởi tạo xong dữ liệu!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.tbTenCoSoDuLieu.Text = nameDatabase;
            }
            catch
            {
                MessageBox.Show("Không khởi tạo được Database, vui lòng kiểm tra lại", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn.Close();
            }
        }