图片以varchar(max)数据格式存储于数据库,每张照片对应一个检测流水号,需要将其取出展示到页面。
一、BLL
publict byte[] 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); } }
时间: 2024-10-12 10:46:43