如何在C#中获取查询值

编程入门 行业动态 更新时间:2024-10-26 18:23:03
本文介绍了如何在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: msdn.microsoft/zh-CN/library/system.data.oledb.oledbcommand.executescalar.aspx [ ^ ] For single values you need to use ExecuteScalar : msdn.microsoft/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#中获取查询值

本文发布于:2023-11-16 16:17:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!