简体   繁体   中英

Cannot implicitly convert type 'string' to 'System.Data.Linq.Binary in LINQ to SQL

I've hashed the password and then I'll insert it to the database but the problem here is that each time I try to do this query this occurs

Cannot implicitly convert type 'string' to 'System.Data.Linq.Binary'

in my table in the database accnt_pass is Binary(50), here's my code

//Instantiate a new Hasher Object
var hasher = new Hasher();

hasher.SaltSize = 16;

//Encrypts The password
var encryptedPassword = hasher.Encrypt(txtPass.Text);

Account newUser = new Account();

newUser.accnt_User = txtUser.Text;
newUser.accnt_Position = txtPosition.Text;
newUser.accnt_Pass = encryptedPassword; 

and I am using Encrypto for hashing,

You need to convert the string encryptedPassword to a byte array if your sql column is of binary type. So instead of the line

newUser.accnt_Pass = encryptedPassword;

put

System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
newUser.accnt_Pass = new System.Data.Linq.Binary(encoding.GetBytes(encryptedPassword));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM