将Datareader转换为字符串 [英] Convert Datareader to String

查看:124
本文介绍了将Datareader转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将sqlDataReader转换为String,以便将数据库值与用户输入的文本值进行比较

i need to convert the sqlDataReader to String for the comparing of database value to user entered text value
how to do?

推荐答案

如db7uk的注释中所述,您不能将数据读取器转换为字符串.您可以做的是使用数据读取器提取值.

此处 [
As mentioned in the comments by db7uk, you can''t convert a datareader into a string. What you CAN do is to use the datareader to pull values.

Here[^] is an article that I found on google that may help you.


sqlconnection con = new sql connction("connection string");
sqlcommand com = new sqlcommand("select columnname from  tablename",con);
con.open();
sqldatareader dr = com.excutereader();
dr.read();

if(!dr.IsDBNull(0) && textbox1.text==dr.GetString(0))
{
    messagebox.show("anything");
}

con.close();



希望这对您有帮助



hope this helps


在这种情况下可以使用

if(dr ["City"]!= null)
{
objAdsProperties.City = dr ["City"]作为字符串;
}
其他
{
objAdsProperties.City =";
}


对于整数值

objAdsProperties.BuiltYear = dr ["BuiltYear"]为int吗? ?? default(int);
this will work in this case

if (dr["City"] != null)
{
objAdsProperties.City = dr["City"] as string;
}
else
{
objAdsProperties.City = "";
}


for integer value

objAdsProperties.BuiltYear = dr["BuiltYear"] as int? ?? default(int);


这篇关于将Datareader转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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