我得到错误
OleDbDataReader上的OleDbException未处理 - 标准中的数据类型不匹配 表达
re = 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!");
}
}
}
}
答案 0 :(得分: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);
您需要指定TextBox的Text属性,而不是TextBox控件本身。
答案 1 :(得分:0)
我对连接到Access不太了解,但这里有一些您可能想要检查的典型问题: