创建SQLServer数据库和数据表

#region 创建SQLServer数据库
        private int CreateDataBase()
        {
            string strCon;
            int nReturn = 1;
            System.Data.OleDb.OleDbConnection myConn = null;
            System.Data.OleDb.OleDbCommand sqlCommand = new System.Data.OleDb.OleDbCommand();
            try
            {
                strCon = "Provider = SQLOLEDB.1;User ID = " + BaseClass.GlobalShare.UserID + ";PassWord = " + BaseClass.GlobalShare.Pwd
                    + ";Initial Catalog = master; Data Source = " + BaseClass.GlobalShare.Server;
                myConn = new System.Data.OleDb.OleDbConnection(strCon);
                myConn.Open();
                sqlCommand.Connection = myConn;
                //sqlCommand.CommandText = "Create DataBase /"" + ConstShare.DataBaseName_Info + "/" Collate Chinese_PRC_CI_AS";
                sqlCommand.CommandText = "Create DataBase LICENCE Collate Chinese_PRC_CI_AS";
                sqlCommand.ExecuteNonQuery();
                myConn.Close();

                strCon = "Provider = SQLOLEDB.1;User ID = " + BaseClass.GlobalShare.UserID + ";PassWord = " + BaseClass.GlobalShare.Pwd
                    + ";Initial Catalog = LICENCE; Data Source = " + BaseClass.GlobalShare.Server;
                myConn = new System.Data.OleDb.OleDbConnection(strCon);
                myConn.Open();
                sqlCommand.Connection = myConn;

                // 创建数据表
                if (ExecSQLFile("CreatTable.txt", sqlCommand) == 0)
                    nReturn = 0;

            }
            catch (Exception e)
            {
                nReturn = 0;
            }
            finally
            {
                myConn.Close();
                myConn.Dispose();
                sqlCommand.Dispose();
            }

            return nReturn;
        }
        #endregion

        #region 创建数据表(读取文件中的SQL语句并执行)
        private int ExecSQLFile(string FileName, System.Data.OleDb.OleDbCommand tmpCommand)
        {
            StreamReader objReader = new StreamReader(Application.StartupPath + Path.DirectorySeparatorChar + FileName, System.Text.Encoding.Unicode);
            string sLine = "";
            string stmpSQL = "";
            int nReturn = 1;
            try
            {
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine == null)
                    {
                        break;
                    }
                    if (sLine.Trim().ToUpper().Equals("GO"))
                    {
                        tmpCommand.CommandText = stmpSQL;
                        tmpCommand.CommandTimeout = 0;
                        tmpCommand.ExecuteNonQuery();
                        stmpSQL = "";
                    }
                    else if (sLine.Trim() != "")
                    {
                        stmpSQL = String.Format("{0}/n{1}", stmpSQL, sLine);
                        //stmpSQL = stmpSQL + " " + sLine ;
                    }
                }
            }
            catch (Exception e)
            {
                nReturn = 0;
            }
            finally
            {
                objReader.Close();
            }

            return nReturn;
        }
        #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值