Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Parameter.Dbtype(存储过程)将映像插入SQL Server数据库_C#_.net_Sql Server_Winforms - Fatal编程技术网

C# 使用Parameter.Dbtype(存储过程)将映像插入SQL Server数据库

C# 使用Parameter.Dbtype(存储过程)将映像插入SQL Server数据库,c#,.net,sql-server,winforms,C#,.net,Sql Server,Winforms,我正在尝试将IMAGE插入SQL Server数据库(通过存储过程),为此,我使用了一个具有参数详细信息的类,并在代码中隐藏了我试图映射值的按钮单击事件 param = comm.CreateParameter(); param.ParameterName = "Imagedata"; param.Value = Imagedata; param.DbType = DbType.String; comm.Parameters.Add(param); 我尝试使用二进制而不是字符串,但出现了一个错

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

param = comm.CreateParameter();
param.ParameterName = "Imagedata";
param.Value = Imagedata;
param.DbType = DbType.String;
comm.Parameters.Add(param);
我尝试使用二进制而不是字符串,但出现了一个错误,说明无法将字符串转换为字节[]。我在SQL中使用的数据类型是Varbinary(MAX)

存储过程

[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); 
数据库类型应该是什么?如何成功地解决和插入映像

  • 您必须使用
    SqlDbType
    而不是
    DbType
  • 要将文件/图像转换为
    字节数组
    ,可以使用
    File.ReadAllBytes()
    而不是
    FileStream
    (请参阅)
  • InsertUser
    方法的最后一个参数中,传递
    Byte Array
    而不是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);
    
  • 希望这有帮助

  • 您必须使用
    SqlDbType
    而不是
    DbType
  • 要将文件/图像转换为
    字节数组
    ,可以使用
    File.ReadAllBytes()
    而不是
    FileStream
    (请参阅)
  • InsertUser
    方法的最后一个参数中,传递
    Byte Array
    而不是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);
    

  • 希望这能有所帮助。

    我错了吗?还是调用用户的最后一个参数。InsertUser是ImageData数组的长度,而不是数组的引用?什么是
    用户。InsertUser
    ?特别是,该方法的最后一个参数是什么?可能的话,请在这里发布您的存储过程代码。您需要在param.DbType中使用SqlDbType.Image,而不是字符串。字符串和二进制数据(图像数据)不同。@Steve请详细告诉我。。我正在获取字节长度,如果它引用数组,那么应该是什么。是我错了还是调用用户的最后一个参数。InsertUser是ImageData数组的长度,而不是数组的引用?什么是
    用户。InsertUser
    ?特别是,该方法的最后一个参数是什么?可能的话,请在这里发布您的存储过程代码。您需要在param.DbType中使用SqlDbType.Image,而不是字符串。字符串和二进制数据(图像数据)不同。@Steve请详细告诉我。。我正在获取字节长度,如果它引用的是数组,那么应该是.File.ReadAllBytes。。感谢您帮助我解决了无法投票但实际使用完整:(因为声誉文件。ReadAllBytes..感谢您帮助我解决了无法投票但实际使用完整:(因为声誉)