C#文本框中的sql数据 [英] sql data in c# text box
本文介绍了C#文本框中的sql数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想在文本框中显示sql命令答案.实际上是在文本框中或多个文本框中显示单元格或列信息或行信息.
hi all,
I want to show sql command answer in text box.in fact to show cells or columns information or rows information in a text box or in severl text box.
what is the way?
推荐答案
正如我意识到的那样,您想在TextBox中而不是GridView或其他控件中显示结果.好了,您可以将SqlDataReader和SqlCommand一起使用以获取数据,然后将其放入TextBox中.这是示例代码:
Hi,
As I realized you want to show the result in a TextBox instead of a GridView or other controls. Well, you can use SqlDataReader along with SqlCommand to fetch the data and then put it in the TextBox. Here is a sample code:
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
try
{
// Open connection to the database
string ConnectionString = "server=xeon;uid=sa;"+
"pwd=manager; database=northwind";
con = new SqlConnection(ConnectionString);
con.Open();
// Set up a command with the given query and associate
// this with the current connection.
string CommandText = "SELECT FirstName, LastName" +
" FROM Employees" +
" WHERE (LastName LIKE @Find)";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
// Add LastName to the above defined paramter @Find
cmd.Parameters.Add(
new SqlParameter(
"@Find", // The name of the parameter to map
System.Data.SqlDbType.NVarChar, // SqlDbType values
20, // The width of the parameter
"LastName")); // The name of the source column
// Fill the parameter with the value retrieved
// from the text field
cmd.Parameters["@Find"].Value = txtFind.Text;
// Execute the query
rdr = cmd.ExecuteReader();
// Fill the list box with the values retrieved
lbFound.Items.Clear();
while(rdr.Read())
{
yourTextBox.Text+= rdr["FirstName"].ToString() +
" " + rdr["LastName"].ToString();
}
}
catch(Exception ex)
{
// Print error message
MessageBox.Show(ex.Message);
}
finally
{
// Close data reader object and database connection
if (rdr != null)
rdr.Close();
if (con.State == ConnectionState.Open)
con.Close();
}
这个想法是使用常规的Ado.Net命令正常获取数据,然后将其放入TextBox中.这是上述示例的全文:
http://www.akadia.com/services/dotnet_data_reader.html [
The idea is to use regular Ado.Net commands to get the data normally and then put it in a TextBox. Here is the full text of the above sample :
http://www.akadia.com/services/dotnet_data_reader.html[^]
I hope it helps,
Cheers
这篇关于C#文本框中的sql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文