2013-02-05 128 views
0

我得到一個「標準表達式中的數據類型不匹配」。在一個數據庫上運行此代碼時出現錯誤,但在另一個數據庫上工作良好。 當試圖將相關表複製到其他數據庫並從他運行時,程序再次失敗!「標準表達式中的數據類型不匹配」。錯誤

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

回答

1

隨着OLEDB(如您使用的)標準不匹配通常意味着,因爲數據庫需要另外一種類型的數據,您要放入數據庫中的數據不能被接受的。 (即數據庫需要一個整數,並且你傳遞一個double)。它可能有點煩人,但是你需要仔細檢查數據庫中所有列的數據類型,以確保你發送了它可以處理的東西。

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

相關問題