从数据库检索信息 [英] retrieve information from database

查看:76
本文介绍了从数据库检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我需要从数据库中检索信息
我应该使用什么代码

hello

I need to retrieve information from database
what the code I should use

string ip;
                        ip = GetIP();
                        SqlCommand command = new SqlCommand("SELECT DeviceName FROM ConnectedDevice  WHERE IP= @IP AND IDName=@IDName  ", connection);
                        command.Parameters.Add(new SqlParameter("@IP", ip));
                        command.Parameters.Add(new SqlParameter("@IDName", name));
                        command.ExecuteNonQuery();

                        SqlDataReader reader1 = command.ExecuteReader();
                         if (reader1.HasRows)
                            {

                                device = reader1.GetValue(0).ToString();
                                
                              
                            }



代码



the code

device = reader1.GetValue(0).ToString();



不要检索任何东西并给出错误
我应该用??替换什么代码?

谢谢:)



do not retrieve any thing and give an error
what the code should I replace it with ??

thanks :)

推荐答案

在这里看看: ^ ]-在页面底部,您将找到一个解决方案:如何从datareader读取数据?
Take a look here: SqlDataReader Class[^] - on the bottom of page you''ll find a solution: how to read data from datareader?


要开始选择声明,替换
To start for a select statement, replace
command.ExecuteNonQuery();




with

SqlDataAdapter sqlDa = new SqlDataAdapter(command);
DataSet ds= new DataSet();
sqlDa.Fill(ds);



的状态
if (reader1.HasRows)
{
       device = reader1.GetValue(0).ToString();
}


写这个


Write this

if (reader1.HasRows)
{
   if(reader1.Read())
   {
       device = reader1.GetValue(0).ToString();
   }
}


您可以删除此行(删除此行)


and you can remove this line(delete this line)

command.ExecuteNonQuery();


这篇关于从数据库检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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