我无法从C#.Net中的访问数据库中获取单个记录

时间:2015-04-09 19:07:07

标签: c# ms-access datagridview

我正在进行图书馆管理系统项目。我想通过选择查询使用借用者号码获取有关学生的所有数据。但我的代码不起作用它在行号40 dAdapter.Fill(dataTable);中的条件表达式中出现类型不匹配的例外情况。请帮我。这是我的代码。 谢谢你:)。

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;
using System.Data.OleDb;

namespace LibraryManagment
{
public partial class ViewStudent : Form
{
    private OleDbConnection bookConn;
    private OleDbCommand oleDbCmd = new OleDbCommand();
    //parameter from mdsaputra.udl
    private String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\mini\database\library.mdb;Persist Security Info=False";



    public ViewStudent()
    {
        bookConn = new OleDbConnection(connParam);
        InitializeComponent();
    }

    private void btnView_Click(object sender, EventArgs e)
    {
        dataGridView1.DataSource = null;
        dataGridView1.Rows.Clear();
        dataGridView1.Refresh();

        OleDbDataAdapter dAdapter = new OleDbDataAdapter("select * from Student where BorrowerNo = '"+ this.txtBorrowerNo.Text +"';", connParam);
        OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);

        DataTable dataTable = new DataTable();
        DataSet ds = new DataSet();

        dAdapter.Fill(dataTable);

        for (int i = 0; i < dataTable.Rows.Count; i++)
        {
            dataGridView1.Rows.Add(dataTable.Rows[i][0], dataTable.Rows[i][1], dataTable.Rows[i][2], dataTable.Rows[i][3], dataTable.Rows[i][4], dataTable.Rows[i][5], dataTable.Rows[i][6], dataTable.Rows[i][7]);
        }
    }


        private void ViewStudent_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'libraryDataSet.Student' table. You can move, or remove it, as needed.
           // this.studentTableAdapter4.Fill(this.libraryDataSet.Student);

        }


}

}

1 个答案:

答案 0 :(得分:0)

根据您的评论中提到的错误,借款人号码的类型号码似乎导致了问题。

OleDbDataAdapter dAdapter = new OleDbDataAdapter("select * from Student where BorrowerNo = "+ this.txtBorrowerNo.Text +";", connParam);

请注意,我已删除了单引号。当数据类型为字符串时,可以使用单引号。