当前位置:Gxlcms > 数据库问题 > 从数据库取图片并展示到页面

从数据库取图片并展示到页面

时间:2021-07-01 10:21:17 帮助过:15人阅读

[] GetPhotoData(string jclsh) { byte[] photoData=new byte[4000]; try { string sqlStr="select fimage from imagetable where jclsh=\‘"+jclsh+"\‘"; DataTable dt=new YTH_CHECKPHOTO_DAL().GetDataTable(sqlStr); photoData=Convert.FromBase64String(dt.Rows[0]["fimage"].ToString().Replace(" ", "+"));//从Base64String格式转换为byte[]时,需要注意 } catch(Exception ex) { } return photoData; }

二、DAL
1、取得表信息

public override DataTable GetDataTable(string sqlStr)
{
DataTable dt = null;
try
{
//查询DataTable
dt = DbHelper.ExecuteGetDataTable(CommandType.Text, sqlStr);
}
catch (Exception ex)
{
Logger.Error(string.Format(@"执行查询异常,SQL:{0} 异常信息为:{1}", sqlStr, ex.Message));
}
finally
{
CloseConnet();
}
return dt;
}

2、DbHelper执行查询

public static DataTable ExecuteGetDataTable(CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
    //创建一个SqlCommand对象
    SqlCommand command = new SqlCommand();
    //创建一个DataTable对象
    DataTable table = new DataTable();
    //创建一个SqlConnection对象
    try
    {
        using (SqlConnection sqlConnection = new SqlConnection(connectionString))
        {
            //调用PrepareCommand方法,对SqlCommand对象设置参数
            PrepareCommand(command, sqlConnection, null, commandType, commandText, commandParameters);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(table);
            sqlConnection.Close();
        }
    }
    catch (Exception ex)
    {
        throw ex;
        Logger.Error(ex.Message);
        
    }
    return table;
}

3、DbHelper准备命令 

private static void PrepareCommand(SqlCommand sqlCommand, SqlConnection sqlConnection, SqlTransaction sqlTransaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
{
    try
    {
        if (sqlConnection.State != ConnectionState.Open)
        {
            sqlConnection.Open();
        }
        sqlCommand.Connection = sqlConnection;
        sqlCommand.CommandText = commandText;
        if (sqlTransaction != null)
        {
            sqlCommand.Transaction = sqlTransaction;

        }
        sqlCommand.CommandType = commandType;
        if (commandParameters != null)
        {
            sqlCommand.Parameters.AddRange(commandParameters);
        }
    }
    catch (Exception ex)
    {
        Logger.Error(ex.Message);
    }
}

 

从数据库取图片并展示到页面

标签:.text   ror   final   ace   orm   取出   exe   方法   stat   

人气教程排行