C#表适配器实际上返回了什么?

时间:2022-03-23 03:16:05

stack overflow !

堆栈溢出 !

I'm working on an application that manipulates SQL tables in a windows form application. Up until now, I've only been using the pre-generated Fill queries, and self-made update and delete queries (which return nothing).

我正在开发一个在Windows窗体应用程序中操作SQL表的应用程序。到目前为止,我只使用预先生成的Fill查询,以及自制的更新和删除查询(不返回任何内容)。

I am interested in storing the value of a single value from a single column (an 'nchar(15)' name), and though I have easily written the SQL code to return that value to the application, I have no idea what it will be returned as.

我感兴趣的是从单个列('nchar(15)'名称)存储单个值的值,虽然我已经轻松编写了SQL代码以将该值返回给应用程序,但我不知道它会是什么归还为。

SELECT [Contact First Name] FROM ContactSkillSet
WHERE [Contact ID] = @CurrentID

Can the result be stored directly as a string? Do I need to cast it? Invoke a toString method?

结果可以直接存储为字符串吗?我需要施展吗?调用toString方法?

Thanks!

谢谢!

Edit: Interestingly enough, the following works:

编辑:有趣的是,以下工作:

String firstName = this.contactSkillSetTableAdapter.GrabFirstName(userKey);

The joys of the Visual Studio data wizard, I suppose.

我想,Visual Studio数据向导的乐趣。

3 个解决方案

#1


2  

A TableAdapter is used to fill a DataTable. The result of your example query will still be a DataTable but with a single column in a single row. You can get the value using:

TableAdapter用于填充DataTable。示例查询的结果仍然是DataTable,但在单行中只有一列。您可以使用以下方式获取值:

var adapter = new SomeKindOfTableAdapter();
var dataTable = new DataTable();

adapter.Fill(dataTable);

string contactFirstName = (string)dataTable.Rows[0][0];

There are other ways to get your value without using a Table Adapter though:

除了使用表适配器之外,还有其他方法可以获得您的价值:

string query = @"SELECT [Contact First Name] 
                FROM ContactSkillSet 
                WHERE [Contact ID] = @Current";

string result;

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    using(SqlCommand command = new SqlCommand(query, conn))
    {
        result = (string)command.ExecuteScalar();
    }
}

#2


2  

If you plan to return a single result from your query you can always use the ExecuteScalar method on a SqlCommand class and then cast it to a string (see example in first link, it casts it to an Int32, but same principle for Strings).

如果您计划从查询中返回单个结果,则可以始终在SqlCommand类上使用ExecuteScalar方法,然后将其强制转换为字符串(请参阅第一个链接中的示例,它将其强制转换为Int32,但字符串的原则相同)。

#3


0  

A SQL statement that returns a single value is called a Scalar query. Normally Statements that use aggregate commands like COUNT, MAX, and MIN fall into this category. Other statements like your example are considered scalar as well.

返回单个值的SQL语句称为标量查询。通常,使用COUNT,MAX和MIN等聚合命令的语句属于此类别。像你的例子这样的其他陈述也被认为是标量。

Basically you can treat it like any other query. Check that there is at least one row, and if there is one, cast row zero column zero to the appropriate data type. If you are using Strongly Typed Data sets there are some features to make this easier, but it is not required to do so.

基本上你可以像对待任何其他查询一样对待它。检查是否至少有一行,如果有,则将行零列零转换为适当的数据类型。如果您使用强类型数据集,则有一些功能可以使这更容易,但不需要这样做。

#1


2  

A TableAdapter is used to fill a DataTable. The result of your example query will still be a DataTable but with a single column in a single row. You can get the value using:

TableAdapter用于填充DataTable。示例查询的结果仍然是DataTable,但在单行中只有一列。您可以使用以下方式获取值:

var adapter = new SomeKindOfTableAdapter();
var dataTable = new DataTable();

adapter.Fill(dataTable);

string contactFirstName = (string)dataTable.Rows[0][0];

There are other ways to get your value without using a Table Adapter though:

除了使用表适配器之外,还有其他方法可以获得您的价值:

string query = @"SELECT [Contact First Name] 
                FROM ContactSkillSet 
                WHERE [Contact ID] = @Current";

string result;

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    using(SqlCommand command = new SqlCommand(query, conn))
    {
        result = (string)command.ExecuteScalar();
    }
}

#2


2  

If you plan to return a single result from your query you can always use the ExecuteScalar method on a SqlCommand class and then cast it to a string (see example in first link, it casts it to an Int32, but same principle for Strings).

如果您计划从查询中返回单个结果,则可以始终在SqlCommand类上使用ExecuteScalar方法,然后将其强制转换为字符串(请参阅第一个链接中的示例,它将其强制转换为Int32,但字符串的原则相同)。

#3


0  

A SQL statement that returns a single value is called a Scalar query. Normally Statements that use aggregate commands like COUNT, MAX, and MIN fall into this category. Other statements like your example are considered scalar as well.

返回单个值的SQL语句称为标量查询。通常,使用COUNT,MAX和MIN等聚合命令的语句属于此类别。像你的例子这样的其他陈述也被认为是标量。

Basically you can treat it like any other query. Check that there is at least one row, and if there is one, cast row zero column zero to the appropriate data type. If you are using Strongly Typed Data sets there are some features to make this easier, but it is not required to do so.

基本上你可以像对待任何其他查询一样对待它。检查是否至少有一行,如果有,则将行零列零转换为适当的数据类型。如果您使用强类型数据集,则有一些功能可以使这更容易,但不需要这样做。