“标准表达中的数据类型不匹配”。错误 [英] "Data type mismatch in criteria expression." Error

查看:342
本文介绍了“标准表达中的数据类型不匹配”。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个数据类型不匹配的条件表达式。在一个数据库上运行此代码时出错,但在其他数据库上工作得很好。
当尝试将相关表复制到其他数据库并从其运行时,程序再次失败!

  using System ; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;

命名空间Project
{
public partial class登录:Form
{
public Login()
{
InitializeComponent() ;
}

private void Login_Load(object sender,EventArgs e)
{

}

private void label1_Click(object发件人,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);
}
}
}

类DALI 'm using。

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data;
使用System.Data.OleDb;

///< summary>
/// DAL
的总结说明///< / summary>
public class DAL
{
private string dbPath;
private OleDbConnection conn;
私人OleDbCommand命令;
私人OleDbDataAdapter适配器;
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的数据库列实际上不是字符串?这似乎很奇怪,但它是已知的发生。一些数据库设计人员将命名一个字段,即使它包含一个整数(以匹配自动编号)您必须查看数据库的预期数据类型以确定


I get a "Data type mismatch in criteria expression." error when running this code on one database but it works perfectly fine on another database. When trying to copy the relevent table to other database and run it from him the program fails again!

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");
    }
}
}

The class "DAL" I'm using.

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();
}
}

解决方案

With OleDb (as you are using) Criteria Mismatch Usually means that the data you are trying to put into the database can't be accepted because the database is expecting a different type of data. (i.e. the database expects an integer and you pass a double to it.) It can be a bit annoying but you'll need to double check all the datatypes of the columns in the database to make sure you are sending something it can handle.

In this case... maybe the the database column of SecretaryUsername is not actually a string? That seems odd but it's been known to happen. Some DB designers will name a field like that even though it contains an integer (to match up with an autonumber) You'll have to look at the Databases's expected datatype to know for sure

这篇关于“标准表达中的数据类型不匹配”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆