C#登陆失败,未处理FormatException,输入字符串的格式不正确。
错误代码intstate=Convert.ToInt32(cmd.ExecuteScalar());登陆代码:stringstrConnection="Provider=...
错误代码 int state = Convert.ToInt32(cmd.ExecuteScalar());
登陆代码:
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=C:\Users\thtf\Desktop\新建文件夹\管理系统\管理系统\Database\xiangmu.mdb"; // 定义连接
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
string sql = "select * from login_user where username='" + this.txtusername.Text + "' and password='" + this.txtpassword.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, objConnection);
int state = Convert.ToInt32(cmd.ExecuteScalar());//执行sql语句,并返回获得值
if (state == 0)//如果数据中没有记录或有多条记录则抱错
{
MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
}
else
{
main2Form main2 = new main2Form();
main2.Show();
this.Hide();
}
objConnection.Close(); 展开
登陆代码:
string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnection += @"Data Source=C:\Users\thtf\Desktop\新建文件夹\管理系统\管理系统\Database\xiangmu.mdb"; // 定义连接
OleDbConnection objConnection = new OleDbConnection(strConnection);
objConnection.Open();
string sql = "select * from login_user where username='" + this.txtusername.Text + "' and password='" + this.txtpassword.Text + "'";
OleDbCommand cmd = new OleDbCommand(sql, objConnection);
int state = Convert.ToInt32(cmd.ExecuteScalar());//执行sql语句,并返回获得值
if (state == 0)//如果数据中没有记录或有多条记录则抱错
{
MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
}
else
{
main2Form main2 = new main2Form();
main2.Show();
this.Hide();
}
objConnection.Close(); 展开
展开全部
加个断点调试下,应该是有空格吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C:\Users\thtf\Desktop\新建文件夹\管理系统\管理系统\Database\xiangmu.mdb不能是中文的
追问
改成英文的也不行
追答
select * from login_user where username='" + this.txtusername.Text + "' and password='" + this.txtpassword.Text + "'
改为
select count( *) from login_user where username='" + this.txtusername.Text + "' and password='" + this.txtpassword.Text + "'试试
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//........
OleDbCommand cmd = new OleDbCommand(sql, objConnection);
object o = cmd.ExecuteScalar();
int state = (o == null || o is DBNull)?0:Convert.ToInt32(o);
//........
OleDbCommand cmd = new OleDbCommand(sql, objConnection);
object o = cmd.ExecuteScalar();
int state = (o == null || o is DBNull)?0:Convert.ToInt32(o);
//........
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询