如何从表的一列相对于表的另一列获取价值? [英] How to get value from one column of a table with respect to other column of a table?

查看:65
本文介绍了如何从表的一列相对于表的另一列获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的SQL数据库中有两个列.一种是用户名,另一种是ID.如果我在第一个文本框中输入ID,则第二个文本框中可以显示相应的用户名.请帮助我.

Hi,
I have two colomns in my sql database. One is username and other one column for ID. If i give id in first text box, the second text box can show the corresponding username. Kindly help me.

Private void lblGET_Click(object sender, EventArgs e)
{
   SqlDataAdapter da = new SqlDataAdapter("select username from login where Answer='"+txtFCN.Text+"'",con);
   dt = new DataTable();
   da.Fill(dt);
   if (dt.Rows.Count != 0)
   { lblANSUN.Text = dt.Rows.Count.ToString();
   }
}


我正在使用此代码...但是它仅显示行数..请清除我的疑问.

谢谢,

Viswanathan.M


I am using this code...but it shows only number of rows..please clear my doubt.

Thanks,

Viswanathan.M

推荐答案

这是不好的方法,因为它允许进行sql注入.您需要限制文本框值的长度,并确保它不是讨厌的,即仅包含数字值.

This is the bad approach as it allows for sql injection. You would need to limit the length of the textbox value and also make sure it is not a nasty i.e. only contains a numeric value.

String sqlcmd = "Select Username from theTable where ID='" + textbox.text + "'";



最好的方法是使用参数;
参见本文(但也有许多其他文章); C#和表值参数 [



The best way is to use parameters;
See this article (but there are many others also); C# and Table Value Parameters[^]


这篇关于如何从表的一列相对于表的另一列获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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