Exemplo n.º 1
0
        // 根据数据库模板的定义,创建一个数据库
        int CreateDatabase(RmsChannel channel,
            string strTemplateDir,
            string strDatabaseName,
            out string strError)
        {
            strError = "";

            int nRet = 0;

            List<string[]> logicNames = new List<string[]>();

            string[] cols = new string[2];
            cols[1] = "zh";
            cols[0] = strDatabaseName;
            logicNames.Add(cols);


            string strKeysDefFileName = PathUtil.MergePath(strTemplateDir, "keys");
            string strBrowseDefFileName = PathUtil.MergePath(strTemplateDir, "browse");

            nRet = ConvertGb2312TextfileToUtf8(strKeysDefFileName,
                out strError);
            if (nRet == -1)
                return -1;

            nRet = ConvertGb2312TextfileToUtf8(strBrowseDefFileName,
                out strError);
            if (nRet == -1)
                return -1;

            string strKeysDef = "";
            string strBrowseDef = "";

            StreamReader sr = null;

            try
            {
                sr = new StreamReader(strKeysDefFileName, Encoding.UTF8);
                strKeysDef = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception ex)
            {
                strError = "装载文件 " + strKeysDefFileName + " 时发生错误: " + ex.Message;
                return -1;
            }


            try
            {
                sr = new StreamReader(strBrowseDefFileName, Encoding.UTF8);
                strBrowseDef = sr.ReadToEnd();
                sr.Close();
            }
            catch (Exception ex)
            {
                strError = "装载文件 " + strBrowseDefFileName + " 时发生错误: " + ex.Message;
                return -1;
            }


            long lRet = channel.DoCreateDB(logicNames,
                "", // strType,
                "", // strSqlDbName,
                strKeysDef,
                strBrowseDef,
                out strError);
            if (lRet == -1)
            {
                strError = "创建数据库 " + strDatabaseName + " 时发生错误: " + strError;
                return -1;
            }

            lRet = channel.DoInitialDB(strDatabaseName,
                out strError);
            if (lRet == -1)
            {
                strError = "初始化数据库 " + strDatabaseName + " 时发生错误: " + strError;
                return -1;
            }

            // 增补其他数据从属对象

            /*
            List<string> subdirs = new List<string>();
            // 创建所有目录对象
            GetSubdirs(strTemplateDir, ref subdirs);
            for (int i = 0; i < subdirs.Count; i++)
            {
                string strDiskPath = subdirs[i];

                // 反过来推算为逻辑路径
                // 或者预先在获得的数组中就存放为部分(逻辑)路径?
                string strPath = "";

                // 在服务器端创建对象
                // parameters:
                //      strStyle    风格。当创建目录的时候,为"createdir",否则为空
                // return:
                //		-1	错误
                //		1	以及存在同名对象
                //		0	正常返回
                nRet = NewServerSideObject(
                    channel,
                    strPath,
                    "createdir",
                    null,
                    null,
                    out strError);
                if (nRet == -1)
                    return -1;
            }
                // 列出每个目录中的文件,并在服务器端创建之
                // 注意模板目录下的文件,被当作cfgs中的文件来创建
             * */

            DirectoryInfo di = new DirectoryInfo(strTemplateDir);
            FileInfo[] fis = di.GetFiles();

            // 创建所有文件对象
            for (int i = 0; i < fis.Length; i++)
            {
                string strName = fis[i].Name;
                if (strName == "." || strName == "..")
                    continue;

                if (strName.ToLower() == "keys"
                    || strName.ToLower() == "browse")
                    continue;

                string strFullPath = fis[i].FullName;

                nRet = ConvertGb2312TextfileToUtf8(strFullPath,
                    out strError);
                if (nRet == -1)
                    return -1;

                Stream s = new FileStream(strFullPath, FileMode.Open);

                try
                {
                    string strPath = strDatabaseName + "/cfgs/" + strName;




                    // 在服务器端创建对象
                    // parameters:
                    //      strStyle    风格。当创建目录的时候,为"createdir",否则为空
                    // return:
                    //		-1	错误
                    //		1	以及存在同名对象
                    //		0	正常返回
                    nRet = NewServerSideObject(
                        channel,
                        strPath,
                        "",
                        s,
                        null,
                        out strError);
                    if (nRet == -1)
                        return -1;
                }
                finally
                {
                    s.Close();
                }
            }



            return 0;
        }