“条件表达式中的数据类型不匹配。”错误

我得到“标准表达式中的数据类型不匹配”。 在一个数据库上运行此代码时出错,但它在另一个数据库上运行正常。 当试图将相关表复制到其他数据库并从他运行时程序再次失败!

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Project { public partial class Login : Form { public Login() { InitializeComponent(); } private void Login_Load(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } private void LoginButton_Click(object sender, EventArgs e) { DAL conn = new DAL(@"|DataDirectory|\ProjectDB.accdb"); DataSet ds = conn.GetDataSet("Select * from Secretarys where SecretaryUsername = "+UserNameBox.Text.ToString()); if (ds.Tables[0].Rows[0][0].ToString().Equals(PassowrdBox.Text)) MessageBox.Show("asd","sdfa"); } } } 

我正在使用的类“DAL”。

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.OleDb; ///  /// Summary description for DAL ///  public class DAL { private string dbPath; private OleDbConnection conn; private OleDbCommand command; private OleDbDataAdapter adapter; private string stQuery; public DAL(string dbPath) { this.dbPath = dbPath; string ConnectionString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", this.dbPath); conn = new OleDbConnection(ConnectionString); command = new OleDbCommand(stQuery, conn); adapter = new OleDbDataAdapter(command); } public DataSet GetDataSet(string strSql) { DataSet ds = new DataSet(); command.CommandText = strSql; adapter.SelectCommand = command; adapter.Fill(ds); return ds; } public bool InsertRow(string sqlInsert) { int rowsEffected; command.CommandText = sqlInsert; conn.Open(); rowsEffected = command.ExecuteNonQuery(); conn.Close(); return (rowsEffected > 0); } public string GetData(string strSql)//שולפת נתונים מהטבלת המשתמשים שנמצאת באקסס { string st = ""; DataSet ds = new DataSet(); command.CommandText = strSql; conn.Open(); st = command.ExecuteScalar().ToString(); conn.Close(); return (st); } public void UpdateRow(string sqlInsert)//הוספת נתונים לטבלת החברים באקסס { command.CommandText = sqlInsert; conn.Open(); command.ExecuteNonQuery(); conn.Close(); } public void DeleteDataSet(DataSet ds) { OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); adapter.DeleteCommand = builder.GetDeleteCommand(); conn.Open(); adapter.Update(ds); conn.Close(); } } 

使用OleDb(正如您所使用的)条件不匹配通常意味着您尝试放入数据库的数据无法被接受,因为数据库需要不同类型的数据。 (即数据库需要一个整数,并向它传递一个double。)它可能有点烦人但你需要仔细检查数据库中列的所有数据类型,以确保你发送它可以处理的东西。

在这种情况下……也许SecretaryUsername的数据库列实际上不是字符串? 这看起来很奇怪但是众所周知。 一些DB设计者会将这样的字段命名为即使它包含一个整数(以匹配自动编号)你必须查看数据库的预期数据类型才能确定