| |

VerySource

 Forgot password?
 Register
Search
Author: weiranli00

Solve the binary conversion problem

[Copy link]

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-4-5 08:00:02
| Show all posts
The conversion is still wrong, saying that it is impossible to convert the DBNull value in byte []
But when I change the database type to char, the value is correct. Why is it empty when I use the binary type
Reply

Use magic Report

0

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-4-7 11:45:01
| Show all posts
What type of UserName field is in your database?
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-4-7 22:30:02
| Show all posts
Is of type binary
But it can be changed to char type. If it is binary type, DBNull value cannot be converted to byte []
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-4-7 23:15:01
| Show all posts
But the database requires binary type
Reply

Use magic Report

0

Threads

15

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-4-10 09:30:01
| Show all posts
Oh
You can judge whether it is empty before force conversion
byte [] b =
(cmd.Parameters ["@ UserName"]. value == null)?
new byte [0]:
(byte []) cmd.Parameters ["@ UserName"]. value;
Reply

Use magic Report

2

Threads

22

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

 Author| Post time: 2020-4-21 21:45:02
| Show all posts
But wrong, why is it empty, it should be valuable, I just want the value
Reply

Use magic Report

0

Threads

18

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-4-26 20:15:01
| Show all posts
Is the stored procedure UsmgcfoxsExict no return value?
Reply

Use magic Report

1

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-4-26 22:30:02
| Show all posts
So why do I have a return value when I change the field to char type?
database:
CREATE procedure UsmgcfoxsExict
@Uid binary (30),
@UserName binary (30) output
as
select @ UserName = UserName from AlbumLogin where Uid = @ Uid
GO
program:
SqlCommand cmd = new SqlCommand ("UsmgcfoxsExict", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add (new SqlParameter ("@ Uid", SqlDbType.Binary, 30));
        cmd.Parameters ["@ Uid"]. Value = Encoding.UTF32.GetBytes (paraUid);

        cmd.Parameters.Add (new SqlParameter ("@ UserName", SqlDbType.Binary, 30));
        cmd.Parameters ["@ UserName"]. Direction = ParameterDirection.Output;

        conn.Open ();
        cmd.ExecuteNonQuery ();
        conn.Close ();

      return Encoding.UTF32.GetString (((byte []) cmd.Parameters ["@ UserName"]. Value));

error:
Unable to cast object of type "System.DBNull" to type "System.Byte []"
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list