算术溢出 - Int32 MySQL到C#

时间:2015-05-26 17:05:07

标签: c# mysql unix overflow type-conversion

架构:

- 运行Windows 2008的Web服务器,其中IIS 7托管在.NET Framwork 4.0中开发的.NET Web应用程序。在同一台服务器上我有驱动程序MySQL ODBC 3.5.1驱动程序

- 在Unix上运行的MySQL数据库服务器

问题:

    public static List<Entity.Document> list(int companyId, int folderId, int userId)
    {
        //Initialise the results
        List<Entity.Document> results = new List<Entity.Document>();

        try
        {
            using (OdbcConnection connection = new OdbcConnection(ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString))
            {
                connection.Open();
                using (OdbcCommand command = new OdbcCommand(string.Concat("SELECT id, name, parentfolderid, gid, companyid, is_deleted FROM tbl_document where parentfolderid = '" + folderId + "' and companyid = '", companyId, "' ORDER BY name"), connection))
                using (OdbcDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        Entity.Document item = new Entity.Document();

                        item.Id = int.Parse(dr["id"].ToString());
                        item.Name = dr["name"].ToString();
                        item.gId = Guid.Parse(dr["gid"].ToString());
                        if (string.IsNullOrEmpty(dr["parentfolderid"].ToString()))
                            item.FolderId = 0;
                        else
                            item.FolderId = int.Parse(dr["parentfolderid"].ToString());

                        if (dr["is_deleted"].ToString() == "1")
                            item.IsDeleted = true;
                        else
                            item.IsDeleted = false;

                        results.Add(item);
                    }
                    dr.Close();
                }
                connection.Close();
            }
        }

遇到代码行 item.Id = int.Parse(dr [“id”]。ToString()); 时,我遇到以下错误:

5/26/2015 (9:29 AM) - Method: System.Collections.Generic.List`1[Entity.Folder] listFolders(Int32, Int32, Int32)
System.OverflowException: Arithmetic operation resulted in an overflow.
   at System.Data.Odbc.OdbcDataReader.GetSqlType(Int32 i)
   at System.Data.Odbc.OdbcDataReader.GetValue(Int32 i)
   at DA.Folder.listFolders(Int32 parentFolderID, Int32 companyId, Int32 userId) in D:\Websites\AMS\AMS1.2\DA\Folder.cs:line 39

请注意,我还检查了从dr [“id”]返回的值的类型,如下所示: 名称:Int32

FullName:System.Int32

我也尝试过System.Convert.ToInt32

您可以在下面找到目标表的说明:

mysql> describe tbl_folder;


+----------------+------------+------+-----+---------+----------------+
| Field          | Type       | Null | Key | Default | Extra          |
+----------------+------------+------+-----+---------+----------------+
| id             | int(11)    | NO   | PRI | NULL    | auto_increment |
| name           | longtext   | NO   |     | NULL    |                |
| parentfolderid | int(11)    | YES  |     | NULL    |                |
| companyid      | int(11)    | NO   |     | NULL    |                |
| is_deleted     | tinyint(4) | NO   |     | NULL    |                |
+----------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql>

您将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我设法解决它。在64位架构上部署在32位操作系统上开发的应用程序是个问题。我在64位环境中编译相同的源代码并进行部署。它现在有效!无论如何,谢谢你的帮助:)