MySqlCommand.ExecuteNonQuery 失败

发布于 2024-12-12 22:12:12 字数 476 浏览 0 评论 0原文

我试图在 mysql 数据库中执行 INSERT 查询,但除了代码执行停止并且没有插入任何内容之外,什么也没有发生。 这是代码(连接是在另一点建立的并且正在工作):

            query = string.Format("INSERT INTO users (username, settings) VALUES('{0}', '{1}')", userName, sw.ToString());
            myCommand = new MySqlCommand();
            myCommand.CommandText = query;
            myCommand.Connection = con;
            myCommand.ExecuteNonQuery();

如果我单步执行代码,它会在executenonquery之后停止,所以显然那里出了问题。天哪,我讨厌它不会向我抛出错误:(

I am trying to execute an INSERT query in a mysql DB, but it doesn't happen anything except that the code executions stops and nothing gets inserted.
Here is the code (the connection is made at another point and is working):

            query = string.Format("INSERT INTO users (username, settings) VALUES('{0}', '{1}')", userName, sw.ToString());
            myCommand = new MySqlCommand();
            myCommand.CommandText = query;
            myCommand.Connection = con;
            myCommand.ExecuteNonQuery();

If I step the code, it stops after executenonquery, so obvisously something is wrong there. God I hate that it doesn't throw an error at me :(

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

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

发布评论

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

评论(1

琉璃繁缕 2024-12-19 22:12:12

您是否检查过连接实际上已打开,并尝试通过将结果分配给 try catch 块中的 int 来执行该方法。

int result;

try 
{
    if (conn.State != ConnectionState.Open)
                    conn.Open();

    result = Convert.ToInt32(dbComm.ExecuteNonQuery());
 }
 catch (Exception ex)
 {
    logger.Error(ex);
 }
 finally
 {
     if (conn.State != ConnectionState.Closed)
                    conn.Close();
 }

Have you checked the connection is actually open and tried executing the method by assigning the result to an int in a try catch block.

int result;

try 
{
    if (conn.State != ConnectionState.Open)
                    conn.Open();

    result = Convert.ToInt32(dbComm.ExecuteNonQuery());
 }
 catch (Exception ex)
 {
    logger.Error(ex);
 }
 finally
 {
     if (conn.State != ConnectionState.Closed)
                    conn.Close();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文