【问题标题】:Insert Image into SQL Server database using Parameter.Dbtype (stored procedure)使用 Parameter.Dbtype(存储过程)将图像插入 SQL Server 数据库
【发布时间】:2015-03-08 22:22:59
【问题描述】:

我正在尝试将 IMAGE 插入 SQL Server 数据库(通过存储过程),为此我使用了一个具有参数详细信息的类,并且在按钮单击事件的代码中我试图映射这些值。

param = comm.CreateParameter();
param.ParameterName = "Imagedata";
param.Value = Imagedata;
param.DbType = DbType.String;
comm.Parameters.Add(param);

我尝试使用二进制而不是字符串,我收到一条错误消息,指出无法将字符串转换为字节 []。我在 SQL 中使用的数据类型是 Varbinary(MAX)。

bool a = false;
String imagefilepath = @fileName;
FileStream imagefile = new FileStream(imagefilepath, FileMode.Open, FileAccess.Read);
Imagedata = new Byte[imagefile.Length];
imagefile.Read(Imagedata, 0, Imagedata.Length);
imagefile.Flush();
imagefile.Close();
a = Users.InsertUser(this.txt_userid.Text.Trim().ToUpper(),
                     this.txt_mobnum.Text.Trim().ToUpper(), 
                     this.txt_name.Text,
                     this.role_cmbox.Text.Trim().ToUpper(), 
                     this.box_branch.Text.Trim().ToUpper(), 
                     this.txt_designation.Text.Trim().ToUpper(), 
                     this.txt_repassword.Text.Trim().ToUpper(), 
                     this.Imagedata.Length.ToString());

存储过程

[dbo].[InsertUser](@UserID varchar(15),@Password varchar(20),@UserName varchar(20),  
@Role varchar(15),@Branch varchar(15),@Designation varchar(15),@Mobilenumber varchar(15),@Imagedata varbinary(MAX))    
as  
INSERT INTO[LBank].dbo.[Login]
           ([UserID]
           ,[Password]  
           ,[UserName]  
           ,[Role]
           ,[Branch]
           ,[Designation]
           ,[Mobilenumber]
           ,[Imagedata]
           )   
VALUES(@UserID,@Password,@UserName,@Role,@Branch,@Designation,@Mobilenumber,@Imagedata); 

DbType应该是什么以及如何成功解决和插入图像?

【问题讨论】:

  • 我错了还是调用Users.InsertUser的最后一个参数是ImageData数组的长度而不是对数组的引用?
  • 什么是Users.InsertUser?特别是,该方法的最后一个参数是什么?
  • 可能的,请在此处发布您的存储过程代码。
  • param.DbType 中需要使用SqlDbType.Image 而不是String。字符串和二进制数据(图像数据)是不同的。
  • @Steve 请详细告诉我.. 我正在接受字节长度,如果它对数组的引用应该是什么。

标签: c# .net sql-server winforms


【解决方案1】:
  1. 您必须使用SqlDbType 而不是DbType
  2. 对于Byte Array 的文件/图像,您可以使用File.ReadAllBytes() 而不是FileStream(请参阅this
  3. InsertUser 方法的最后一个参数中传递 Byte Array 而不是字节数组的 Length

        bool a = false;
        String imagefilepath = @fileName;
        ImageData = File.ReadAllBytes(imagefilepath);
    
        a = Users.InsertUser(this.txt_userid.Text.Trim().ToUpper(),
                       this.txt_mobnum.Text.Trim().ToUpper(),
                       this.txt_name.Text,
                       this.role_cmbox.Text.Trim().ToUpper(),
                       this.box_branch.Text.Trim().ToUpper(),
                       this.txt_designation.Text.Trim().ToUpper(),
                       this.txt_repassword.Text.Trim().ToUpper(),
                       this.Imagedata); //Here you need to pass the byte array not length
    
    
        var param = comm.CreateParameter();
        param.ParameterName = "Imagedata";
        param.Value = Imagedata;
        param.SqlDbType = SqlDbType.Image;
        comm.Parameters.Add(param);
    

希望这会有所帮助。

【讨论】:

  • File.ReadAllBytes .. 感谢您帮助我为我工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
相关资源
最近更新 更多