程序类型:Windows Form 应用程序窗体上放了:Label1,TextBox1,TextBox2
还有一个TextBox3
又放了一个按钮在TextBox3中输入一个数值18做为年龄用类似:select 序号,姓名,年龄 from ta 年龄=???? 的串在按钮中写什么代码可以把返回的内容“序号,姓名,年龄”依次显示于Label1,TextBox1,TextBox2中?服务器:Server1
数据库: test
帐户:sa
密码:mypass
还有一个TextBox3
又放了一个按钮在TextBox3中输入一个数值18做为年龄用类似:select 序号,姓名,年龄 from ta 年龄=???? 的串在按钮中写什么代码可以把返回的内容“序号,姓名,年龄”依次显示于Label1,TextBox1,TextBox2中?服务器:Server1
数据库: test
帐户:sa
密码:mypass
解决方案 »
- 谁有C#的WinForm编程资料啊
- 线程间操作无效: 从不是创建控件“Form1”的线程访问它。
- 为 200分求救 winform修改App.config 加分
- 如何获取一个目录下包含子目录内的所有文件名,并保存在一个数组中
- c#系统开发结束后,打包之后,使用机器中对框架存在那些要求
- 关于DataGrid里对数据增删改问题!;
- C#updateAPter.Update 重命名的表字段不能更新
- 老板要杀人了!C# WebBrowser 和 IE浏览完全不同,貌似就想IE6不兼容一样。
- c#读机器所有串口的问题
- 请问那里可以找到兼职的工作?
- 急!hashtable
- 对TIFF图像进行黑白颜色反转时遇到的问题!
reader.Read();
Label1.Text=reader["序号"].ToString();
TextBox1.Text=reader["姓名"].ToString();
TextBox2.Text=reader["年龄"].ToString();
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();
//通过用户名取得 其详细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();
}
}
}
}
}
不过有一小点
要是那个19不是具体数,是textBox3内的值呢?我原问也是这么问的
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=19", con);
里的19改成引用textBox1里的数字,就圆满结帖了,我是刚学,什么也不会啊
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄="+textBox1.Text, con);
SqlCommand cmd = new SqlCommand("select 序号,姓名,年龄 from ta 年龄=@age", con);
cmd.Parameters.AddWithValue("@age",19);