简体   繁体   English

如何从Access数据库检索数据

[英]How to retrieve data from an Access database

When I run the following code I receive the error 当我运行以下代码时,我收到错误

No value given for one or more required parameters. 没有为一个或多个必需参数给出值。

Can anyone suggest why that might be? 谁能说出为什么呢?

namespace ATMPROJECT
{
    public partial class pin : Form
    {
        public pin()
        {
            InitializeComponent();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection con= new OleDbConnection ();
            OleDbCommand cmd;

            DataSet ds;
            OleDbDataAdapter da;
            OleDbDataReader dr = null;
            string i;
             con = new OleDbConnection ( "Provider = Microsoft.ACE.OLEDB.12.0; Data Source =C:\\Users\\RELIABLE TRADING CO\\Documents\\atm.accdb ");
            con.Open();
            if (textBox1.Text == "")
            {
                label1.Text  = ("PLZ ENTER YOUR PIN FIRST");

            }
            else
            {

                ds = new DataSet();

                i = @"SELECT * from atm WHERE pin= ?";
                cmd = new OleDbCommand(i,con);


                 cmd.Parameters.Add("@pin",OleDbType.VarChar).Value = textBox1 .Text;
                 da = new OleDbDataAdapter(cmd);


                 MessageBox.Show("done");
                 da.Fill(ds);
                 dataGridView1.DataSource = ds;

// generates error (No value given for one or more required parameters.)

That error indicates your atm table does not have a field called pin . 该错误表明您的atm表没有名为pin的字段。

Also, you probably have to specify the table in the DataSource: 另外,您可能必须在数据源中指定表:

dataGridView1.DataSource = ds.Tables[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM