从SQL Server取一行数并显示于TextBox中

needacoder 2012-04-03 10:47:54
程序类型:Windows Form 应用程序

窗体上放了:Label1,TextBox1,TextBox2
还有一个TextBox3
又放了一个按钮

在TextBox3中输入一个数值18做为年龄

用类似:select 序号,姓名,年龄 from ta 年龄=???? 的串

在按钮中写什么代码可以把返回的内容“序号,姓名,年龄”依次显示于Label1,TextBox1,TextBox2中?

服务器:Server1
数据库: test
帐户:sa
密码:mypass
...全文
360 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangchun1213 2012-04-03
  • 打赏
  • 举报
回复
lz是初学者?这问题还要问么?
hetengfei_ 2012-04-03
  • 打赏
  • 举报
回复
哎,下面我给也一个 ado.net 操作数据库 MSSQL 的例子,你可以参改一下:
//通过用户名取得 其详细info
public void GetUsers(string UserName)
{
//window 本地实例
string connectionString = "Data Source=.\\SqlExpress;DataBase=master;Integrated Security=SSPI;";
using(SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
using(SqlCommand comm = connect.CreateCommand() )
{
try
{
comm.CommandText="select [id] ,[UserName],[Password] from [master].[dbo].[T_User] where [UserName]=@UN";
comm.Parameters.Add(new SqlParameter("UN", UserName));
using (SqlDataReader reader = comm.ExecuteReader())
{
while (reader.Read())
{
long id =(long) reader.GetSqlInt64(reader.GetOrdinal("id"));
string UName = reader.GetString(reader.GetOrdinal("UserName"));
string PWord = reader.GetString(reader.GetOrdinal("Password"));
MessageBox.Show(id.ToString()+"-->"+UName+":"+PWord); //你在这里改改到textBox里
}
}
}
catch (SqlException ex)
{
//异常信信显示
string ErrorInfo="\n数据库执行出错了:\n";
ErrorInfo+="\n信息"+ex.Errors;
ErrorInfo+="\n类弄"+ex.Number;
ErrorInfo+="\n名称"+ex.Source;
ErrorInfo+="\n行号"+ex.LineNumber;
ErrorInfo+="\n详细"+ex.Message;
MessageBox.Show(ErrorInfo);
}
finally
{ //关闭数据库连接
if (connect.State == ConnectionState.Open)
{
connect.Close();
connect.Dispose();
}
}
}
}
}
mingcsharp 2012-04-03
  • 打赏
  • 举报
回复
using System.Data;
using System.Data.SqlClient;

string conStr = "server=Server1;database=test;uid=sa;pwd=mypass";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=19", con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Label1.Text = reader["序号"].ToString();
TextBox1.Text = reader["姓名"].ToString();
TextBox2.Text = reader["年龄"].ToString();
}
reader.Close();
con.Close();
needacoder 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
ADO.NET查询数据库,返回一个阅读器SqlDataReader reader

关键代码
reader.Read();
Label1.Text=reader["序号"].ToString();
TextBox1.Text=reader["姓名"].ToString();
TextBox2.Text=reader["年龄"].ToString();
[/Quote]
我很菜,请高人补全
dingxiang506 2012-04-03
  • 打赏
  • 举报
回复
方法很多很多的。
jyh070207 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
写一个方法查找数据库中的内容然后,返回一个dataset,然后接收绑定到你的控件上面
[/Quote]++
我爱小土豆 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

ADO.NET查询数据库,返回一个阅读器SqlDataReader reader

关键代码
reader.Read();
Label1.Text=reader["序号"].ToString();
TextBox1.Text=reader["姓名"].ToString();
TextBox2.Text=reader["年龄"].ToString();
[/Quote]
这个方法没用过,学习一下。。。。。。
我爱小土豆 2012-04-03
  • 打赏
  • 举报
回复
写一个方法查找数据库中的内容然后,返回一个dataset,然后接收绑定到你的控件上面
dalmeeme 2012-04-03
  • 打赏
  • 举报
回复
ADO.NET查询数据库,返回一个阅读器SqlDataReader reader

关键代码
reader.Read();
Label1.Text=reader["序号"].ToString();
TextBox1.Text=reader["姓名"].ToString();
TextBox2.Text=reader["年龄"].ToString();
dalmeeme 2012-04-03
  • 打赏
  • 举报
回复
用参数化SQL,避免注入
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=@age", con);
cmd.Parameters.AddWithValue("@age",19);
orochiheart 2012-04-03
  • 打赏
  • 举报
回复
发错了
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄="+textBox1.Text, con);
orochiheart 2012-04-03
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄="+textBox1.Text+, con);
needacoder 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
using System.Data;
using System.Data.SqlClient;

string conStr = "server=Server1;database=test;uid=sa;pwd=mypass";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlComma……
[/Quote]
谁把7楼回的这行
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=19", con);
里的19改成引用textBox1里的数字,就圆满结帖了,我是刚学,什么也不会啊
orochiheart 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]

引用 10 楼 的回复:
引用 7 楼 的回复:

谢鼓励,以后还需要你帮助我,请关注我
[/Quote]
这里高手很多的能帮你的人太多了 呵呵 我若时间允许自然会帮你的
needacoder 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
引用 7 楼 的回复:
[/Quote]
谢鼓励,以后还需要你帮助我,请关注我
needacoder 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
using System.Data;
using System.Data.SqlClient;

string conStr = "server=Server1;database=test;uid=sa;pwd=mypass";
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlComma……
[/Quote]
谢大手
不过有一小点
要是那个19不是具体数,是textBox3内的值呢?我原问也是这么问的
orochiheart 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

using System.Data;
using System.Data.SqlClient;

string conStr = "server=Server1;database=test;uid=sa;pwd=mypass";
SqlConnection con = new SqlConnection(conStr);
SqlComma……
[/Quote]
+1 楼主加油!!!都是从初学过来了

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧