如何在变量中存储查询? [英] How to store a query in a variable?

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

问题描述

嗨程序员!



我知道如何在ASP.Net中连接数据库和执行查询。但我不知道如何在变量中存储查询结果。例如,我想存储此查询的结果:

Hi programmers!

I know how to connect database and execute queries in ASP.Net. But I do not know on how to store the result of a query in a variable. Like for example, I would like to store the result of this query:

SqlCommand cmd = new SqlCommand("SELECT Milestone_Code FROM tblMilestone WHERE MIlestone_Name='"+DropdownList1.SelectedItem+"'", amicassa);



in string res。该怎么做?

请帮助我!!!


in string res. How to do that?
PLEASE HELP ME!!!

推荐答案





SQL客户端完整参考: http:// msdn.microsoft.com/en-us/library/system.data.sqlclient(v=vs.110).aspx [ ^ ]





SQL命令详细信息: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v = vs.110).aspx [ ^ ]



第二个链接将演示如何获取数据并将其设置为变量。



1st将帮助您了解其他预期SQL Cl ient
Hi,

SQL Clients Full ref : http://msdn.microsoft.com/en-us/library/system.data.sqlclient(v=vs.110).aspx[^]


SQL Command Details : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand(v=vs.110).aspx[^]

2nd link will demonstrate how to fetch data and set it in a variable.

1st will help you to learn other prospective of SQL Client


对连接执行此查询。

然后使用此阅读器定义数据读取器并存储结果。

参考

使用DataReader检索数据 [ ^ ]

使用C#.NET DataReader检索数据 [ ^ ]
Execute this query against a connection.
Then define a data reader and store results using this reader.
Refer
Retrieving Data Using a DataReader[^]
Retrieving Data Using a C# .NET DataReader [^]


您可以使用 ExecuteScalar 如果您的查询返回一个值。对于多个,您可以使用 ExecuteReader 并将读取器中的值存储到您自己的对象中。



作为旁注,您的代码是否有效?什么 SelectedItem 持有?您可能需要根据需要将其更改为 SelectedValue SelectedText
You can make use of ExecuteScalar if your query returns one value. For more than one, you can make use of ExecuteReader and store values from reader into your own object.

As a side note, will your code work? What does SelectedItem hold? You may have to change it to SelectedValue or SelectedText based on your needs.


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

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