2013-10-29 40 views
-1

即時得到錯誤OleDbException是未處理的 - 在條件表達式數據類型不匹配

OleDbException是未處理的 - 在標準數據類型不匹配的OleDbDataReader重新= cmd.ExecuteReader()線 表達

誰能幫我?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.SqlClient; 
using System.Data.OleDb; 

namespace posSystem 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnLogin_Click(object sender, EventArgs e) 
     { 



      OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=userName.accdb"); 
      OleDbCommand cmd = new OleDbCommand("Select * From userAndPAss Where ID = '" + txtBoxUserName + "' and Password = '" + txtBoxPassword + "';", conn); 
      conn.Open(); 
      OleDbDataReader re = cmd.ExecuteReader(); 

      if(re.Read() 
      { 

       MessageBox.Show("Login Successfull"); 



      } 
      else{ 
      MessageBox.Show("Login NOT Successfull, Try again!"); 
      } 
     } 
    } 
} 

回答

2

始終使用參數,以避免SQL注入:

OleDbCommand cmd = new OleDbCommand("Select * From userAndPAss Where ID = @id and Password = @password", conn); 
cmd.Parameters.AddWithValue("@id", txtBoxUserName.Text); 
cmd.Parameters.AddWithValue("@password", txtBoxPassword.Text); 

您需要指定文本框的Text屬性,而不是TextBox控件本身。

0

我不知道很多有關連接到訪問,但在這裏,你可能要檢查一些典型問題:你的表列的

  • 檢查數據類型 - 是「ID」真串?
  • 檢查,如果COLUMNNAMES被保留在訪問(尤其是「密碼」)
  • 訪問可能有一些問題,如果單元格值爲空(用「的Nz」)
關鍵字
相關問題