Ejemplo n.º 1
0
        /// <summary>
        /// 获取指定实例内的所有数据库名。
        /// </summary>
        /// <param name="instanceName">实例名。如 SQL Server 所在计算机的IP地址</param>
        /// <param name="loginSecure">是否使用 Windows 身份验证方式。如不使用此方式则需输入登录名与密码</param>
        /// <param name="username">使用 SQL Server 身份验证方式时的登录用户名</param>
        /// <param name="password">使用 SQL Server 身份验证方式时的登录密码</param>
        /// <returns></returns>
        public static IEnumerable <string> GetSqlServerDatabases(string instanceName, bool loginSecure, string username, string password)
        {
            List <string> lsNames = new List <string>();

            SQLServerDb db = new SQLServerDb();

            db.InstanceName = instanceName;
            db.LoginSecure  = loginSecure;
            if (loginSecure == false)
            {
                db.UserName = username;
                db.Password = password;
            }
            db.DatabaseName = "master";

            System.Data.IDbConnection conn = db.GetDbConnection();
            conn.Open();

            System.Data.IDbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT name FROM sysdatabases";
            cmd.Connection  = conn;

            System.Data.IDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                lsNames.Add(Function.ToTrim(dr["name"]));
            }
            dr.Close();

            conn.Close();

            return(lsNames);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建 SQL Server 数据库。
        /// </summary>
        /// <param name="instanceName">数据库实例名。</param>
        /// <param name="loginSecure">是否使用 Windows 身份验证登录。</param>
        /// <param name="username">使用 SQL Server 身份验证时所使用的登录帐号。</param>
        /// <param name="password">使用 SQL Server 身份验证时所使用的密码。</param>
        /// <param name="databaseName">欲创建数据库的数据库名。</param>
        /// <param name="filePath">指定数据库文件路径。如传入 null 则使用默认路径。</param>
        /// <param name="dataFileSize">指定数据库的初始大小(MB)。</param>
        public static void CreateDatabase(string instanceName, bool loginSecure, string username, string password, string databaseName, string filePath, int dataFileSize)
        {
            SQLServerDb db = new SQLServerDb();

            db.InstanceName = instanceName;
            db.LoginSecure  = loginSecure;
            if (loginSecure == false)
            {
                db.UserName = username;
                db.Password = password;
            }
            db.DatabaseName = "master";

            System.Data.IDbConnection conn = db.GetDbConnection();
            conn.Open();

            System.Data.IDbCommand cmd = conn.CreateCommand();
            cmd.Connection = conn;

            string strSql;

            strSql = "CREATE DATABASE " + databaseName;

            if (filePath != null && filePath.Length > 0)
            {
                if (filePath.Substring(filePath.Length - 1).Equals("\\") == false)
                {
                    filePath += "\\";
                }
                strSql += " ON (NAME=" + databaseName + ",FILENAME='" + filePath + databaseName + ".mdf',";

                if (dataFileSize > 0)
                {
                    strSql += "SIZE=" + dataFileSize.ToString() + ",";
                }
                strSql += "FILEGROWTH=10%)";
            }
            cmd.CommandText = strSql;
            cmd.ExecuteNonQuery();

            conn.Close();
        }