C#之判断Mysql数据库是否存在

今天想实现一个功能,程序能自动创建数据库,采用C#实现。

涉及到的相关SQL语句如下:

判断数据库是否存在:

select Count(*) as A from information_schema.schemata where schema_name='test1'

创建数据库

CREATE DATABASE test1

部分代码如下:

            DBMysql dBMysql = new DBMysql();
            dBMysql.ConnectMySql("127.0.0.1", 3306, "root", "sa");

            string strSql = string.Format(@"select Count(*) as A from information_schema.schemata where schema_name='test1'");
            System.Data.DataSet dataSet = new DataSet();
            var bRet =  dBMysql.SqlExe(strSql,ref dataSet);
            if(false == bRet)
            {
                return;
            }
            if(1 !=  dataSet.Tables[0].Rows.Count)
            {
                return;
            }
            DataRow row = dataSet.Tables[0].Rows[0];
            var a = row["A"];
            if("0" == a.ToString())
            {
                string strCreat = string.Format(@"CREATE DATABASE test1");

                Console.WriteLine(dBMysql.SqlExe(strCreat) +" "+strCreat);
            }

效果检查:

 成功创建了数据库

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值