2023年6月21日发(作者:)
c#SqlServer字段保存⼆进制⽂件ver数据库中保存⼆进制的字段[varbinary](max),建⼀个表CREATE TABLE [dbo].[FileDemoModel]( [ID] [int] NULL, [demoFileName] [varchar](100) NULL, [demoFile] [varbinary](max) NULL)
2.创建⼀个测试Demo3.创建表对应的model public class FileDemoModel { public Nullable ID { get; set; } public string demoFileName{ get; set; } public byte[] demoFile { get; set; }
}per /// /// 执⾏增删改操作 /// /// /// public static int Update(string sql) { try { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); (); int result = eNonQuery(); (); return result; } catch (Exception ex) { return -1; } } /// /// 返回⼀个数据集的查询 /// /// /// public static SqlDataReader GetReader(string sql) { try { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); (); return eReader(onnection); } catch (Exception ex) { //(ng()); throw; } }#region 执⾏带参数的SQL语句 public static int Update(string sql, SqlParameter[] parameters) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(parameters);//添加参数 int result = eNonQuery(); return result; } catch (Exception ex) { //写⼊⽇志... throw ex; } finally { (); } } #endregion5.数据访问 public class FileDemoServer { #region 添加实体 public int Insert(FileDemoModel model) { try { StringBuilder sbSqlParent = new StringBuilder(); ("Insert into FileDemoModel(ID,demoFileName,demoFile)VALUES(@ID,@demoFileName,@demoFile)"); //执⾏并返回 SqlParameter[] param = new SqlParameter[] { new SqlParameter("@ID",), new SqlParameter("@demoFileName",leName), new SqlParameter("@demoFile",le), }; return (ng(), param); } catch (Exception) { throw; } } #endregion #region 获取实体 public FileDemoModel GetModel(FileDemoModel model) { try { StringBuilder sbSqlParent = new StringBuilder(); ($"select * from FileDemoModel where ID ={} ");
SqlDataReader objReader = der(ng()); FileDemoModel p = new FileDemoModel(); while (()) { = 32(objReader["ID"]); leName = objReader["demoFileName"].ToString(); le = objReader["demoFile"] as byte[]; } (); return p; } catch (Exception ex ) { throw; }
} #endregion }6.最上层的调⽤ [TestClass] public class FileDemo { #region 上传 [TestMethod] public void UpLoadFile() { String filePath = @"C:UsersLiu_NDesktopLN_"; byte[] File2Bytes = 2Bytes(filePath); FileDemoModel model = new FileDemoModel { ID = 1, demoFileName = "", demoFile = File2Bytes }; FileDemoServer fileDemoServer = new FileDemoServer(); int res = (model); } #endregion #region 下载 [TestMethod] public void DownloadFile() { String filePath = @"C:UsersLiu_NDesktopLN_"; FileDemoServer fileDemoServer = new FileDemoServer(); //=>获取实体 FileDemoModel model = el(new FileDemoModel { ID=1}); //=>转化 2File(le, filePath); } #endregion }7.⼯具类:⽂件与⼆进制流间的转换 /// /// ⼯具类:⽂件与⼆进制流间的转换 /// public class FileBinaryConvertHelper { /// /// 将⽂件转换为byte数组 /// /// ⽂件地址 /// 转换后的byte数组 public static byte[] File2Bytes(string path) { if (!(path)) { return new byte[0]; } FileInfo fi = new FileInfo(path); byte[] buff = new byte[]; FileStream fs = ad(); (buff, 0, 32()); (); return buff; } /// /// 将byte数组转换为⽂件并保存到指定地址 /// /// byte数组 /// 保存地址 public static void Bytes2File(byte[] buff, string savepath) { if ((savepath)) { (savepath); } FileStream fs = new FileStream(savepath, New); BinaryWriter bw = new BinaryWriter(fs); (buff, 0, ); (); (); } }代码如下:vjxd
2023年6月21日发(作者:)
c#SqlServer字段保存⼆进制⽂件ver数据库中保存⼆进制的字段[varbinary](max),建⼀个表CREATE TABLE [dbo].[FileDemoModel]( [ID] [int] NULL, [demoFileName] [varchar](100) NULL, [demoFile] [varbinary](max) NULL)
2.创建⼀个测试Demo3.创建表对应的model public class FileDemoModel { public Nullable ID { get; set; } public string demoFileName{ get; set; } public byte[] demoFile { get; set; }
}per /// /// 执⾏增删改操作 /// /// /// public static int Update(string sql) { try { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); (); int result = eNonQuery(); (); return result; } catch (Exception ex) { return -1; } } /// /// 返回⼀个数据集的查询 /// /// /// public static SqlDataReader GetReader(string sql) { try { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); (); return eReader(onnection); } catch (Exception ex) { //(ng()); throw; } }#region 执⾏带参数的SQL语句 public static int Update(string sql, SqlParameter[] parameters) { SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(sql, conn); try { (); ge(parameters);//添加参数 int result = eNonQuery(); return result; } catch (Exception ex) { //写⼊⽇志... throw ex; } finally { (); } } #endregion5.数据访问 public class FileDemoServer { #region 添加实体 public int Insert(FileDemoModel model) { try { StringBuilder sbSqlParent = new StringBuilder(); ("Insert into FileDemoModel(ID,demoFileName,demoFile)VALUES(@ID,@demoFileName,@demoFile)"); //执⾏并返回 SqlParameter[] param = new SqlParameter[] { new SqlParameter("@ID",), new SqlParameter("@demoFileName",leName), new SqlParameter("@demoFile",le), }; return (ng(), param); } catch (Exception) { throw; } } #endregion #region 获取实体 public FileDemoModel GetModel(FileDemoModel model) { try { StringBuilder sbSqlParent = new StringBuilder(); ($"select * from FileDemoModel where ID ={} ");
SqlDataReader objReader = der(ng()); FileDemoModel p = new FileDemoModel(); while (()) { = 32(objReader["ID"]); leName = objReader["demoFileName"].ToString(); le = objReader["demoFile"] as byte[]; } (); return p; } catch (Exception ex ) { throw; }
} #endregion }6.最上层的调⽤ [TestClass] public class FileDemo { #region 上传 [TestMethod] public void UpLoadFile() { String filePath = @"C:UsersLiu_NDesktopLN_"; byte[] File2Bytes = 2Bytes(filePath); FileDemoModel model = new FileDemoModel { ID = 1, demoFileName = "", demoFile = File2Bytes }; FileDemoServer fileDemoServer = new FileDemoServer(); int res = (model); } #endregion #region 下载 [TestMethod] public void DownloadFile() { String filePath = @"C:UsersLiu_NDesktopLN_"; FileDemoServer fileDemoServer = new FileDemoServer(); //=>获取实体 FileDemoModel model = el(new FileDemoModel { ID=1}); //=>转化 2File(le, filePath); } #endregion }7.⼯具类:⽂件与⼆进制流间的转换 /// /// ⼯具类:⽂件与⼆进制流间的转换 /// public class FileBinaryConvertHelper { /// /// 将⽂件转换为byte数组 /// /// ⽂件地址 /// 转换后的byte数组 public static byte[] File2Bytes(string path) { if (!(path)) { return new byte[0]; } FileInfo fi = new FileInfo(path); byte[] buff = new byte[]; FileStream fs = ad(); (buff, 0, 32()); (); return buff; } /// /// 将byte数组转换为⽂件并保存到指定地址 /// /// byte数组 /// 保存地址 public static void Bytes2File(byte[] buff, string savepath) { if ((savepath)) { (savepath); } FileStream fs = new FileStream(savepath, New); BinaryWriter bw = new BinaryWriter(fs); (buff, 0, ); (); (); } }代码如下:vjxd
发布评论