如何从数据库中获取一个简单的字符串

发布于 2024-10-17 00:29:41 字数 384 浏览 6 评论 0原文

下面的代码不起作用。该表中只有 1 行。我如何获得 sql 将返回的语句?:

SqlConnection conn = new SqlConnection(connectionStringArg);
SqlCommand command = new SqlCommand("select applicationname from tbl_settings");
command.Connection = conn;
conn.Open();
string simpleValue = command.ExecuteReader()[0].ToString();
conn.Close();
return simpleValue;

好的,任何有关如何实现这个相对简单的任务的帮助都会很棒。

The following code does not work. There is only 1 row in this table. How do I just get the statement that the sql would return?:

SqlConnection conn = new SqlConnection(connectionStringArg);
SqlCommand command = new SqlCommand("select applicationname from tbl_settings");
command.Connection = conn;
conn.Open();
string simpleValue = command.ExecuteReader()[0].ToString();
conn.Close();
return simpleValue;

Okay any help on how to achieve this relatively simple task would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

别闹i 2024-10-24 00:29:41

由于只返回一个值,您可以这样做:

 string value = (string)command.ExecuteScalar();

如果您需要返回的第一行中的第一列以上的值,则需要使用 ExecuteReader()。请参阅 driis 的答案

Since there's only a single value returned, you could do this:

 string value = (string)command.ExecuteScalar();

If you need more than the first column from the first row returned, you'll need to use ExecuteReader(). See driis' answer.

留蓝 2024-10-24 00:29:41

DataReader 不能这样索引。您需要类似的东西:

using(conn)
using(var reader = command.ExecuteReader()) 
{
    reader.Read();
    string simpleValue = reader.GetString(0);
}

总体思路是推进每条记录的读取器(使用Read),然后您可以从该行读取值。正如另一个答案中所指出的,如果您知道结果集中只有一个值,command.ExecuteScalar() 就会为您提供这一点。

The DataReader can't be indexed like that. You need something like:

using(conn)
using(var reader = command.ExecuteReader()) 
{
    reader.Read();
    string simpleValue = reader.GetString(0);
}

The general idea is to advance the reader for each record (using Read), then you can read values from the row. As pointed out in another answer, if you know there is only one single value in the result set, command.ExecuteScalar() gives you just that.

风吹雨成花 2024-10-24 00:29:41

您必须在从 ExecuteReader 返回的 DataReader 上调用 Read 方法才能到达第一行。像这样的东西:

        SqlDataReader rdr = command.ExecuteReader();
        if (rdr.Read())
             ...

You have to call the Read method on a DataReader returned from ExecuteReader to get to the first row. Something like this:

        SqlDataReader rdr = command.ExecuteReader();
        if (rdr.Read())
             ...
蛮可爱 2024-10-24 00:29:41

你会做例如

    using(SqlConnection conn = new SqlConnection(connectionStringArg))
    using(SqlCommand command = new SqlCommand("select applicationname from tbl_settings")) {

        command.Connection = conn;
        conn.Open();
        SqlDataReader reader = command.ExecuteReader();
        if(reader.read()) {
            return reader.GetString(0);
        }
        return null;
    }

You'd do e.g.

    using(SqlConnection conn = new SqlConnection(connectionStringArg))
    using(SqlCommand command = new SqlCommand("select applicationname from tbl_settings")) {

        command.Connection = conn;
        conn.Open();
        SqlDataReader reader = command.ExecuteReader();
        if(reader.read()) {
            return reader.GetString(0);
        }
        return null;
    }
等风来 2024-10-24 00:29:41

For a single string you want to use ExecuteScalar to get a single value and then attempt to cast it to string.
From doc: Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

Example:
string result = command.ExecuteScalar() as string;

For small stuff I often find LINQ2SQL the easiest to set up and use. This tutorial should get you up and running in a few minutes. For bigger projects LINQ2SQL is considered obsolete, see this question for discussion and alternatives.

江湖正好 2024-10-24 00:29:41

检查 DataReader.Read()需要调用它。

Check DataReader.Read() you need to call it.

暖树树初阳… 2024-10-24 00:29:41
string applicationname = "";
using(var reader = command.ExecuteReader()) 
 {
    reader.Read();
    applicationname = reader.GetString(reader.GetOrdinal("applicationname"));
 }
string applicationname = "";
using(var reader = command.ExecuteReader()) 
 {
    reader.Read();
    applicationname = reader.GetString(reader.GetOrdinal("applicationname"));
 }
第几種人 2024-10-24 00:29:41
var query_result = com.ExecuteScalar() as string;
if (!string.IsNullOrEmpty(query_result )) // do your thing here...
var query_result = com.ExecuteScalar() as string;
if (!string.IsNullOrEmpty(query_result )) // do your thing here...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文