本文介绍了如何在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#中获取查询值
发布评论