Visual Studio从变量DataGridView查询数据库
本文关键字:查询 数据库 DataGridView 变量 Studio Visual | 更新日期: 2023-09-27 18:10:42
我一直在尝试查询访问数据库并在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];
}
}
}
您为不正确的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);