C#中从SQL中查询到一个字段值怎么赋值给textBox2.Text
[DllImport("User32.dll")]publicstaticexternintMessageBox(inth,stringm,stringc,inttype...
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
public SqlConnection cn = new SqlConnection("server=.;database=db_ldsztj;user id=sa;password=");
public ModifyPassword()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim()=="")
MessageBox(0, "点击‘查询’按钮前,请先输入用户编码!", "提示!", 0);
else
{
cn.Open();
SqlCommand cmd = new SqlCommand("", cn);
cmd.CommandText = "select * from db_ldsztj.dbo.tb_userlist where id='" + textBox1.Text.Trim() + "'";
if (null == cmd.ExecuteScalar())
MessageBox(0, "你输入的用户编码系统中不存在,请先输入正确的用户编码!", "提示!", 0);
else
{
textBox2.Text = "select username from tb_userlist where id='" + textBox1.Text.Trim() + "'";
textBox5.Text = "select quan from tb_userlist where id='" + textBox1.Text.Trim() + "'";
}
cn.Close();
}
结果:怎么点击“查询“按钮后,textBox2和textBox5分别显示的是 赋值给它的SQL查询语句了,比如:textBox2框中显示的是select username from tb_userlist where id='001'
结果:比如:textBox2框中显示的是lilinjun,操作员才对,语句在SQL2000中执行结果是显示lilinjun的
谢谢了,我找到原因了,再加上
string textbox2 = "select username from tb_userlist where id='" + textBox1.Text.Trim() + "'";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(textbox2, cn);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
string username = ds.Tables[0].Rows[0]["username"].ToString();
this.textBox2.Text = username;
cn.Close();
就可以了 展开
public static extern int MessageBox(int h, string m, string c, int type);
public SqlConnection cn = new SqlConnection("server=.;database=db_ldsztj;user id=sa;password=");
public ModifyPassword()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim()=="")
MessageBox(0, "点击‘查询’按钮前,请先输入用户编码!", "提示!", 0);
else
{
cn.Open();
SqlCommand cmd = new SqlCommand("", cn);
cmd.CommandText = "select * from db_ldsztj.dbo.tb_userlist where id='" + textBox1.Text.Trim() + "'";
if (null == cmd.ExecuteScalar())
MessageBox(0, "你输入的用户编码系统中不存在,请先输入正确的用户编码!", "提示!", 0);
else
{
textBox2.Text = "select username from tb_userlist where id='" + textBox1.Text.Trim() + "'";
textBox5.Text = "select quan from tb_userlist where id='" + textBox1.Text.Trim() + "'";
}
cn.Close();
}
结果:怎么点击“查询“按钮后,textBox2和textBox5分别显示的是 赋值给它的SQL查询语句了,比如:textBox2框中显示的是select username from tb_userlist where id='001'
结果:比如:textBox2框中显示的是lilinjun,操作员才对,语句在SQL2000中执行结果是显示lilinjun的
谢谢了,我找到原因了,再加上
string textbox2 = "select username from tb_userlist where id='" + textBox1.Text.Trim() + "'";
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(textbox2, cn);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
string username = ds.Tables[0].Rows[0]["username"].ToString();
this.textBox2.Text = username;
cn.Close();
就可以了 展开
2个回答
2011-06-02
展开全部
你这里写的给它赋一个字符串,当然就显示一个字符串咯
textBox2.Text = "select username from tb_userlist where id='" + textBox1.Text.Trim() + "'";
要改为: textBox2.Text = cmd.ExecuteScalar()+"";
textBox2.Text = "select username from tb_userlist where id='" + textBox1.Text.Trim() + "'";
要改为: textBox2.Text = cmd.ExecuteScalar()+"";
追问
谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询