我利用C#生成一个数据库!代码如下 /// <summary> 
        /// 创建连接起用进程建立数据库 
        /// </summary> 
        /// <returns></returns> 
        private bool execfile() 
        { 
            try 
            {
                string connStr = "server=.;uid=sa;pwd=sa;";
                ExecuteSql(connStr, "master", "CREATE DATABASE " + TextUserName.Text);//调用ExecuteNonQuery()来创建数据库 
                System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();//创建一个进程 
                sqlProcess.StartInfo.FileName = "osql.exe";//OSQL基于ODBC驱动连接服务器的一个实用工具(可查阅SQL帮助手册) 
                sqlProcess.StartInfo.Arguments = " -U sa -P sa -d SqlTest -i C:\\Program Files\\Microsoft SQL Server\\MSSQL\\Data";//获取启动程序时的参数 
                sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//调用进程的窗口状态,隐藏为后台 
                sqlProcess.Start(); 
                sqlProcess.WaitForExit(); 
                sqlProcess.Close(); 
                return true; 
            } 
            catch (Exception ex) 
            { 
                throw ex; 
            } 
        }         /// <summary> 
        /// 创建数据库,调用ExecuteNonQuery()执行 
        /// </summary> 
        /// <param name="conn"></param> 
        /// <param name="DatabaseName"></param> 
        /// <param name="Sql"></param> 
        private void ExecuteSql(string conn, string DatabaseName, string Sql) 
        { 
            System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn); 
            System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection); 
            Command.Connection.Open(); 
            Command.Connection.ChangeDatabase(DatabaseName); 
            try 
            { 
                Command.ExecuteNonQuery(); 
            } 
            finally 
            { 
                Command.Connection.Close(); 
            } 
        }该段代码是生成一个数据库。如何在生成数据库的同时生成表,每次创建一个数据库,所生成的表都是相同的,包括触发器!
请问该如何操作!    在线等!

解决方案 »

  1.   

    你可以先创建一个不含数据的母库,然后调用sp_attach_db直接附加新的库即可 http://msdn.microsoft.com/zh-cn/library/ms179877.aspx 
      

  2.   

    先创建数据库
    use 数据库
    再创建表
    也可直接附加数据库
      

  3.   

    use 数据库名
    create table ...
    或分两步,先数据库,再创建table
      

  4.   

    C# 创建数据库,不也是同样的道理将 所要创建的SQL语句 通过ADO.NET的方式执行与数据库。
      

  5.   

    全部用sql脚本执行,先创建数据库再创建表
      

  6.   

    利用C#在对SQL脚本进行操作吗?
      

  7.   

    你可以先创建一个不含数据的母库,然后调用sp_attach_db直接附加新的库即可 http://msdn.microsoft.com/zh-cn/library/ms179877.aspx 
      

  8.   

    以上是一个创建数据库的方法,你大概看一下思路。
    /// <summary>
            /// 名称:BtnNewDB_Click
            /// 功能:创建并初始化数据库
            /// </summary>
            private void BtnNewDB_Click(object sender, EventArgs e)
            {
                try
                {
                    if (saveConn)
                    {
                        DbName = this.txtNewDB.Text.ToString().Trim();
                        if (DbName == string.Empty)
                        {
                            CommonClass.MessageBoxOK("请输入初始化数据库名称!");
                            txtNewDB.Focus();
                            return;
                        }
                        else
                        {
                            bool boolean = false;
                            string strCreate = "create database " + DbName;
                            SqlCommand cmd = new SqlCommand(strCreate, conn);
                            conn.Open();
                            try
                            {
                                cmd.ExecuteNonQuery();
                                boolean = true;
                                string FilePath = Application.StartupPath + "\\sqlIntoDB.txt";
                                CommonClass.DecryptFile(FilePath, FilePath);
                                FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                                StreamReader m_streamReader = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
                                String strLine = @m_streamReader.ReadToEnd();
                                m_streamReader.Close();
                                CommonClass.EncryptFile(FilePath, FilePath);
                                string strConnTemp = "uid=" + CommonClass.ReadXml("User") + ";pwd=" + CommonClass.ReadXml("Password");
                                strConnTemp += ";initial catalog=" + DbName + ";Server=" + CommonClass.ReadXml("ServerIP") + ";";
                                strConnTemp += "Connect Timeout=30";
                                conn = new SqlConnection(strConnTemp);
                                string[] strtableprocedureinsert = strLine.Split(new string[] { "tableprocedureinsert" }, StringSplitOptions.None);
                                SqlCommand Command = new SqlCommand();
                                for (int typecount = 0; typecount < strtableprocedureinsert.Length; typecount++)
                                {
                                    if (typecount == 0 || typecount == 2)
                                    {
                                        Command = new SqlCommand(strtableprocedureinsert[typecount], conn);
                                        conn.Open();
                                        try
                                        {
                                            Command.ExecuteNonQuery();
                                        }
                                        finally
                                        {
                                            Command.Connection.Close();
                                        }
                                    }
                                    else
                                    {
                                        string strProc = strtableprocedureinsert[typecount].ToString();
                                        string[] tempProcArray = strProc.Split(new string[] { "sxjadzjsyxgs" }, StringSplitOptions.None);
                                        for (int a = 0; a < tempProcArray.Length; a++)
                                        {
                                            Command = new SqlCommand(tempProcArray[a], conn);
                                            conn.Open();
                                            try
                                            {
                                                Command.ExecuteNonQuery();
                                            }
                                            finally
                                            {
                                                Command.Connection.Close();
                                            }
                                        }
                                    }
                                }
                                CommonClass.MessageBoxOK("数据库初始化成功!");
                                this.Close();
                            }
                            catch
                            {
                                boolean = false;
                            }
                            finally
                            {
                                cmd.Connection.Close();
                            }
                            if (!boolean)
                            {
                                CommonClass.MessageBoxOK("数据库已存在!");
                                return;
                            }
                        }
                    }
                    else
                    {
                        CommonClass.MessageBoxOK("系统提示:\n\n请首先配置数据库并保存设置!");
                    }
                }
                catch { }
            }