C# 表适配器实际上返回什么? [英] What do C# Table Adapters actually return?
问题描述
堆栈溢出!
我正在开发一个在 Windows 窗体应用程序中操作 SQL 表的应用程序.到目前为止,我只使用了预先生成的 Fill 查询,以及自制的更新和删除查询(不返回任何内容).
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).
我对存储单个列中单个值的值感兴趣('nchar(15)' 名称),虽然我很容易编写了 SQL 代码将该值返回给应用程序,但我不知道它将返回什么as.
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.
SELECT [Contact First Name] FROM ContactSkillSet
WHERE [Contact ID] = @CurrentID
结果可以直接存储为字符串吗?我需要投它吗?调用 toString 方法?
Can the result be stored directly as a string? Do I need to cast it? Invoke a toString method?
谢谢!
有趣的是,以下工作:
String firstName = this.contactSkillSetTableAdapter.GrabFirstName(userKey);
Visual Studio 数据向导的乐趣,我想.
The joys of the Visual Studio data wizard, I suppose.
推荐答案
TableAdapter 用于填充 DataTable.您的示例查询的结果仍将是一个 DataTable,但在一行中有一个列.您可以使用以下方法获取值:
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:
var adapter = new SomeKindOfTableAdapter();
var dataTable = new DataTable();
adapter.Fill(dataTable);
string contactFirstName = (string)dataTable.Rows[0][0];
还有其他方法可以在不使用 Table Adapter 的情况下获得您的价值:
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();
}
}
这篇关于C# 表适配器实际上返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!