创建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

你可能感兴趣的:(SQL,Server,sqlserver,数据库,exception,string,database,null)