Csharp run sql script create database

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; usin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using Microsoft.SqlServer.Management.Common;//引用Microsoft.SqlServer.ConnectionInfo
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Nmo;
using System.IO;
using System.ServiceProcess;//添加引用

namespace Skype
{
    /// <summary>
    /// 20120813 塗聚文 Geovin Du
    /// csharp run sql script create database
    /// </summary>
    public partial class InstallingSQLForm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public InstallingSQLForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 用腳本安裝SQL Server 2005數據庫
        /// 20120813 塗聚文 締友計算機信息技術有限公司
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InstallingSQLForm_Load(object sender, EventArgs e)
        {
            
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Databases", typeof(string));
            dt.Columns.Add("Tables", typeof(string));
            try
            {
                


                Server server = new Server(@"B2FC96ADD7DC472\GEOVINDU");
                Database dbMaster = server.Databases["Master"];
                string strScriptDir = string.Empty;
                string strInstnwd = "instnwd.sql";
                string strInstpub = "instpubs.sql";
                strScriptDir = @"C:\filese\" + @"setup\";
                if (!server.Databases.Contains("Northwind"))
                {
                    if (File.Exists(strScriptDir + strInstnwd))
                    {
                        MessageBox.Show("創建數據庫Northwind。。。");
                        StreamReader rdr = new StreamReader(strScriptDir + strInstnwd);
                        dbMaster.ExecuteNonQuery(rdr.ReadToEnd());
                    }
                }
                if (!server.Databases.Contains("pubs"))
                {
                    if (File.Exists(strScriptDir + strInstpub))
                    {
                        MessageBox.Show("創建數據庫pubs。。。");
                        StreamReader rdr = new StreamReader(strScriptDir + strInstpub);
                        dbMaster.ExecuteNonQuery(rdr.ReadToEnd());
                    }
                }
                //顯示所有數據庫及數據庫的表;
                //如果是脫機狀態,後面的數據庫讀不出來,會報錯。如何判斷數據庫是脫機狀態呢?
                //20120813 塗聚文 締友計算機信息技術有限公司
                int i = 0;
                foreach (Database db in server.Databases)
                {
                    //db.Name;
                    

                    if (db.Tables.Count > 0)
                    {
                        foreach (Table tbl in db.Tables)
                        {
                            //tbl.Name;
                            dt.Rows.Add(i, db.Name, tbl.Name);
                            i++;
                        }
                    }
                    else
                    {
                        dt.Rows.Add(i, db.Name, null);
                        i++;
                    }
                }

                this.dataGridView1.DataSource = dt;
         
            }
            catch (SqlServerManagementException ex)
            {
                this.dataGridView1.DataSource = dt;
                ex.Message.ToString();
            }

        }

        /// <summary>
        /// 判断数据库服务是否已经启动,如果已经启动就返回True,否则返回False
        /// </summary>
        /// <returns></returns>
        private bool DBServerStatus()
        {
            bool ExistFlag = false;
            ServiceController[] service = ServiceController.GetServices();
            for (int i = 0; i < service.Length; i++)
            {
                if (service[i].ServiceName.ToString().Contains(@"B2FC96ADD7DC472\GEOVINDU"))
                {
                    ExistFlag = true;
                    string strOuput = string.Format("数据库服务器启动了服务名:{0},服务显示名:{1}\n", service[i].ServiceName, service[i].DisplayName);
                    MessageBox.Show(strOuput);
                }
            }
            return ExistFlag;
        }



    }
}

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
SQL 数据库 存储
Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor
原文:Sql性能检测工具:Sql server profiler和优化工具:Database Engine Tuning Advisor 一、工具概要     数据库应用系统性能低下,需要对其进行优化,     如果不知道问题出在哪里,可以使用性能检测工具sql server profiler。
2106 0
|
9月前
|
关系型数据库 MySQL 数据库
java.sql.SQLException: Connections could not be acquired from the underlying database!
java.sql.SQLException: Connections could not be acquired from the underlying database!
150 0
|
11月前
|
SQL 人工智能 自然语言处理
当LLM遇到Database:阿里达摩院联合HKU推出Text-to-SQL新基准​(1)
当LLM遇到Database:阿里达摩院联合HKU推出Text-to-SQL新基准​
2278 0
|
11月前
|
SQL 自然语言处理 达摩院
当LLM遇到Database:阿里达摩院联合HKU推出Text-to-SQL新基准​(2)
当LLM遇到Database:阿里达摩院联合HKU推出Text-to-SQL新基准​
1345 1
|
11月前
|
SQL 存储 安全
在 Go 中如何使用 database/sql 来操作数据库
在 Go 中如何使用 database/sql 来操作数据库
187 0
|
SQL 关系型数据库 MySQL
设计模式之Database/SQL与GORM实践|青训营笔记
本篇笔记暂时的定位是以介绍两种方式操作(MySQL)数据库为主,并辅以一些源码的解读帮助大家更好理解数据库连接的过程。
223 0
设计模式之Database/SQL与GORM实践|青训营笔记
|
SQL 存储 自然语言处理
Database Inside 系列 ——SQL 是如何执行的
Database Inside 系列 ——SQL 是如何执行的
104 0
Database Inside 系列 ——SQL 是如何执行的
|
存储 SQL 移动开发
WEB本地存储:localStorage、Web SQL Database、IndexedDB
在HTML5之前,应用程序数据只能存储在 cookie 中,并且会包含在每个服务器请求中。与 cookie 不同,浏览器本地存储限制要大得多(至少5MB),并且信息不会被传输到服务器。本文将要介绍的本地存储包括:localStorage、Web SQL Database、IndexedDB。
178 0
WEB本地存储:localStorage、Web SQL Database、IndexedDB
|
Java 关系型数据库 MySQL
报错:java.sql.SQLNonTransientConnectionException: Could not create connection to database server
报错:java.sql.SQLNonTransientConnectionException: Could not create connection to database server
1658 0
报错:java.sql.SQLNonTransientConnectionException: Could not create connection to database server
|
SQL Java 关系型数据库
spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete
spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete
123 0
spring boot集成mybatis只剩两个sql 并提示 Cannot obtain primary key information from the database, generated objects may be incomplete