MySQL的ExecuteNonQuery()出现异常 “未处理 System.ArgumentOutOfRangeException”
Posted in misc on 03/13/2010 12:30 上午 by tunpishuang错误内容如下:
未处理 System.ArgumentOutOfRangeException Message="索引和长度必须引用该字符串内的位置。\r\n参数名: length" Source="mscorlib" ParamName="length" StackTrace: 在 System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) 在 MySql.Data.MySqlClient.MySqlTokenizer.NextParameter() 在 MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql, MySqlParameterCollection parameters, MySqlPacket packet) 在 MySql.Data.MySqlClient.Statement.BindParameters() 在 MySql.Data.MySqlClient.PreparableStatement.Execute() 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 在 MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() 在 DiguBackup.CDigu.BackupAsDb() 位置 d:\projects\DiguBackup\DiguBackup\Form1.cs:行号 245 在 DiguBackup.Form1.btnBackup_Click(Object sender, EventArgs e) 位置 d:\projects\DiguBackup\DiguBackup\Form1.cs:行号 76 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm) 在 DiguBackup.Program.Main() 位置 d:\projects\DiguBackup\DiguBackup\Program.cs:行号 18 在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:
google到了此文 “用mysql相当多问题。不是说开源不好,可是拜托,争争气吧” 的第五条,发现情况和这个几乎一样,看作者的语气对开源的东东都不怎么看好,并且说是源代码产生的问题,无法调试。我就郁闷死了。难不成要换种数据库?
这个是mysql官方09年的一个bug讨论贴 http://bugs.mysql.com/bug.php?id=44960 , 我开始怀疑是sql语句有需要转义的字符,于是查看sql语句:
insert into status(created_at,text,picPath,source,in_reply_to_status_id,in_reply_to_user_id,in_reply_to_user_name,in_reply_to_screen_name,favorited) values ('Mon Nov 16 21:02:03 +0800 2009','被杨教授电击的后果完全可以详见《飞越疯人院》(《One Flew Over the Cuckoo's Nest》)','','网站',0,0,'','','false')
这样可能看不清楚,拷贝到navicat和sqlserver management studio里面看看:
Cuckoo后面的单引号没有被转义,所以肯定要报错。
mysql中字符转义的方法是在前面加上反斜杠 \ 字符。
可以将字符里面的 ‘ 替换为 \’ ,直接用c#里面的replace(oldstring,newstring)替换。
我以为是replace(“‘”,”\’”) ,其实是replace(“‘”,@”\’”)。@是用来避免转义,有啥字符输出啥字符的。





