如何打印数据集的单个值

发布于 2024-09-16 04:57:14 字数 298 浏览 3 评论 0原文

conn = new SqlConnection(@"Data Source=ASHISH-PC\SQLEXPRESS; initial catalog=bank; integrated security=true");
ada = new SqlDataAdapter("select total_amount from debit_account where account_no=12", conn);
ds = new DataSet();

ada.Fill(ds);

现在,我想打印数据集的值......如何?请帮我。

conn = new SqlConnection(@"Data Source=ASHISH-PC\SQLEXPRESS; initial catalog=bank; integrated security=true");
ada = new SqlDataAdapter("select total_amount from debit_account where account_no=12", conn);
ds = new DataSet();

ada.Fill(ds);

Now, I want to print value of the dataset... how? Please help me.

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

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

发布评论

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

评论(2

谜兔 2024-09-23 04:57:14

我建议这里最好的选择实际上不是 SqlDataAdapterDataSet,而是 SqlCommand。试试这个:

using(SqlConnection conn = new SqlConnection(@"Data Source=ASHISH-PC\SQLEXPRESS; initial catalog=bank; integrated security=true"))
{
    conn.Open()
    using (SqlCommand command  = new SqlCommand("select total_amount from debit_account where account_no=12", conn)
    {
        var result = command.ExecuteScalar();
        Console.WriteLine("The total_amount for the account is {0}", result);
    }
}

ExecuteScalar() SqlCommand 上的 方法返回查询返回的第一行第一列中的值,这在这种情况下是理想的。

如果您绝对必须使用数据集,那么您需要执行以下操作:

using(SqlConnection conn = new SqlConnection(@"Data Source=ASHISH-PC\SQLEXPRESS; initial catalog=bank; integrated security=true"))
{
    conn.Open()
    using (SqlDataAdapter adapter = new SqlDataAdapter("select total_amount from debit_account where account_no=12", conn)
    {
        var ds = new DataSet();
        adapter.Fill(ds);
        Console.WriteLine("The total_amount for the account is {0}", ds.Tables[0].Rows[0][0]); // Get the value from the first column of the first row of the first table
    }
}

注意: 我已将这两个示例包装在 C# using 语句,这确保您的所有数据库资源都已清理,因此您不会遇到任何泄漏非托管资源的问题。这并不是特别困难或复杂,所以非常值得做

I'd suggest that the best option here isn't actually a SqlDataAdapter and DataSet, but rather a SqlCommand. Try this:

using(SqlConnection conn = new SqlConnection(@"Data Source=ASHISH-PC\SQLEXPRESS; initial catalog=bank; integrated security=true"))
{
    conn.Open()
    using (SqlCommand command  = new SqlCommand("select total_amount from debit_account where account_no=12", conn)
    {
        var result = command.ExecuteScalar();
        Console.WriteLine("The total_amount for the account is {0}", result);
    }
}

The ExecuteScalar() method on SqlCommand returns the value in the first column of the first row that your query returns, which is ideal in this situation.

If you absolutely have to use a dataset then you'd want to do the following:

using(SqlConnection conn = new SqlConnection(@"Data Source=ASHISH-PC\SQLEXPRESS; initial catalog=bank; integrated security=true"))
{
    conn.Open()
    using (SqlDataAdapter adapter = new SqlDataAdapter("select total_amount from debit_account where account_no=12", conn)
    {
        var ds = new DataSet();
        adapter.Fill(ds);
        Console.WriteLine("The total_amount for the account is {0}", ds.Tables[0].Rows[0][0]); // Get the value from the first column of the first row of the first table
    }
}

Note: I've wrapped both examples in the C# using statement, this ensures that all your database resources are cleaned up so you don't have any problems with with leaking unmanaged resources. It's not particularly difficult or complicated so is well worth doing

挽清梦 2024-09-23 04:57:14

我认为,在这种情况下,您最好(从性能角度)使用 SqlCommand 而不是适配器和数据集,并调用 ExecuteScalar 方法。

请参阅 http://msdn.microsoft.com /en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

但是,如果您必须使用数据集,ds.Tables[0].Rows[0]["total_amount" ] 应该检索您的值。不过,您可能需要对值进行类型转换。

I think, in this case, you'd be better off (performance-wise) to use a SqlCommand instead of the adapter and dataset, and invoke the ExecuteScalar method.

See http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

If you must use the data set, however, ds.Tables[0].Rows[0]["total_amount"] should retrieve your value. You will probably need to type cast the value, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文