private string GetTablesScript(string dtName)
        {
            try
            {
                SQLDMO.SQLServer oserver  = new SQLDMO.SQLServer();
                string           connStr  = KingTop.Common.SQLHelper.ConnectionStringLocalTransaction;
                string[]         arrConn  = connStr.Split(';');
                string           server   = string.Empty;
                string           database = string.Empty;
                string           login    = string.Empty;
                string           password = string.Empty;

                for (int i = 0; i < arrConn.Length; i++)
                {
                    string[] itemArr = arrConn[i].Split('=');
                    if (itemArr[0].ToLower() == "server")
                    {
                        server = itemArr[1];
                    }
                    else if (itemArr[0].ToLower() == "database")
                    {
                        database = itemArr[1];
                    }
                    else if (itemArr[0].ToLower() == "uid")
                    {
                        login = itemArr[1];
                    }
                    else if (itemArr[0].ToLower() == "pwd")
                    {
                        password = itemArr[1];
                    }
                }
                oserver.Connect(server, login, password);
                SQLDMO._Database mydb        = oserver.Databases.Item(database, "owner");
                SQLDMO._Table    mytable     = mydb.Tables.Item(dtName, "dbo");
                string           tableScript = mytable.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, null, null, SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default);

                tableScript = tableScript.Replace("[nvarchar] (0)", "[nvarchar] (max)");
                tableScript = tableScript.Replace("[varchar] (-1)", "[varchar] (max)");
                //去掉GO
                int lastPosNum = tableScript.LastIndexOf("GO");
                if (lastPosNum > 0)
                {
                    tableScript = tableScript.Substring(0, lastPosNum);
                }

                oserver.DisConnect();

                return(tableScript.Replace("'", "''"));
            }
            catch
            {
                Response.Write("<div align=center style='padding:20px'>导出失败,原因是sqldmo.dll未注册,注册方法如下:<br><br> 打开开始,在运行中输入 regsvr32 \"C:\\Program Files\\Microsoft SQL Server\\80\\Tools\\Binn\\sqldmo.dll\" 注册sqldmo.dll。<br><br>在注册前请确认sqldmo.dll是否存在,不存在请从网上下载sqldmo.dll到相应目录,再进行注册");
                Response.Write("<br><br><a href=# onclick='history.back();'>[返回]</a></div>");
                Response.End();
                return("");
            }
        }
예제 #2
0
        //生成表脚本
        public string CreateTableSql(string dbName, object tableName)
        {
            string s = string.Empty;

            try
            {
                SQLDMO._Database mydb    = GetDatabase(dbName);
                SQLDMO._Table    myTable = mydb.Tables.Item(tableName, "dbo");
                s = myTable.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript_Default, null, null, SQLDMO.SQLDMO_SCRIPT2_TYPE.SQLDMOScript2_Default);
                s = s.Replace("[nvarchar] (0)", "[nvarchar] (max)");
                s = s.Replace("[varchar] (-1)", "[varchar] (max)");
                //去掉GO
                int lastPosNum = s.LastIndexOf("GO");
                if (lastPosNum > 0)
                {
                    s = s.Substring(0, lastPosNum);
                }
            }
            catch
            {
            }
            return(s);
        }