如何检查输入的值和字段的数据类型正确. [英] How to check values entered and datatypes of the fields are correct.

查看:112
本文介绍了如何检查输入的值和字段的数据类型正确.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要编写一个c#程序,以便使用相应的列输入数据库的数据具有与数据库中相同的数据类型.

I need to write a c# program such that the data entered into the database with respective column has the same datatypes as of in the database.

如果任何字段数据不匹配,也要为该特定字段写日志并记录不匹配.

also if any of the field data is mismatch then want to write the log for that specific field and record mismatch.

任何帮助都将是可观的.

any of the help will be appreciable.

推荐答案

您好EmpAnsar,

Hi EmpAnsar,

这是有关检查数据库中SQL列类型的C#代码.

Here is C# code about checking the type of a SQL column into database.

SqlDataReader read = command.ExecuteReader();
    while (read.Read())
    {
        for (int i = 0; i < read.FieldCount; i++)
        {
            Type dataType = read.GetFieldType(i);
            if (dataType == typeof(int))
            {
                // Do for integers (INT, SMALLINT, BIGINT)
            }
            else if (dataType == typeof(double))
            {
                // Do for doubles (DOUBLE)
            }
            else if (dataType == typeof(string))
            {
                // Do for Strings (VARCHAR, NVARCHAR)
            }
            else if (dataType == typeof(DateTime))
            {
                // Do for DateTime (DATETIME)
            }
            else if (dataType == typeof(byte[]))
            {
                // Do for Binary (BINARY, VARBINARY, NVARBINARY, IMAGE)
            }
        }
    }

从上面的代码中,我们可以获得SQl数据表的数据类型.然后尝试将其与您输入的值的数据类型进行比较.

From above code, we can get datatypes of SQl datatable. Then try to compare it with your value entered datatype.

希望有帮助!

祝你有美好的一天!

克里斯汀


这篇关于如何检查输入的值和字段的数据类型正确.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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