zoukankan      html  css  js  c++  java
  • SqlParameter 操作 image 字段

    public static void AddEmployee(  
      string lastName,   
      string firstName,   
      string title,   
      DateTime hireDate,   
      int reportsTo,   
      string photoFilePath,   
      string connectionString)  
    {  
      byte[] photo = GetPhoto(photoFilePath);  
      
      using (SqlConnection connection = new SqlConnection(  
        connectionString))  
      
      SqlCommand command = new SqlCommand(  
        "INSERT INTO Employees (LastName, FirstName, " +  
        "Title, HireDate, ReportsTo, Photo) " +  
        "Values(@LastName, @FirstName, @Title, " +  
        "@HireDate, @ReportsTo, @Photo)", connection);   
      
      command.Parameters.Add("@LastName",    
         SqlDbType.NVarChar, 20).Value = lastName;  
      command.Parameters.Add("@FirstName",   
          SqlDbType.NVarChar, 10).Value = firstName;  
      command.Parameters.Add("@Title",       
          SqlDbType.NVarChar, 30).Value = title;  
      command.Parameters.Add("@HireDate",   
           SqlDbType.DateTime).Value = hireDate;  
      command.Parameters.Add("@ReportsTo",   
          SqlDbType.Int).Value = reportsTo;  
      
      command.Parameters.Add("@Photo",  
          SqlDbType.Image, photo.Length).Value = photo;  
      
      connection.Open();  
      command.ExecuteNonQuery();  
      }  
    }  
      
    public static byte[] GetPhoto(string filePath)  
    {  
      FileStream stream = new FileStream(  
          filePath, FileMode.Open, FileAccess.Read);  
      BinaryReader reader = new BinaryReader(stream);  
      
      byte[] photo = reader.ReadBytes((int)stream.Length);  
      
      reader.Close();  
      stream.Close();  
      
      return photo;  
    }  

    from: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/inserting-an-image-from-a-file
  • 相关阅读:
    Element UI表格组件技巧:如何简洁实现跨页勾选、跨页统计功能
    Spring Cloud Gateway转发Spring WebSocket
    mac OS 安装配置Nginx服务器
    异常处理
    模块和包调用方法、执行顺序
    模块:序列化
    模块:random
    模块:time
    模块:collections,Python中的扩展数据类型
    re模块的基本用法
  • 原文地址:https://www.cnblogs.com/hellowzl/p/10495546.html
Copyright © 2011-2022 走看看