GUID, the abbreviation of "Global Unique Identifier", is a unique reference number used as an identifier in computer software.

GUIDs are usually stored as 128-bit values, and are commonly displayed as 32 hexadecimal digits with groups separated by hyphens, such as:

  • 21EC2020-3AEA-4069-A2DD-08002B30309D

My new task is to transfer the signature information, which directly saved as bytes[] data in DB. What I'm gonna to do is to give every signature a unique name and save the file name in DB. GUID is perfect for my requirement.

Step1: Get the img bytes from DB

  1. byte[] decodedImage = (byte[])reader["Signature"];

Step2: Transfer bytes to img

  1. using (MemoryStream ms = new MemoryStream(decodedImage))
  2. {
  3. Image img = Image.FromStream(ms);
  4. }

Step3: Give the img a unique name

  1. using (MemoryStream ms = new MemoryStream(decodedImage))
  2. {
  3. Image img = Image.FromStream(ms);
  4. var imgFileName = Guid.NewGuid().ToString() + ".png";
  5. }

Step4: Save the file in system media folder and save the file name in DB

  1. using (MemoryStream ms = new MemoryStream(decodedImage))
  2. {
  3. Image img = Image.FromStream(ms);
  4. var imgFileName = Guid.NewGuid().ToString() + ".png";
  5. fileName = imgFileName;
  6. idArr = reader["Id"].ToString();
  7. string path = Path.GetFullPath(SIGNATURE_PATH);
  8. img.Save(path + imgFileName, System.Drawing.Imaging.ImageFormat.Png);
    }
  1. string sql = "UPDATE [Kiwi-UAT].[dbo].[StaffClockSignature]" +
  2. "SET SignImageFile='" + fileName +
  3. "' WHERE Id=" + idArr;
  4. using(SqlCommand cmd = new SqlCommand(sql, conn))
  5. {
  6. try
  7. {
  8. cmd.ExecuteNonQuery();
  9. }
  10. catch (System.Exception e)
  11. {
  12. Console.WriteLine(e.Message);
  13. }
  14. }
  • Tips: Using these package at the begining of your file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7. using System.IO;
  8. using System.Drawing;

Now I'm got the png file in my folder and the fileName record in DB table ^_^:

  

Using GUID to generate the unique file name in C#的更多相关文章

  1. Creating a Unique File Name

    If you are working with files or file attachments in PeopleCode, you will typically want to create a ...

  2. Unity3d导入工程出现错误“Creating unique file”的解决方法

    Unity3d导入工程出现错误“Creating unique file:creating file Temp/tempFile failed.Please ensure there is enoug ...

  3. Shell: how to list all db links in oracle DB to generate a flat file (生成dblink列表文件)

    如果数据库里有上百个DATABASE LINK, 而且同时要管理几十套这样的数据库,在日后改数据库用户密码时就要格外注意是否有DB LINK在使用,否则只改了LOCAL DB 的用户密码,没有级连修改 ...

  4. executing in nfs will not generate core dump file

    最近遇到了一个奇怪的问题. linux系统的pc搭建nfs server,开发板作为nfs client,开发板中全程root权限操作,执行的程序放到 nfs server 中 exports 出的目 ...

  5. Fedora 24中的日志管理

    Introduction Log files are files that contain messages about the system, including the kernel, servi ...

  6. mybatis反向生成sql,基本的增删改查

    用到的几个文件 MyBatisGeneratorProxy.java package com.timestech.wsgk.test.tools; import static org.mybatis. ...

  7. GUID概念

     GUID概念 GUID: 即Globally Unique Identifier(全球唯一标识符) 也称作 UUID(Universally Unique IDentifier) . GUID是 ...

  8. UUID GUID

    http://baike.baidu.com/link?url=xkck9gR5bzOx0oBKP1qNJwGGq3IO56V4i8cg9zTSpSDMVBMA0F7jr0AdkQTGyk7F0FGj ...

  9. codeforces 710E E. Generate a String(dp)

    题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...

随机推荐

  1. 【HDOJ】1908 Double Queue

    双端队列+二分. #include <cstdio> #define MAXN 1000005 typedef struct { int id; int p; } node_st; nod ...

  2. [LeetCode#260]Single Number III

    Problem: Given an array of numbers nums, in which exactly two elements appear only once and all the ...

  3. 数据结构(平衡树,树分治,暴力重构):WC 2014 紫荆花之恋

    [题目描述] 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从紫荆树上长了出来. 仔细看看的话,这棵大树实际上是一个带权 ...

  4. HDU-1846 Brave Game

    http://acm.hdu.edu.cn/showproblem.php?pid=1846 (一)巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最 ...

  5. tomcat配置多实例

    CATALINA_HOME环境变量不必配置,因为在startup.sh脚本里会指定CATALINA_HOME的位置.     配置tomcat多实例 首先是理解下原理:CATALINA_HOME指向安 ...

  6. BZOJ4195 [Noi2015]程序自动分析(离散化+并查集)

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 689  Solved: 296 [Submit][Sta ...

  7. Oracle设计规范!

    Oracle设计规范! 一哥们整理的Oracle的设计规范,相当的不错,贴这以备后续之需! 目录 1.数据库模型设计方法规范 1.1.数据建模原则性规范 1.2.实体型之间关系认定规范 1.3.范式化 ...

  8. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

  9. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  10. C#执行带参数的Oracle存储过程

    public void UpdateByRowGuid(string RowGuid) { //OracleConnection conn = new OracleConnection("d ...