如何使用不同的按钮来控制页面 [英] how to use different button to control page

查看:64
本文介绍了如何使用不同的按钮来控制页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

网络已断开连接,这意味着当用户执行操作然后断开连接时,您的代码就会运行。每次单击按钮时,您都需要打开与数据库的连接(该按钮会导致回发。)
The web is disconnected meaning your code runs when the user does something and then it disconnects. You'll need to open the connection to your db each time a button is clicked (the button causes a postback.)


这篇关于如何使用不同的按钮来控制页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆