数据集中的字段如何更改我的数据类型; [英] Field from the dataset how can I change my data type;

查看:133
本文介绍了数据集中的字段如何更改我的数据类型;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以在我的桌子上返回
数据集中的字段如何更改我的数据类型;
ds.Columns("dataletter").DataType = GetType(string);

I have a function that you return on my table
Field from the dataset how can I change my data type;
ds.Columns("dataletter").DataType = GetType(string);

public DataTable letterrecive(string tblname, string Filedname, string Where, string Order)
        {
            if (Where.Trim() == "")
               Where = " 1=1 ";
            command.CommandText = "Select " + Filedname + " From " + tblname + " where " + Where + Order;
            SqlDataAdapter search1 = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            search1.SelectCommand = command;
            command.Connection = connection;
            search1.Fill(ds, "dt");
            
             return ds.Tables[0];
        }



[edit]添加了前置标记[/edit]



[edit]Added pre tags [/edit]

推荐答案

在数据绑定之前添加具有正确类型的列.在数据绑定事件中,将您的数据转换为正确的类型并绑定到该列.如果在绑定数据后更改列数据类型,则会发生错误.
Add the column with the right type before data binding. At data binding event convert your data to the correct type and bind to that column. Error will be occurring if you change column data type after data bound.


由于在获取数据之前未将列添加到数据表中,因此列中的数据类型数据表中的数据将是数据库中对应的数据类型.

似乎您正在尝试使用此方法处理多种不同类型的查询.我实际上建议的是,与其尝试建立使用串联字符串的通用"获取机制,不如建立专用" SqlCommand实例并使用SqlParameters而不是文字条件.如果愿意,您可以具有一个辅助函数来执行命令并填充数据表.

如果由于某种原因需要在某些列中包含字符串,则还可以使用例如^ ]
Since you don''t add the columns into the data table before you do the fetch, the data type in your column in the data table will be the corresponding datatype from what''s in the database.

Seems that you''re trying to handle multiple different kind of queries with this method. What I actually would suggest is that instead of trying to build a ''common'' fetch mechanism where you use concatenated strings, build the ''specialized'' SqlCommand instances and use SqlParameters instead of literal conditions. If you like you can have a helper function to execute the commands and to fill a datatable.

If you, for some reason, need to have a string in some columns, you can also handle the conversion in the database using for example cast[^]


这篇关于数据集中的字段如何更改我的数据类型;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆