• 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
  • 相关阅读:
    开源控件PullToRefreshGridView的使用(三)
    JS中的运算符 以及变量和输入输出
    HTML中表格
    JS中 事件冒泡与事件捕获
    JS中的循环结构
    UML类图详解
    一步一步生成图片水印
    快速生成缩略图
    ==和Equal()的区别
    屌丝程序人生(下)
  • 原文地址:https://www.cnblogs.com/hellowzl/p/10495546.html
Copyright © 2020-2023  润新知