使用DataReader将数据写入文本框 [英] Write data to textbox with datareader

查看:89
本文介绍了使用DataReader将数据写入文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在创建一个网站项目,我需要将数据从数据库写入文本框.
我使用了数据读取器.我确定它已连接到数据库,但是它不能渗透到while循环中.
有人可以帮我吗?

这是我的代码;

Hi,

I am creating a website project and I need to write data to textbox from database.
I used datareader. I am sure that it is connected to database, but it can not penetrate into while loop.
Could anyone help me?

Here is my code;

SqlConnection conn = new SqlConnection();
SqlDataReader dr;

string TCNumber = Request.QueryString["TC"].ToString();
                    

SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC='"+TCNumber+"'");

comm2.Connection = conn;

dr = comm2.ExecuteReader();

while (dr.Read())
{
       TCNo.Text = dr["TC"].ToString();
       Name.Text = dr["Name"].ToString();
       Surname.Text = dr["Surname"].ToString();
       BirthDate.Text = dr[3].ToString();
       Phone_Number.Text = dr[4].ToString();
}
dr.Close();
dr.Dispose();
conn.Close();

推荐答案

您尚未打开连接.
所以写
You have not opened your connection.
so write
comm2.Connection.Open();


行后


after the line

comm2.Connection = conn;


在该行之前


and before the line

dr = comm2.ExecuteReader();


希望它能解决您的问题.
请让我知道...


Hope it solves your problem.
Please let me know...


每次循环时,您都要设置每个文本框的新值,并丢弃以前的任何值.因此,如果您有多于TC等于TCNumber的行,则仅显示最后一行-这可能不是您想要的.除了尝试使用+=代替="之外,我不完全知道您要实现的目标.

如果没有任何匹配项,则将不会显示任何内容.如果没有显示任何记录,则说明您没有匹配的记录,或者您的查询字符串不包含您认为的记录.

顺便说一句:不要串联字符串来构建SQL命令.它使您对意外或蓄意的SQL注入攻击敞开大门,这可能会破坏整个数据库.
Every time you go round your loop, you set the new value of each textbox, and discard any previous value. So if you have more than one row where TC is equal to TCNumber then only the last will be displayed - this is probably not what you want. I don''t know exactly what you are trying to achieve, so I can''t suggest a solution, other than "Try using += instead of ="

If you don''t have any that match, nothing will be displayed at all. If not of your records are displaying, then either you have not records that match, or your querystring does not contain what you think it does.

And BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


一切都很好,只需按如下所示打开连接即可....

Hi Every thing is fine just open the connection as follows....

SqlConnection conn = new SqlConnection();
SqlDataReader dr;
 
string TCNumber = Request.QueryString["TC"].ToString();
                    
 
SqlCommand comm2 = new SqlCommand("SELECT TC, Name, Surname, BirthDate, Phone from Pers_Info WHERE TC=''"+TCNumber+"''");
 
comm2.Connection = conn;
  conn.Open();
dr = comm2.ExecuteReader();
 
while (dr.Read())
{
       TCNo.Text = dr["TC"].ToString();
       Name.Text = dr["Name"].ToString();
       Surname.Text = dr["Surname"].ToString();
       BirthDate.Text = dr[3].ToString();
       Phone_Number.Text = dr[4].ToString();
}
dr.Close();
dr.Dispose();
conn.Close();


这篇关于使用DataReader将数据写入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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