Dim strSQL As String = "select 文件内容 from  合同 where id =" & htid
        Dim cmd As New SqlCommand(strSQL, myConnection)
        If myConnection.State = ConnectionState.Open Then
        Else
            myConnection.Open()
        End If
        Try
            Dim wjData() As Byte = cmd.ExecuteScalar()
            myConnection.Close()
            Dim K As Long
            K = UBound(wjData)
            Dim fs As New FileStream _
             (wjmc, FileMode.Create, FileAccess.Write)
            fs.Write(wjData, 0, K)
            fs.Close()
            fs = Nothing
            Response.Redirect("temp\" & file_name)
        Catch ex As Exception
         '不能完成
            Exit Function
        End Try
这是我合同下载的代码,改改就行了.

解决方案 »

  1.   

    这是我曾经做的程序中的部分代码,是没有问题的。 string strSql = "select * from tpzl"; string strConnection = ConfigurationSettings.AppSettings["SQLConnectionString"];
    SqlConnection objConnection = new SqlConnection(strConnection);
    objConnection.Open();
    SqlCommand cmd = new SqlCommand(strSql, objConnection); cmd = new SqlCommand(strSql, objConnection);
    SqlDataReader objDataReader = cmd.ExecuteReader(); FileStream fs;
    BinaryWriter bw;
    int bufferSize = 100;
    byte[] outbyte = new byte[bufferSize];
    string url,jxtName,tpzlName;
    string jxtUrl; long retval;
    long startIndex = 0; StringCollection tpzlUrls = new StringCollection(); while (objDataReader.Read())   
    {
    if (objDataReader["tpzl"].ToString().Equals("jxt"))     {
    jxtName = Server.MapPath("Pics/Family/")+objDataReader["record_id"].ToString() + ".jpg";

    //////jxtUrl:输出图片
    jxtUrl = Request.ApplicationPath + "/Submit/Pics/Family/" + objDataReader["record_id"].ToString() + ".jpg";
    EXE.ExecuteSql("update tpzl set path='"+jxtUrl+"' where record_id="+objDataReader["record_id"]);
    fs = new FileStream(jxtName, FileMode.OpenOrCreate, FileAccess.Write);
    bw = new BinaryWriter(fs); startIndex = 0;
    //关键是下面的命令,其中的5表示的是取objDataReader中读到的第5列image类型数据为参数
    retval = objDataReader.GetBytes(5, startIndex, outbyte, 0, bufferSize);
    outbyte = (byte[])objDataReader["tpsj"];
    bw.Write(outbyte);
    bw.Flush();
    bw.Close();
    fs.Close(); }

      

  2.   

    感谢mh51,我用你的代码通过了,同样感谢 liuyi0605(刘怡) 的关注!