C# Winforms ADO.NET - DataGridView INSERT 从空数据开始
我有一个连接到 SQL Server 2005 数据库的 C# Winforms 应用程序。我当前正在处理的表单允许用户将大量不同类型的数据输入到各种文本框、组合框和 DataGridView 中以插入到数据库中。它全部代表一种特定类型的机器,数据分布在大约九个表中。
我遇到的问题是我的 DataGridView 代表一种可能会也可能不会添加到数据库中的数据类型。因此,当创建 DataGridView 时,它是空的并且没有数据绑定,因此无法输入数据。我的问题是,我应该创建一个带有硬编码字段名称的表来表示数据在数据库中的显示方式,还是有一种方法可以简单地让列名称不填充任何数据,以便用户可以输入它喜欢?我不喜欢对它们进行硬编码的想法,以防数据库模式发生变化,但我不确定如何处理这个问题。
I have a C# Winforms app that is connecting to a SQL Server 2005 database. The form I am currently working on allows a user to enter a large amount of different types of data into various textboxes, comboboxes, and a DataGridView to insert into the database. It all represents one particular type of machine, and the data is spread out over about nine tables.
The problem I have is that my DataGridView represents a type of data that may or may not be added to the database. Thus, when the DataGridView is created, it is empty and not databound, and so data cannot be entered. My question is, should I create the table with hard-coded field names representing the way that the data looks in the database, or is there a way to simply have the column names populate with no data so that the user can enter it if they like? I don't like the idea of hard-coding them in case there is a change in the database schema, but I'm not sure how else to deal with this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在数据库中查询列的名称,如果您使用的是
SqlDataReader
,则可以使用它的GetSchemaTable
。以下是执行此操作的一些示例代码:http://support.microsoft.com/kb/310107
You could query the database for the names of the columns, if you're using a
SqlDataReader
you can use it'sGetSchemaTable
. Here's some sample code for doing this:http://support.microsoft.com/kb/310107