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
  • 相关阅读:
    分布式事务之可靠消息
    分布式事务之本地消息表
    分布式事务
    数据库之 事务
    WePY开发小程序(二):项目入口及注册页面、组件
    WePY开发小程序(一):入门
    vue学习笔记-事件监听
    vue学习笔记-列表渲染
    vue学习笔记-缩写
    vue学习笔记-常用指令
  • 原文地址:https://www.cnblogs.com/hellowzl/p/10495546.html
Copyright © 2011-2022 走看看