使用 select 语句在 datagridview table 中显示数据
Display data in datagridview table with select statement
我想知道如何使用从 Select 语句中找到的信息填充 DataGridView。这是 select 按钮的事件:
private void SelectCust(object sender, EventArgs e)
{
OleDbConnection conn = null;
conn = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0; " + "Data Source=" + "c:/inetpub/ftproot/ora103/App_Data/Fashion.accdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * from Customer where CustNum = @MyCust ", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
if (dt.Rows.Count > 0)
{
}
conn.Close();
}
我想使用 "if" 语句插入我从客户 table 那里收集的信息,并填充 DataGridView。但是,我在将 DataGridView 连接到我的 Microsoft Access 数据库(称为 Fashion.accdb)时遇到了问题。这是我的 DataGridView 现在的样子(我知道它不多):
<asp:GridView ID="dataGridView1" runat="server" EnableModelValidation="True">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
编辑
这是错误:
Exception Details: System.Data.OleDb.OleDbException: No value given
for one or more required parameters.
我的想法是在文本框中输入客户编号。然后我会点击提交按钮。然后该事件将从输入的文本框中检索该号码,获取数据库中与该客户相关的信息,并将其显示在 GridView
中
oledb 参数与 sql 服务器的不同。
...where CustNum = @MyCust
需要
...where CustNum = ? (or: where CustNum = [?])
设置适配器命令后,设置参数:
cmd.Parameters.Add("@CustNum", OleDbType.Integer, 15).Value = yourValue;
希望对您有所帮助。
我想知道如何使用从 Select 语句中找到的信息填充 DataGridView。这是 select 按钮的事件:
private void SelectCust(object sender, EventArgs e)
{
OleDbConnection conn = null;
conn = new OleDbConnection(
"Provider=Microsoft.ACE.OLEDB.12.0; " + "Data Source=" + "c:/inetpub/ftproot/ora103/App_Data/Fashion.accdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand("Select * from Customer where CustNum = @MyCust ", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
if (dt.Rows.Count > 0)
{
}
conn.Close();
}
我想使用 "if" 语句插入我从客户 table 那里收集的信息,并填充 DataGridView。但是,我在将 DataGridView 连接到我的 Microsoft Access 数据库(称为 Fashion.accdb)时遇到了问题。这是我的 DataGridView 现在的样子(我知道它不多):
<asp:GridView ID="dataGridView1" runat="server" EnableModelValidation="True">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
编辑
这是错误:
Exception Details: System.Data.OleDb.OleDbException: No value given for one or more required parameters.
我的想法是在文本框中输入客户编号。然后我会点击提交按钮。然后该事件将从输入的文本框中检索该号码,获取数据库中与该客户相关的信息,并将其显示在 GridView
中oledb 参数与 sql 服务器的不同。
...where CustNum = @MyCust
需要
...where CustNum = ? (or: where CustNum = [?])
设置适配器命令后,设置参数:
cmd.Parameters.Add("@CustNum", OleDbType.Integer, 15).Value = yourValue;
希望对您有所帮助。