如何以编程方式确定DataColumn的类型?

编程入门 行业动态 更新时间:2024-10-26 22:23:37
本文介绍了如何以编程方式确定DataColumn的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好! 感谢您阅读我的文章. 我有以下问题: 我的程序包括一个DataGridView,它显示了用户提供的select语句中的数据.我使用OdbcDataAdapter填充数据表,然后将其分配为DataGridView的数据源. 如果用户提供了返回二进制数据的SQL select语句,则当DataGridView尝试显示数据时,我将收到DataError异常. 如何确定数据表中的给定列是否包含二进制数据?我尝试使用DataColumn.DataType,但是无法识别二进制数据(类型为Byte [])(或者我不知道要比较什么). 如果DataGridView在二进制列中显示了二进制数据的字符串表示形式,或仅在列中显示了二进制数据"的消息,那将起作用. 谢谢, Jess

Howdy! Thanks for reading my post. I have the following problem: My program includes a DataGridView, which shows data from select statements supplied by the user. I use an OdbcDataAdapter to fill a DataTable, which is then assigned as the DataGridView''s DataSource. If the user supplies an SQL select statement which returns binary data, I get a DataError exception when the DataGridView tries to display the data. How do I determine if a given column in the datatable includes binary data? I''ve tried using DataColumn.DataType, but binary data (which is of type Byte[]) isn''t recognized (or I don''t know what to compare to). If the DataGridView showed a string representation of the binary data, or just a message stating ''binary data'' in the column, that would work. Thanks, Jess

推荐答案

我能够使用 I was able to use if (0 == String.Compare(dc.DataType.ToString().Trim(), "System.Byte[]"))

确定数据类型.我将它的表列索引存储在列表中,并且只是删除了该列. 谢谢, 杰西

to determine the datatype. I stored it''s table column index in a list<int>, and just deleted that column. Thanks, Jess

List<int> theseAreBinaryData = new List<int>(); foreach (DataColumn dc in dt.Columns) { if (0 == String.Compare(dc.DataType.ToString().Trim(), "System.Byte[]")) { theseAreBinaryData.Add(dt.Columns.IndexOf(dc.ColumnName)); } } if (theseAreBinaryData.Count > 0) { foreach (int i in theseAreBinaryData) { dt.Columns.RemoveAt(i); } } dataGridView.DataSource = dt;

如果对进行 ToString() DataType ,它应该告诉您使用了哪种数据类型,您可以从那里访问. If you do a ToString() on the DataType, it should tell you what data type is used and you can go from there.

更多推荐

如何以编程方式确定DataColumn的类型?

本文发布于:2023-11-03 16:46:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   方式   DataColumn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!