如何使用文本框传递表名称的参数 [英] How Do I Pass Parameter For Table Name Using Textbox

查看:76
本文介绍了如何使用文本框传递表名称的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在这里,我传递了一张Table Giving表的名字。



但是现在我想要通过表名文本框。我试过很多方法来传递表名的值。



请帮助代码。



我猜它会起作用



  string  tablename = txtSearch.Text; 
字符串 query = 从数据库中选择*其中tablename = @ tablename;



什么是确切查询。



如何修改它以便从文本框中读取。





< pre lang =c#> private void btnSearch_Click( object sender,EventArgs e)
{
connection.Open();
尝试
{

OleDbCommand command = new 的OleDbCommand();
command.Connection = connection;
string query = 从TestTable中选择* ;
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

command.ExecuteNonQuery();


}
catch (Exception ex)
{
MessageBox.Show(ex的ToString());
}
connection.Close();
}

解决方案

如果你想要的只是更改表的名称并从表中获取所有列,您可以使用 String.Format [ ^ ]方法。



示例:

 字符串 query = 字符串 .Format( 选择*从{0},txtSearch.Text); 





如果您想要更高级的查询,您应该关注解决方案1中的建议。


不确定你想要什么,但是......



MS Access提供了一种创建方法 pa rametrized query [ ^ ]。

 PARAMETERS [myParam]  CHAR ; 
SELECT < FieldList>
FROM TableName
WHERE FieldName = [myParam];



现在,您需要创建 OleDbCommand with parameters(s) [ ^ ]。使用 AddWithValue [ ^ ]方法:)


谢谢Sir先生工作良好。

非常感谢你。





private void btnSearch_Click(object sender,EventArgs e)

{

connection.Open( );

尝试

{



OleDbCommand command = new OleDbCommand();

command.Connection = connection;

string tablename = Txt_Search.Text;

String query = String.Format(Select * from {0},Txt_Search.Text );

command.CommandText = query;



OleDbDataAdapter da = new OleDbDataAdapter(command);

DataTable dt = new DataTable();

da.Fill(dt);

dataGridView1.DataSource = dt;



// command.ExecuteNonQuery();





}

catch(exception ex)

{

MessageBox.Show(ex.ToString());

}

connection.Close();

}


Hi
Here I passed a Table Giving table's Name.

But Now i want to Pass Table's Name through textbox. I tried in many ways to pass values for table's name.

Please help with code.

I am guessing It would work as

string tablename = txtSearch.Text;
String query = "Select * from Database Where tablename=@tablename";


What is exact query.

How to modify it to read from textbox.


private void btnSearch_Click(object sender, EventArgs e)
{
connection.Open();
try
{

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "Select * from TestTable";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

command.ExecuteNonQuery();


}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
connection.Close();
}

解决方案

If the only thing you want is to change the name of the table and get all columns from the table, you can use the String.Format[^] method.

Example:

String query = String.Format("Select * from {0}", txtSearch.Text);



If you want a more advanced query you should follow the advice in Solution 1.


Not sure what you want, but...

MS Access provides a way to create parametrized query[^].

PARAMETERS [myParam] CHAR;
SELECT <FieldList>
FROM TableName
WHERE FieldName = [myParam];


Now, you need to create OleDbCommand with parameter(s)[^]. It's quite simple with AddWithValue[^] method :)


Thank You Sir Its working Good.
Thank You So Much.


private void btnSearch_Click(object sender, EventArgs e)
{
connection.Open();
try
{

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string tablename = Txt_Search.Text;
String query = String.Format("Select * from {0}", Txt_Search.Text);
command.CommandText = query;

OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;

// command.ExecuteNonQuery();


}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
connection.Close();
}


这篇关于如何使用文本框传递表名称的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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