通过数据读取器读取值 [英] Reading values through Data Reader

查看:53
本文介绍了通过数据读取器读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用数据读取器从数据库中表的列中读取值.
现在,我想将列的值与文本框中的文本进行比较.
但是,仅比较第一条记录的值.
这是我的代码,看看是否有人可以帮忙!

Hi,
I am using a data reader to read values from a column of a table in the database.
Now, i wanted to compare the values of the column with the text in the text box.
However, only value of the first record gets compared.
This is my code, see if anyone can help!

public partial class AJAXTestPage : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader reader;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        con.Open();
        cmd = new SqlCommand("select User_Name from Registration", con);
        reader = cmd.ExecuteReader();
        
    }
    protected void btnCheck_Availablity_Click(object sender, EventArgs e)
    {
        dt.Load(reader);
        if (txtUser_Name.Text == dt.Columns["User_Name"].ToString())
        {
            lblUnavailableUser_Name.Visible = true;
            lblAvailable.Visible = false;
        }
        else
        {
            lblAvailable.Visible = true;
            lblUnavailableUser_Name.Visible = false;
        }

    }
}




在此先感谢!!!




Thanks in advance!!!

推荐答案

我将添加到walterhevedeich的解决方案中.

I would add to walterhevedeich''s solution.

bool Exists;
foreach(DataRow row in dt.Rows)
{
        if (txtUser_Name.Text == row["User_Name"].ToString())
        {
            Exists = true;
            break;
        }
}
if(Exists)
{
        lblUnavailableUser_Name.Visible = true;
        lblAvailable.Visible = false;
}
else
{
        lblAvailable.Visible = true;
        lblUnavailableUser_Name.Visible = false;
}


如果要将它们与文本框进行比较,则需要遍历数据表中的所有行.您可以尝试以下操作:

You need to loop through all the rows on your data table if you want to compare them to your textbox. You can try something like the following:

foreach(DataRow row in dt.Rows)
{
        if (txtUser_Name.Text == row["User_Name"].ToString())
        {
            lblUnavailableUser_Name.Visible = true;
            lblAvailable.Visible = false;
        }
        else
        {
            lblAvailable.Visible = true;
            lblUnavailableUser_Name.Visible = false;
        }
}


很抱歉,您的代码混乱了.原因:
1.为什么要在不同的事件中执行命令并在不同的事件中加载值?
2.您希望通过此行实现什么:
if (txtUser_Name.Text == dt.Columns["User_Name"].ToString()).
这只会给出列的表达式(而不是值).

如果您要检查输入的用户名是否存在于数据库中,请对您的查询应用过滤器,并使之类似于以下内容:
"select User_Name from Registration where User_Name = " + txtUser_Name.Text
并使用ExecuteNonQuery方法代替"ExecuteReader",因为您只想检查值的存在.

希望有帮助!
Sorry to say but your code is messed up. Reasons:
1. Why execute the command in different event and load the values in different event?
2. What are you trying to achieve with this line:
if (txtUser_Name.Text == dt.Columns["User_Name"].ToString()).
This will just give the expression of the column (not value).

If you are trying to check if the entered user name exists in DB or not, apply filter to your query and make it something like this:
"select User_Name from Registration where User_Name = " + txtUser_Name.Text
and use ExecuteNonQuery method in place of "ExecuteReader" as you just want to check for the existence of a value.

Hope that helps!


这篇关于通过数据读取器读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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