2013-06-20 36 views
-5

不执行请求:c#。 MySQL的。 “插”。错误

  mycmd = new MySqlCommand(); 
      mycmd.CommandText = "INSERT INTRO 'users' (userid, username, password, ipaddr, port, channel, inbox, inboxadm, access, other) VALUES (NULL, 'username2', 'password2', NULL, NULL, 'channel2', NULL, NULL, 'access2', NULL);"; 
      mycmd.Connection = mconnection; 
      mycmd.Parameters.AddWithValue("username2", message.Login); 
      mycmd.Parameters.AddWithValue("password2", message.Password); 
      mycmd.Parameters.AddWithValue("channel2", "channel"); 
      mycmd.Parameters.AddWithValue("access2", 0); 
      mycmd.CommandType = CommandType.Text; 

      mycmd.ExecuteNonQuery(); 

帮助找到并要求纠正错误。

Exapmle phpmyadmin的SQL查询:

INSERT INTO `shachat`.`users` (`userid`, `username`, `password`, `ipaddr`, `port`, `channel`, `inbox`, `inboxadm`, `access`, `other`) VALUES (NULL, 'myacc', 'mypass', NULL, NULL, 'administratorY', NULL, NULL, '9', NULL); 
+0

请指定您收到的错误消息 –

+0

可能有助于指定您的错误,为我们定义架构并添加任何其他相关详细信息。 –

+0

错误不存在,在“mycmd.ExecuteNonQuery();”之后不会出现。 – BrainOverflow

回答

0

马上蝙蝠,我相信

INSERT INTRO 'users' 

应该

INSERT INTO users 
+0

谢谢!它解决了一个问题! – BrainOverflow

4

的问题是INSERT 'INTRO' 的关键词INSERT INTO

2

除了INSERT INTRO问题之外,您还需要在参数前添加“at”符号。另外,字符值周围不需要单引号;他们会在这里导致一个错误。

表名周围的单引号也是一个问题;不要将它们与反引号混淆。在这样的情况下,表名不是保留字,我宁愿去没有反引号。

mycmd = new MySqlCommand(); 
mycmd.CommandText = 
    "INSERT INTO users (userid, username, password, ipaddr, port, channel, inbox, inboxadm, access, other) " + 
    "VALUES (NULL, @username2, @password2, NULL, NULL, @channel2, NULL, NULL, @access2, NULL);"; 
mycmd.Connection = mconnection; 
mycmd.Parameters.AddWithValue("@username2", message.Login); 
mycmd.Parameters.AddWithValue("@password2", message.Password); 
mycmd.Parameters.AddWithValue("@channel2", "channel"); 
mycmd.Parameters.AddWithValue("@access2", 0); 
mycmd.CommandType = CommandType.Text; 

mycmd.ExecuteNonQuery(); 
+0

和表名.... – Steve

+0

谢谢@Steve - 好赶上!我习惯于在MySQL查询中看到不必要的单引号和反引号,我甚至不会再看到它们:)我会更新我的答案。 –