0

此代码的目标是通过单击表格数据来动态更新网站。命令字符串有可变章节,我只喜欢更新和重新加载 gridview。

protected void Page_Load(object sender, EventArgs e)
{
string masterfile = "C://Temp/List of Validations.xlsx";
string CS = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + masterfile + ";Extended Properties=Excel 12.0;";
using (OleDbConnection con = new OleDbConnection(CS))
{
con.Open();
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Validation$] where chapter = @chapter", con);
//How to Dynamically assign this ch variable using the value table data text(01, 02, 03) and reload the contents in the gridview1.  
    cmd.Parameters.AddWithValue("@chapter", ch)
    using (OleDbDataReader rdr = cmd.ExecuteReader())
{
             DataTable mastertable = new DataTable();
               mastertable.Columns.Add("Chapter");
               mastertable.Columns.Add("Section");
}
etc...
GridView1.DataSource = mastertable;
GridView1.DataBind();
}

以下是我使用服务器对象所做的测试,它工作得很好

public OleDbConnection connectionobject() {
connection object code 
return con;
}
public OleDbCommand commandobject(OleDbConnection con, int ch) {
command object code
return cmd;
}
protected void Button1_Click(object sender, EventArgs e)
{
int ch = Convert.ToInt32(TextBox1.Text);
OleDbConnection con;
con = connectionobject();
OleDbCommand cmd;
cmd = commandobject(con, ch);
con.Open();
using (OleDbDataReader rdr = cmd.ExecuteReader())
{
GridView3.DataSource = rdr;
GridView3.DataBind();
}
4

0 回答 0