C# 应用程序中插入语句中 MySql ODBC 5.1 驱动程序的内存分配错误

发布于 2024-09-04 07:51:52 字数 1595 浏览 2 评论 0原文

我有一个用 C# 编写的 .NET Wndows 应用程序。这是一个使用 MySql 5.1 数据库社区版的简单 Windows 应用程序。我已经下载了 MySql ODBC 驱动程序,并在本地计算机上为我的数据库创建了一个 dsn。在我的应用程序上,我可以毫无问题地执行 get 类型查询,但是当我执行给定的插入语句(不是我尝试执行任何其他操作)时,我收到以下错误:

{"ERROR [HY001] [MySQL][ODBC 5.1 驱动程序][mysqld-5.0.27-community-nt]内存分配错误"}

我在 Windows XP 机器上运行。我的机器有 1 GB 内存。 有人有什么想法吗?请参阅下面的代码

OdbcConnection MyConn = DBConnection.getDBConnection();
    int result = -1;
    try
    {
        MyConn.Open();
        OdbcCommand myCmd = new OdbcCommand();
        myCmd.Connection = MyConn;
        myCmd.CommandType = CommandType.Text;
        OdbcParameter userName = new OdbcParameter("@UserName", u.UserName);
        OdbcParameter password = new OdbcParameter("@Password", u.Password);
        OdbcParameter firstName = new OdbcParameter("@FirstName", u.FirstName);
        OdbcParameter LastName = new OdbcParameter("@LastName", u.LastName);
        OdbcParameter sex = new OdbcParameter("@sex", u.Sex);
        myCmd.Parameters.Add(userName);
        myCmd.Parameters.Add(password);
        myCmd.Parameters.Add(firstName);
        myCmd.Parameters.Add(LastName);
        myCmd.Parameters.Add(sex);
        myCmd.CommandText = mySqlQueries.insertChatUser;
        result = myCmd.ExecuteNonQuery();
    }
    catch (Exception e)
    {
             //{"ERROR [HY001] [MySQL][ODBC 5.1 Driver][mysqld-5.0.27-community-nt]Memory  
             // allocation error"} EXCEPTION ALWAYS THROWN HERE 
    }
    finally
    {
        try
        {
            if (MyConn != null) MyConn.Close();
        }
        finally { }
    }

I have a .NET Wndows application in C#. It's a simple Windows application that is using the MySql 5.1 database community edition. I've downloaded the MySql ODBC driver and have created a dsn to my database on my local machine. On my application, I can perform get type queries without problems, but when I execute a given insert statement (not that I've tried doing any others), I get the following error:

{"ERROR [HY001] [MySQL][ODBC 5.1 Driver][mysqld-5.0.27-community-nt]Memory allocation error"}

I'm running on a Windows XP machine. My machine has 1 GB of memory.
Anyone have any ideas? See code below

OdbcConnection MyConn = DBConnection.getDBConnection();
    int result = -1;
    try
    {
        MyConn.Open();
        OdbcCommand myCmd = new OdbcCommand();
        myCmd.Connection = MyConn;
        myCmd.CommandType = CommandType.Text;
        OdbcParameter userName = new OdbcParameter("@UserName", u.UserName);
        OdbcParameter password = new OdbcParameter("@Password", u.Password);
        OdbcParameter firstName = new OdbcParameter("@FirstName", u.FirstName);
        OdbcParameter LastName = new OdbcParameter("@LastName", u.LastName);
        OdbcParameter sex = new OdbcParameter("@sex", u.Sex);
        myCmd.Parameters.Add(userName);
        myCmd.Parameters.Add(password);
        myCmd.Parameters.Add(firstName);
        myCmd.Parameters.Add(LastName);
        myCmd.Parameters.Add(sex);
        myCmd.CommandText = mySqlQueries.insertChatUser;
        result = myCmd.ExecuteNonQuery();
    }
    catch (Exception e)
    {
             //{"ERROR [HY001] [MySQL][ODBC 5.1 Driver][mysqld-5.0.27-community-nt]Memory  
             // allocation error"} EXCEPTION ALWAYS THROWN HERE 
    }
    finally
    {
        try
        {
            if (MyConn != null) MyConn.Close();
        }
        finally { }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦亿 2024-09-11 07:51:52

这是因为某些字段接受 null,所以我将它们作为 null 传递,而它们应该作为 DBNull.Value 传递。对于所有允许 null 的字段,应检查是否为 null,如果发现为 null,则应传递 DBNull.Value。

It was because some fields accept null, I had passed them as null where they should be passed as DBNull.Value. For all the fields which allow null should be checked for null and if found null, DBNull.Value should be passed.

请止步禁区 2024-09-11 07:51:52

为了完整起见,Chinjoo 的 SQL 语句可能是这样的:

mySqlQueries.insertChatUser = "insert into ChatUsers (UserName, Password, FirstName, LastName, sex) values (?,?,?,?,?);";

这称为参数化插入,其中每个问号代表他的参数之一。在这个简单的示例中,代码中参数集合中参数的顺序必须与 SQL 语句中列名的顺序匹配。

虽然不如使用函数那么优雅,但他的 null 问题的修复对于他的参数之一来说看起来像这样:

OdbcParameter LastName = new OdbcParameter("@LastName", u.LastName);

被替换为

// if the value is "null" return DBNull, else just the value
OdbcParameter LastName = new OdbcParameter("@LastName", 
(u.LastName == null) ? System.DBNull.Value : (object)u.LastName);

至少在我的代码中(略有不同)需要内部强制转换为类型对象,否则编译器不确定 ?: 运算符应返回什么类型。

希望这对那些对参数化等相对较新的人有所帮助。

完全没有暗示对 Chinjoo 的批评——他的帖子帮助了我!只是想我会为经验不足的人分享。我绝不是专家,所以对我所说的一切持保留态度。

Just for the sake of completeness, Chinjoo's SQL statement would likely be something like this:

mySqlQueries.insertChatUser = "insert into ChatUsers (UserName, Password, FirstName, LastName, sex) values (?,?,?,?,?);";

This is known as a parameterized insert where each question mark represents one of his parameters. In this simple example the order of the parameters in the parameter collection in code must match the order of the column names in the SQL statement.

While less elegant than using a function, the fix for his null problem would look something like this for one of his parameters:

OdbcParameter LastName = new OdbcParameter("@LastName", u.LastName);

is replaced with

// if the value is "null" return DBNull, else just the value
OdbcParameter LastName = new OdbcParameter("@LastName", 
(u.LastName == null) ? System.DBNull.Value : (object)u.LastName);

At least in my code (which is slightly different) the inner cast to type object is required since otherwise the compiler isn't sure what type the ?: operator should return.

Hope this helps anyone who is relatively new to parameterization, etc.

No criticism of Chinjoo implied at all--his posting helped me out! Just thought I'd share for the less-experienced. I'm by no means expert so take everything I say with a grain of salt.

花开半夏魅人心 2024-09-11 07:51:52

如果您尝试在 VARCHAR 字段中插入无效字符,也可能引发此异常。例如,如果字符串是由 UNIX 机器生成的并且具有行尾字符“\n”。您可以将有问题的字符替换为 Windows 样式,反之亦然,或者如果您不想存储行尾,则将其删除。

您可以检查字符串,如果其中任何一个有行尾字符,请尝试重复插入删除它们。如果有效,问题就出在这些字符上。

This exception also can be raised if you try to insert invalid chars in a VARCHAR field. In example, if the string is generated by a UNIX machine and has end of line characters '\n'. You can just replace that problematic characters to Windows style, or viceversa, or just delete it if you don't want to store end of lines.

You can check the strings, and if any of them has end of line characters, try to repeat the insert deleting them. If it works, the problem are these characters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文