using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 项目
{
public partial class 登陆 : Form
{
public 登陆()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button2_MouseMove(object sender, MouseEventArgs e)
{
this.Close();
} private void button1_Click(object sender, EventArgs e)
{
string sql = string.Format("select count(*) from Logins where UserID='{0}'and UserPwd='{1}'", txtId.Text, txtPwd.Text);
try
{
DBHelper.connection.Open();
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
int num = (int)command.ExecuteScalar();
DBHelper.connection.Close();
if (num != 1)//没返回 说明密码不存在
{
MessageBox.Show("用户名或密码不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//判断用户类型 0为普通用户 1为会员
int type = 0;
MessageBox.Show("登入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
string sql1 = string.Format("select State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text);
DBHelper.connection.Open();
SqlCommand command1 = new SqlCommand(sql, DBHelper.connection);
type = (int)command1.ExecuteScalar();//此处出错 返回值永远是1 难道不是用.ExecuteScalar()方法么? 希望各位老师帮帮忙
Main main = new Main();
Second second = new Second();
if (type==0)
{
main.Show();
this.Visible = false;
}
else if(type==1)
{
second.Show();
this.Visible = false;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally {
DBHelper.connection.Close();
}
}
}
}
实验阶段 按钮名称没改 望见谅
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace 项目
{
public partial class 登陆 : Form
{
public 登陆()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button2_MouseMove(object sender, MouseEventArgs e)
{
this.Close();
} private void button1_Click(object sender, EventArgs e)
{
string sql = string.Format("select count(*) from Logins where UserID='{0}'and UserPwd='{1}'", txtId.Text, txtPwd.Text);
try
{
DBHelper.connection.Open();
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
int num = (int)command.ExecuteScalar();
DBHelper.connection.Close();
if (num != 1)//没返回 说明密码不存在
{
MessageBox.Show("用户名或密码不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//判断用户类型 0为普通用户 1为会员
int type = 0;
MessageBox.Show("登入成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
string sql1 = string.Format("select State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text);
DBHelper.connection.Open();
SqlCommand command1 = new SqlCommand(sql, DBHelper.connection);
type = (int)command1.ExecuteScalar();//此处出错 返回值永远是1 难道不是用.ExecuteScalar()方法么? 希望各位老师帮帮忙
Main main = new Main();
Second second = new Second();
if (type==0)
{
main.Show();
this.Visible = false;
}
else if(type==1)
{
second.Show();
this.Visible = false;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally {
DBHelper.connection.Close();
}
}
}
}
实验阶段 按钮名称没改 望见谅
你把txtId和txtPwd值放进去,在SQL里执行一下,看看state到底是不是1不就知道了,
SqlCommand command1 = new SqlCommand(sql, DBHelper.connection);
type = (int)command1.ExecuteScalar();//此处出错 返回值永远是1 难道不是用.ExecuteScalar()方法么? 希望各位老师帮帮忙
*/
lz这里用的是sql
string sql = string.Format("select count(*) from Logins where UserID='{0}'and UserPwd='{1}'", txtId.Text, txtPwd.Text);当然返回1应该用string sql1 = string.Format("select State from Logins where UserID='{0}' and UserPwd='{1}'", txtId.Text, txtPwd.Text);