你好,我是使用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异常是未处理
算术运算导致溢出”
我不使用任何算术运算在这里。任何帮助?
你能张贴堆栈跟踪或异常的其他细节?您会发现在您要调用的某个库中发生异常。 –
还,你的代码目前暴露[SQL注入(http://en.wikipedia.org/wiki/SQL_injection) – Oren
关的话题,你可能想看看[using语句(C#)]( http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx) –