2013-01-16 58 views
0

你好,我是使用C#来构建应用程序连接到远程MySQL服务器。算术运算导致溢出错误C#

下面是代码:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 

namespace login 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (tryLogin(textBox1.Text, textBox2.Text) == true) 
      { 
       MessageBox.Show("Authed!"); 
      } 
      else 
      { 
       MessageBox.Show("Auth Failure."); 

      } 

     } 
     public bool tryLogin(string username, string password) 
     { 
MySqlConnection con = new MySqlConnection("host=myhostname;user=myusername;password=mypassword;database=mydatabase;"); 
MySqlCommand cmd = new MySqlCommand("SELECT * FROM test WHERE username = '" + username + "' AND password = '" + password + "';"); 
      cmd.Connection = con; 
      con.Open(); 
      MySqlDataReader reader = cmd.ExecuteReader(); 
      if (reader.Read() != false) 
      { 
       if (reader.IsDBNull(0) == true) 
       { 
        cmd.Connection.Close(); 
        reader.Dispose(); 
        cmd.Dispose(); 
        return false; 
       } 
       else 
       { 
        cmd.Connection.Close(); 
        reader.Dispose(); 
        cmd.Dispose(); 
        return true; 

       } 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 
} 

它显示了以下错误:

“OverflowException异常是未处理

算术运算导致溢出”

我不使用任何算术运算在这里。任何帮助?

+3

你能张贴堆栈跟踪或异常的其他细节?您会发现在您要调用的某个库中发生异常。 –

+3

还,你的代码目前暴露[SQL注入(http://en.wikipedia.org/wiki/SQL_injection) – Oren

+3

关的话题,你可能想看看[using语句(C#)]( http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx) –

回答

0

检查testmysql server

确保所有字段在usernamepassword列中都有有效值。

UPDATE

尝试包括选项"Use Pipe=false;",您的连接string.Then它会打开连接就好了。

我希望这会帮助你。

+0

是的,先生他们都有有效的价值。 – Ajay

+0

@Ajay你可以把你的'堆栈跟踪'在你的文章? – Sampath

+0

什么是堆栈跟踪,我的意思是我可以在哪里找到它? – Ajay

相关问题