2017-12-09 23 views
-2
Encoding.ASCII.GetString(o.Username) == textBox1.Text 

これは、以前私がこの問題を解決するために与えたコードでした。私は自分のコードにどこに置くべきかわからない。助けてください!!演算子 '=='は、 'byte []'型と 'string'型のオペランドには適用できません。編集済み

これはエラーとセクションです:

private void button1_Click(object sender, EventArgs e) 
{ 
    if (string.IsNullOrEmpty(textBox1.Text)) 
    { 
     MessageBox.Show("Please Enter your username.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     textBox1.Focus(); 
     return; 
    } 
    try 
    { 
     using (DataEntities test = new DataEntities()) 
     { 
      var query = from o in test.Users 
         where o.Username == textBox1.Text && o.Password == textBox2.Text 
         select o; 
      if(query.SingleOrDefault() != null) 
      { 
       MessageBox.Show("You have been successfully logged in.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        //Add your code process login here 
      } 
      else 
      { 
       MessageBox.Show("Your username or password is incorrect.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
} 
+0

[投稿した質問](https://stackoverflow.com/questions/47707289/operator-cannot-be-applied-to-operands-of-type-byte-and-string)を再投稿しないでください。 )をクリックして編集します。投稿の[編集](https://stackoverflow.com/posts/47707289/edit)リンクをクリックするだけです。この質問は削除する必要があり、もう1つは問題があれば修正するため編集されています。 –

+0

'o.Password == textBox2.Text' - >あなたは**理想的には塩でユーザーパスワードを**ハッシングする必要があります。単に等価チェックをすることはできません。データベースをハックすると、ユーザーの詳細と平文のパスワードがすべて取得されます。 – john

答えて

-2

あなたは右、コンパイルしようとすると、このエラーを取得していますか?

あなたの問題は実際にはあなたのクエリの周りに引用符がないことだと思います。

この:

var query = from o in test.Users 
where o.Username == textBox1.Text && o.Password == textBox2.text 
select o; 

は次のようになります。

var query = "from o in test.Users" + 
"where o.Username == textBox1.Text && o.Password == textBox2.text" + 
"select o"; 

クエリ文字列があるので、あなたは右の引用符で=の全てを囲む必要があります。これはコンパイラに、それ以上のコードではなく文字列リテラルであることを伝えます。

+0

これはLinqであり、SQLクエリではありません。たとえSQLクエリであっても、有効なクエリではありません。 Linqのクエリの構文[here](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/basic-linq-query-operations)について知ることができます。 – john

関連する問題