簡體   English   中英

“條件表達式中的數據類型不匹配。”錯誤

[英]“Data type mismatch in criteria expression.” Error

我收到“條件表達式中的數據類型不匹配”。 在一個數據庫上運行此代碼時出現錯誤,但在另一個數據庫上運行良好。 嘗試將relevent表復制到其他數據庫並從他那里運行該程序時,程序再次失敗!

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>
/// Summary description for DAL
/// </summary>
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(正在使用)時,條件不匹配通常意味着您試圖放入數據庫中的數據無法接受,因為數據庫需要其他類型的數據。 (即數據庫需要一個整數,然后將其傳遞給它。)這可能有點煩人,但是您需要仔細檢查數據庫中列的所有數據類型,以確保發送的是可以處理的數據。

在這種情況下...也許SecretaryUsername的數據庫列實際上不是字符串? 這似乎很奇怪,但已知會發生。 即使它包含一個整數(以與一個自動編號匹配),某些數據庫設計人員也會使用這樣的字段命名,您必須查看數據庫的預期數據類型才能確定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM