基于Asp.Net 2.0(C#)和SQL Server 2005使用DSOFramer全攻略

昨天开始研究DSOFramer基于Asp.Net 2.0和SQL Server 2005的使用方法,百试不灵,aoao不爽,
已至深夜,遂混混然一觉天亮,今晨,灵感突发,渐入佳境,不觉间代码已成。



不扯犊子了,开整!

一、先注册一下DSOFramer.ocx 
  

    操作:将DSOFramer.ocx复制到C:/windows/system32目录下, 
  

         开始->运行->regsvr32 DSOFramer.ocx , 系统会提示DSOFramer.ocx中的DllRegisterServer成功。 
  

二、添加DSOFramer.ocx到你的项目中 
  

    操作:先说明一下,我用VS 2005 ,其他VS版本可能操作会有不同,操作应该也类似自己试试,问题应该不大。 
  
          在你要访问DSOFramer.ocx的目录上点选右键菜单中的“添加现有项”,找到DSOFramer.ocx,确定。 
  


三、在网页中加载DSOFramer 
  

     新建Office.aspx 
  

    添加如下代码: 
  
     <object id="MyOffice" name = "MyOffice" style="LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%" 
  
    classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57" codebase="dsoframer.ocx#version=2,2,0,0" > 
  
    </object> 
  

     [注]:VS 2005对语法的要求贼多,什么ID的值要用""括起来啦,<object>不能大写啦,…… 
  
           没什么大碍,但是很烦人,简直就是微软版的唐僧,我给大家提供的代码是修改过的,VS不会有哪些废话了。 
  

     然后再body中加入onload事件的处理函数 
  

                   <body οnlοad="show_word();"> 
  

     再在<head></head>中间加入函数体 
  


     <script language="javascript" type="text/javascript"> 
  
        <!-- 
  

        function show_word() { 
  
                var str=window.location.search; 
  
                var pos_start=str.indexOf("id")+3; 
  
                if (pos_start == 2) 
  
                return ; 
  
        
  
                var id = "http://localhost/Getdc.aspx?id=" + str.substring(pos_start); 
  
                document.all. MyOffice.Open( id,false, "Word.Document"); 
  
        } 
  

        // --> 
  
        </script> 
  


四、编制Getdc.aspx.cs文件 
  
      建立Getdc.aspx文件,VS会同时建立与之关联的Getdc.aspx.cs文件 
  

      先加入命名空间 
  
        using System.Data.SqlClient; 
  
        using System.Data.SqlTypes; 
  

      编辑Getdc.aspx.cs的Page_Load函数; 
  
      protected void Page_Load(object sender, EventArgs e) 
  
      { 
  
        int pid = Convert.ToInt32(Request["id"]); 
  

        SqlConnection myConnection = new SqlConnection("Data Source=/"localhost/";Initial Catalog=/"demo/";Persist Security Info=True;User ID=demo;Password=demo");//数据库的相关设置自己改吧,我就不废话了 
  
        SqlCommand mycommand = myConnection.CreateCommand(); 
  
        myConnection.Open(); 
  

        mycommand.CommandText = "SELECT filedata " + 
  
            " FROM Table_word WHERE (ID = " + pid.ToString() + ") ";//其中filedata的数据库类型是varbinary(MAX) 
  
        SqlDataReader myReader = mycommand.ExecuteReader(); 
  
        myReader.Read(); 
  
        SqlBinary binaryStream = myReader.GetSqlBinary(0); 
  
        myReader.Close(); 
  
        myConnection.Close(); 
  

        Response.BinaryWrite(binaryStream.Value); 
  
    } 
  

     至此,只要你指定的ID没问题,就应该可以加载Word文档了。 
  



五、建立保存文档的按钮 
  


        在Office.aspx中添加按钮 
  

        <input id="Button1" type="submit" value="保存" οnclick="return Submit_upload_onclick()" /> 
  

        再建立一个input 
  

            <input type="file" name="File" id = "File"/> 
  


        然后添加Submit_upload_onclick()函数: 
  

        function Submit_upload_onclick() { 
  
        var str=window.location.search; 
  
        var pos_start=str.indexOf("id")+3; 
  
        if (pos_start == 2) 
  
        return ; 
  

        document.all.MyOffice.HttpInit(); 
  
        document.all.MyOffice.HttpAddPostCurrFile("File", ""); 
  
        var id = "http://localhost/Savedc.aspx?id=" + str.substring(pos_start); 
  
        document.all.MyOffice.HttpPost(id); 
  
        } 
  

六、编制Savedc.aspx.cs文件 
  
      建立Savedc.aspx文件,VS会同时建立与之关联的Savedc.aspx.cs文件 
  

      先加入命名空间 
  
        using System.Data.SqlClient; 
  
        using System.Data.SqlTypes; 
  

      编辑Savedc.aspx.cs的Page_Load函数; 
  

    protected void Page_Load(object sender, EventArgs e) 
  
    { 
  
        int pid = Convert.ToInt32(Request["id"]); 
  
        Byte[] source_bin = Request.BinaryRead(Request.TotalBytes); 
  

        //--------------------------------------------------------------------------------------- 
  
       int i, loop, again = -1, file_begin = -1; 
  
         for (i = 0; i < Request.TotalBytes; i++) 
  
        { 
  
            if (source_bin[i] == 13) 
  
                if (source_bin[i + 1] == 10) 
  
                    break; 
  
        } 
  
        Byte[] MyHeader = new Byte[i]; 
  
        for (loop = 0; loop < i; loop++) 
  
            MyHeader[loop] = source_bin[loop]; 
  

        for (i += 2; i < Request.TotalBytes; i++) 
  
        { 
  
            if (source_bin[i] == 13) 
  
                if (source_bin[i + 1] == 10) 
  
                    if (source_bin[i + 2] == 13) 
  
                        if (source_bin[i + 3] == 10) 
  
                            break; 
  
        } 
  

        file_begin = i + 4; 
  
        for (i = file_begin; i < Request.TotalBytes; i++) 
  
        { 
  
            for (loop = 0; loop < MyHeader.Length; loop++) 
  
                if (source_bin[i + loop] != MyHeader[loop]) 
  
                    break; 
  
            if (loop >= MyHeader.Length) 
  
            { 
  
                break; 
  
            } 
  
        } 
  

        Byte[] result = new Byte[i - file_begin]; 
  
        //这是个不得已的办法,用循环肯定会慢,但我不会其他方法 
  
        //希望高手完善 
  
        for (loop = file_begin; loop < i - file_begin; loop++) 
  
            result[loop - file_begin] = source_bin[loop]; 
  

        //--------------------------------------------------------------------------------------- 
  
        //以上代码将word文档从二进制流中提取出来,存储在result[]中,方法虽笨,肯定好使 
  
        //本人不懂VB,更不懂Java, 
  
        //上面代码是我楞从梁无惧用VB写的无惧上传类V2.0里挑选有用的部分翻译成C#的,泪ing…… 
  
        //看看人家梁兄,多无私,给同行提供这么好的东东 
  
        //我在网上找了N + 1天,就TM没找到C#版的,再泪ing…… 
  
        //现在提供给大家,希望我是最后一个为此郁闷的人 
  


        SqlConnection myConnection = new SqlConnection("Data Source=/"localhost/";Initial Catalog=/"demo/";Persist Security Info=True;User ID=demo;Password=demo");//数据库的相关设置自己改吧 
  
        SqlCommand mycommand = myConnection.CreateCommand(); 
  
        mycommand.CommandText = "UPDATE Table_word SET filedata = @myfiledata , b_finished = 1 WHERE (ID = @myID)"; 
  
        mycommand.Parameters.Add("@myfiledata", SqlDbType.VarBinary, 0, "filedata"); 
  
        mycommand.Parameters.Add("@myID", SqlDbType.Int, 4, "ID"); 
  

        SqlDataAdapter mydpt; 
  
        DataSet myds = new DataSet(); 
  

        mydpt = new SqlDataAdapter("SELECT ID ,filedata , b_finished FROM Table_copy  WHERE (ID = " + Request["id"] + ")", myConnection); 
  
        myConnection.Open(); 
  

        mydpt.UpdateCommand = mycommand; 
  
        mydpt.Fill(myds); 
  
        DataTable mydt = myds.Tables[0]; 
  
        DataRow myrow; 
  
        myrow = mydt.Rows[0]; 
  
        myrow.BeginEdit(); 
  
        myrow["filedata"] = result; 
  
        myrow.EndEdit(); 
  

        mydpt.Update(myds); 
  
        myConnection.Close();/**/ 
  
          }




        至此,只要你指定的ID没问题,就应该可以保存Word文档了。