C# SqlDataAdapter.Fill 在传递数据表参数时给出有关数据转换的错误

发布于 2024-09-14 21:06:29 字数 1018 浏览 10 评论 0原文

我正在尝试使用 C# 中的 SqlDataAdapter 填充数据表。我对这些对象不太熟悉,基本上正在研究其他人的代码模板,试图弄清楚它是如何工作的。这是基本形式。

        SqlCommand command = new SqlCommand(@"SELECT * FROM tblEmployees WHERE Name = " + firstSSN,connection);

        SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText.ToString(), connection.ConnectionString.ToString());

        SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        adapter.Fill(table);

这对他们的形式来说效果很好。我尝试对我的进行同样的操作,但在尝试将 nvarchar 转换为数据类型 int 的列时出现错误。我查看了 MSDN 并尝试了以下操作:1)使用 tblEmployees 中的适当名称/类型/主键将列添加到 DataTable 中。 2)将 TableMapping 添加到 DataAdapter,尽管我不能 100% 确定这部分的语法正确。我愿意:

adapter.TableMappings.Add("work", "dbo.tblEmployees");

为此。 DataTable 被命名为“work”,但我不确定语法是否正确,我输入了 SQL Server Management Studio 中显示的表名称,但不知道如何测试它是否正确链接。

感谢您提供的任何帮助。我一直在努力解决这个问题,以至于我即将以完全不同的方式实现我的目标,并抛弃我所拥有的。

I'm trying to fill a datatable using a SqlDataAdapter in C#. I'm not very familiar with the objects, and am basically working off a template of someone else's code to try to figure out how it works. Here's the basic form.

        SqlCommand command = new SqlCommand(@"SELECT * FROM tblEmployees WHERE Name = " + firstSSN,connection);

        SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText.ToString(), connection.ConnectionString.ToString());

        SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
        DataTable table = new DataTable();
        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
        adapter.Fill(table);

And that works fine on their form. I tried doing the same with mine, but got an error about trying to convert a nvarchar to a column of data type int. I looked through MSDN and tried the following: 1) Adding the columns to the DataTable with the appropriate names/types/primary keys from tblEmployees. 2) Adding the TableMapping to the DataAdapter, though I'm not 100% sure I have the syntax on this part right. I do:

adapter.TableMappings.Add("work", "dbo.tblEmployees");

for that. The DataTable is named "work", but I'm unsure if I have the syntax right, I put in the table name as it appears in SQL Server Management Studio but don't know how I test if it's linking up correctly.

Thanks for any help you can provide. I've been beating my head on this to the point that I'm on the verge of approaching my goal an entirely different way and throwing away what I have.

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

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

发布评论

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

评论(1

◇流星雨 2024-09-21 21:06:29

乍一看,当查看代码的第一行时,请查看 firstSSN 的位置:

@"Select * From tblEmployees WHERE Name = " + firstSSN

我会想象“Name”是错误消息引用的 nvarchar 字段,并且如果 firstSSN 是整数仅值,请尝试在firstSSN周围放置''标记,如下所示:

@"Select * From tblEmployees WHERE Name = '" + firstSSN + "'"

否则,请确保字段“Name”的类型与您传递到命令中的参数类型匹配。这很可能只是一个数据类型问题。

我给你的一个建议 - 前几天我刚刚开始尝试 LINQ,发现它非常方便用于提取、操作数据并将其过滤到易于使用的记录集中。有各种有关如何使用 LINQ 执行查询的文档,因此,如果您有时间,请看一下。希望这有帮助。

At first glance, when looking at the first line of your code, look where firstSSN is:

@"Select * From tblEmployees WHERE Name = " + firstSSN

I would imagine that 'Name' is the nvarchar field that the error message is referencing, and if firstSSN is an integer value only, try putting '' marks around firstSSN like this:

@"Select * From tblEmployees WHERE Name = '" + firstSSN + "'"

Otherwise, make sure that the type of the field "Name" matches the type of the parameter you are passing into your command. This is most likely just a data type issue.

One recommendation I have for you - I just started experimenting with LINQ the other day, and find it pretty handy to use for pulling out, manipulating, and filtering data into an easy to use record set. There's all kinds of documentation available on how to execute queries with LINQ, so have a look if you have a few minutes. Hope this helps.

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