无法隐式转换类型'字符串'到LINQ to SQL中的System.Data.Linq.Binary

时间:2012-02-24 19:16:34

标签: c# database linq-to-sql hash

我已经对密码进行了哈希,然后我将其插入到数据库中,但问题在于此 每次我尝试这样做时都会发生这种情况

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

在我的数据库表中,accnt_pass是二进制(50),这是我的代码

//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; 

我正在使用Encrypto进行散列,

1 个答案:

答案 0 :(得分:6)

如果sql列是二进制类型,则需要将字符串encryptedPassword转换为字节数组。 所以而不是行

newUser.accnt_Pass = encryptedPassword;

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