使用C#从Access数据库中提取整数

时间:2016-06-16 17:18:42

标签: c# ms-access-2010

我正在尝试从2010 Access数据库中提取数据。这应该是拉动供应商编号。

虽然我收到“无效的CastException未被用户代码处理。指定的强制转换无效。”当我尝试构建项目时。我已经在我的数据库中检查了我的列VNNO并将其设置为整数。我必须遗漏一些简单的东西。

    public SelectList GetVendors(String selected = "")
    {
        IList<SelectListItem> vendors = new List<SelectListItem>();
        vendors.Add(new SelectListItem { Text = "", Value = "- Select Vendor -" });

        vendors.Add(new SelectListItem { Text = "TO BE ASSIGNED", Value = "TO BE ASSIGNED" });

        using (OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["VendorConnection"].ConnectionString))
        {
            myConnection.Open();

            using (OleDbCommand command = new OleDbCommand())
            {
                command.CommandText = "SELECT * FROM [MY PPUR301] ORDER BY [VNNO] ASC";
                command.Connection = myConnection;
                command.Prepare();

                using (OleDbDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string text = "";
                        string value = "";

                        if (!reader.IsDBNull(reader.GetOrdinal("VNNO")))
                        {
                            text = reader.GetInt32(reader.GetOrdinal("VNNO")).ToString();
                        }

                        if (!reader.IsDBNull(reader.GetOrdinal("VNNO")))
                        {
                            value = reader.GetInt32(reader.GetOrdinal("VNNO")).ToString();
                        }

                        vendors.Add(new SelectListItem { Text = text.ToUpper(), Value = value.ToUpper() });
                    }
                }
            }
        }

        return new SelectList(vendors, "Text", "Value", selected);
    }

这是另一段完美无瑕的代码。它提取供应商的实际名称。

    public SelectList GetVendors(String selected = "")
    {
        IList<SelectListItem> vendors = new List<SelectListItem>();
        vendors.Add(new SelectListItem { Text = "", Value = "- Select Vendor -" });

        vendors.Add(new SelectListItem { Text = "TO BE ASSIGNED", Value = "TO BE ASSIGNED" });

        using (OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["VendorConnection"].ConnectionString))
        {
            myConnection.Open();

            using (OleDbCommand command = new OleDbCommand())
            {
                command.CommandText = "SELECT * FROM [MY PPUR301] ORDER BY [VNNAME] ASC";
                command.Connection = myConnection;
                command.Prepare();

                using (OleDbDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        string text = "";
                        string value = "";

                        if (!reader.IsDBNull(reader.GetOrdinal("VNNAME")))
                        {
                            text = reader.GetString(reader.GetOrdinal("VNNAME")).ToString().Trim();
                        }

                        if (!reader.IsDBNull(reader.GetOrdinal("VNNAME")))
                        {
                            value = reader.GetString(reader.GetOrdinal("VNNAME")).ToString().Trim();
                        }

                        vendors.Add(new SelectListItem { Text = text.ToUpper(), Value = value.ToUpper() });
                    }
                }
            }
        }

        return new SelectList(vendors, "Text", "Value", selected);
    }

2 个答案:

答案 0 :(得分:1)

这就是我编写代码的方式:

public static Tortuga.Chain.AccessDataSource DataSource = Tortuga.Chain.AccessDataSource.CreateFromConfig("VendorConnection");

public class Foo
{
    public int? VNNO { get; set; }
    public string VnnoString
    {
        get
        {
            if (VNNO.HasValue)
                return VNNO.ToString();
            else
                return "";
        }
    }
    public string VNNAME { get; set; }
}



var items = dataSource.From("MY PPUR301").WithSorting("VNNO").ToCollection<Foo>().Execute();

foreach (var item in items)
{
    vendors.Add(new SelectListItem { Text = item.VNNAME.ToUpper(), Value = item.VnnoString });
}

答案 1 :(得分:1)

感谢@JonathanAllen的帮助!这绝对是数据库方面的事情。我有一个宏从另一个系统拉出并将其转储到我正在使用的Access数据库中。它最初是转储十进制格式的数字。我进去并将列更新为int,但是,它没有工作。

因此,我只是将我的GetInt改为GetDecimal,并且为了程序的目的,这不会伤害到一件事。从长远来看,这将节省时间,因此在提取原始数据时我不必创建另一个宏。

感谢大家的帮助!