问题描述
OleDbConnection con = new OleDbConnection();
//您的数据源当前我放置的位置路径D驱动器位置
string dsource = @D:\ Workinging \WebBasedData \WebBasedData \ Data \Attachments_;
//将数据源路径放在连接字符串中,例如if您在D:\目录中有csv文件更改数据源= D:\
string constr = @Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ dsource +; Extended Properties ='text; HDR = Yes; FMT = Delimited';;
try
{
con.ConnectionString = constr;
//为命令对象创建实例
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
//在下面设置你的文件名查询
cmd.CommandText =select * from [fpyield 12162013 1030.csv];
cmd.C ommandText =从[fpyield 12162013 0930.csv]中选择进程;
cmd.CommandText =从[fpyield 12162013 0830.csv]中选择产品;
//打开Oledb连接读取CSV文件
con.Open();
//创建一个数据表来存储CSV文件中的数据
DataTable dt = new DataTable();
//将数据加载到数据表中
dt.Load(cmd.ExecuteReader());
//绑定数据在Gridview中
GridViewShow.DataSource = dt;
GridViewShow.DataBind();
}
catch(exception ex)
{
Label3.Text =读时出错+ ex.ToString();
}
终于
{
con.Close();
}
这就是我的好奇心。有没有办法让我使用不同的按钮来点击并控制command.text ....但我只想让我的con.open只运行一次。对于ex:3个不同的按钮来做不同的命令。谢谢
OleDbConnection con = new OleDbConnection();
//Your datasource Location path currently i placed D drive location
string dsource = @"D:\Working \WebBasedData\WebBasedData\Data\Attachments_";
//Put your datasource path in the connection string for example if you have csv files in D:\ directory change datasource= D:\
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dsource + ";Extended Properties='text;HDR=Yes;FMT=Delimited';";
try
{
con.ConnectionString = constr;
//create instance for command object
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
// set your file name in the below query
cmd.CommandText = "select * from [fpyield 12162013 1030.csv]" ;
cmd.CommandText = "select process from [fpyield 12162013 0930.csv]" ;
cmd.CommandText = "select product from [fpyield 12162013 0830.csv]" ;
//Open Oledb Connection to read CSV file
con.Open();
//Create one datatable to store data from CSV file
DataTable dt = new DataTable();
// Load Data into the datatable
dt.Load(cmd.ExecuteReader());
//Bind data in the Gridview
GridViewShow.DataSource = dt;
GridViewShow.DataBind();
}
catch (Exception ex)
{
Label3.Text = "Error while reading" + ex.ToString();
}
finally
{
con.Close();
}
this is what i curious. is there any way for me to use different button to clicked and control the command.text.... but i only want my con.open to run it one time only. for ex: 3 different button to do different command. thanks
推荐答案
这篇关于如何使用不同的按钮来控制页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!