如何在C#中获取查询值 [英] how to get the query value in C#

查看:113
本文介绍了如何在C#中获取查询值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我正在C#Windows应用程序中执行代码,而我必须在C#中获取查询值.
下面我给出代码

Hi All
I am doing code in C# windows application where the query value I have to get in C#
Below I give the code

string qry = "Select COUNT(rack)FROM Report ";
OleDbCommand cmd = new OleDbCommand();
cmd = new OleDbCommand(qry, conn);
OleDbDataReader dr = cmd.ExecuteReader();



在这里检查给定的总行数是19的SQL查询
但是如何将其存储在C#中
感谢全部



where I check that the SQL query given total number of row is 19
But how can I store that in C#
Thanks To All

推荐答案

对于单个值,您需要使用ExecuteScalar: ^ ]
For single values you need to use ExecuteScalar : http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.executescalar.aspx[^]



您可以使用 OleDbDataAdapter [ ^ ]获取值并将其存储到 DataTable [ ^ ]或数据集 [
Hi,
You can use OleDbDataAdapter[^] to get the values and store it to DataTable[^] or DataSet[^]
Try this:
DataTable dt = new DataTable();
string qry = "Select COUNT(rack)FROM Report";
OleDbCommand cmd = new OleDbCommand();
cmd = new OleDbCommand(qry, conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(dt);//Here you are having a datatable with all your 19 columns



祝一切顺利.
--Amit



All the best.
--Amit


SqlCommand cmd=new SqlCommand("Select COUNT(rack)FROM Report");
cmd.Connection=con;
con.Open();
int i=cmd.ExecuteScalar();
con.Close();


变量i现在将具有计数.


The variable i will have the count now.


这篇关于如何在C#中获取查询值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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