图像显示,保存在SQL Server中

时间:2015-05-07 09:46:48

标签: c# asp.net sql-server

我正在尝试显示我保存在SQL Server数据库中的图像。

这是我正在使用的代码。

import math

def linch_dict_divider(raw_dict, num):
    list_result = []
    len_raw_dict = len(raw_dict)
    if len_raw_dict > num:
        base_num = len_raw_dict / num
        addr_num = len_raw_dict % num
        for i in range(num):
            this_dict = dict()
            keys = list()
            if addr_num > 0:
                keys = raw_dict.keys()[:base_num + 1]
                addr_num -= 1
            else:
                keys = raw_dict.keys()[:base_num]
            for key in keys:
                this_dict[key] = raw_dict[key]
                del raw_dict[key]
            list_result.append(this_dict)

    else:
        for d in raw_dict:
            this_dict = dict()
            this_dict[d] = raw_dict[d]
            list_result.append(this_dict)

    return list_result

myDict = {'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4, 'key5': 5}
print myDict
myList = linch_dict_divider(myDict, 2)
print myList

问题是,函数public partial class SavedFileDisplay : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strQuery = "select Name, ContentType, Data from tblFiles where id = @id"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["ImageID"]); DataTable dt = GetData(cmd); if (dt != null) { Byte[] bytes = (Byte[])dt.Rows[0]["Data"]; Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = dt.Rows[0]["ContentType"].ToString(); Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString()); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } } private Boolean GetData(SqlCommand cmd) { String strConnString = System.Configuration.ConfigurationManager .ConnectionStrings["conString"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); cmd.CommandType = CommandType.Text; cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); return true; } catch (Exception ex) { Response.Write(ex.Message); return false; } finally { con.Close(); con.Dispose(); } } } 将数据保存在数据表中,但函数的类型为GetData。请帮我写正确的代码。

2 个答案:

答案 0 :(得分:1)

您需要一个返回DataTable而不是bool

的方法
private DataTable GetData(SqlCommand cmd)
{
    string myConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection myConn = new SqlConnection(myConnectionString))
    {
        cmd.Connection = myConn;
        using (SqlDataAdapter myDataAdapter = new SqlDataAdapter(cmd))
        {

            DataTable dtResult = new DataTable();
            myDataAdapter.Fill(dtResult);
            return dtResult;
        }
    }
}

答案 1 :(得分:0)

您正在为一个DataTable("[{"idFonction":"1","nomFonction":"Manager","nomEntite":"DIRECTION"},{"idFonction":"2","nomFonction":"Développeur","nomEntite":"SERVICE CONTROLE"},{"idFonction":"3","nomFonction":"Stagiaire","nomEntite":"SERVICE COMPTABILITE"},{"idFonction":"4","nomFonction":"rjeizjrze","nomEntite":"DIRECTION"}]" )分配一个bool值,因为DataTable dt = GetData(cmd);返回bool,这是无效的。您需要将GetData的返回值更改为DataTable并更改GetData的逻辑以从数据库中检索数据并将其推送到数据表中:

GetData()