【问题标题】:Visual Studio Query Database from Variable DataGridViewVisual Studio 从变量 DataGridView 查询数据库
【发布时间】:2014-06-28 18:11:20
【问题描述】:

我一直在尝试查询访问数据库并在 DataDrigView 中显示结果。当我使用“SELECT * FROM CustomerPayments”时,网格会显示所有数据。但是,一旦我尝试将参数添加到查询中,我会收到一条错误消息

adapter.Fill(ds); //No value given for one more required parameters

但是,我已经在程序的另一部分成功地使用了参数查询,但似乎只有当我想在数据网格中显示时才会发生这种情况

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;
using System.Data.SqlClient; //Import database class


namespace WindowsFormsApplication1
{
    public partial class frmCustomerDetails : Form
    {
        //Database
        //Get connection from database
        String connectionString = staticConnectionString.connectionString;

        //Declare a connection object
        OleDbConnection con;

        //Declare a command object for the SQL
        OleDbCommand cmd;

        //Declare an accedd type reader to read SQL commands
        OleDbDataReader dr;

        String[] CustomerID = new String[500]; 
        String[] CustomerName = new String[500];
        String[] Street = new String[500];
        String[] Area = new String[500];
        String[] PostCode = new String[500];
        String[] TelNo = new String[500];
        String[] Email = new String[500];

        int RecordNo = 0;
        int LastRecord = 0;

        public frmCustomerDetails()
        {
            InitializeComponent();
        }

        private void frmCustomerDetails_Load(object sender, EventArgs e)
        {
            try
            {
                //create a connection object to the database via string location
                con = new OleDbConnection(connectionString);
                //Open the connection to the database
                con.Open();
                //Creat a new command object
                cmd = new OleDbCommand();
                //Set the SQL command text
                cmd.CommandText = "SELECT * FROM Customer WHERE CustomerID = @ID;";

                cmd.Parameters.AddWithValue("@ID", GlobalVar.SelectedCustomer);

                //Link the command to the connection so the correct DB is used
                cmd.Connection = con;
                //Run the command and store resulting table in the datareader
                dr = cmd.ExecuteReader();
            }
            catch (Exception err)
            {
                //Any database errors jump here and output error message
                MessageBox.Show("A database error has occurred: " + Environment.NewLine + err.Message);
            }
            finally
            {
                //Do this whatever happens
                //This is so the first record will be displayed
                //Connection and dr are left open until from closes so more records can be accessed
            }

            //dr.Read() gets the next in the results if possible
            while (dr.Read())
            {
                //Fill the text boxes with data
                CustomerID[RecordNo] = dr["CustomerID"].ToString();
                CustomerName[RecordNo] = dr["Names"].ToString();
                Street[RecordNo] = dr["Street"].ToString();
                Area[RecordNo] = dr["Area"].ToString();
                PostCode[RecordNo] = dr["PostCode"].ToString();
                TelNo[RecordNo] = dr["TelNo"].ToString();
                Email[RecordNo] = dr["Email"].ToString();

                RecordNo = RecordNo + 1;

            }
            LastRecord = RecordNo;
            RecordNo = 0;
            ReadDisplay();
        }


        private void btnMenu_Click(object sender, EventArgs e)
        {
            var SelectCustomer = new frmSelectCustomer();
            SelectCustomer.Show();

            this.Close();
        }



        private void btnWeeklyOrders_Click(object sender, EventArgs e)
        {

            String connectionString = staticConnectionString.connectionString;
            OleDbCommand command;
            OleDbDataAdapter adapter;

            con = new OleDbConnection(connectionString);
            command = con.CreateCommand();

            //create data set
            DataSet ds = new DataSet();

            //Clear the gride of data
            CustomerInfo.DataSource = null;

            //create a new dataset
            ds = new DataSet();

            //Open connection
            con.Open();

            //Run the query
            command.CommandText = "SELECT * FROM CustomerPayments WHERE CustomerID = @ID;";
            cmd.Parameters.AddWithValue("@ID", GlobalVar.SelectedCustomer);



            adapter = new OleDbDataAdapter(command);
            adapter.Fill(ds);

            //close connection
            con.Close();

            //Populate grid with data
            CustomerInfo.DataSource = ds.Tables[0];

        }


        private void ReadDisplay()
        {

            lblCustomerID.Text = CustomerID[RecordNo];
            lblNames.Text = CustomerName[RecordNo];
            lblStreet.Text = Street[RecordNo];
            lblArea.Text = Area[RecordNo];
            lblPostCode.Text = PostCode[RecordNo];
            lblTelNo.Text = TelNo[RecordNo];
            lblEmail.Text = Email[RecordNo];

        }

    }
}

【问题讨论】:

  • GlobalVar.SelectedCustomer 的确切值是多少?
  • 公共静态字符串 SelectedCustomer;它是一个从另一个表单存储客户 ID 的 var,当我在这个表单上成功使用它时,它可以在标签中显示数据时工作

标签: c# sql datagridview


【解决方案1】:

您为不正确的OleDbCommand 变量分配参数:

command.CommandText = "SELECT * FROM CustomerPayments WHERE CustomerID = @ID;";
cmd.Parameters.AddWithValue("@ID", GlobalVar.SelectedCustomer);

应该是:

command.CommandText = "SELECT * FROM CustomerPayments WHERE CustomerID = @ID;";
command.Parameters.AddWithValue("@ID", GlobalVar.SelectedCustomer);

【讨论】:

  • 啊,真是漫长的一天,非常感谢。它有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
相关资源
最近更新 更多