[align=center][/align]
这下面是代码:namespace 学管1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//设置连接字符串
string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\专业软件\学管__1\学2数据库.mdb";
DataSet dataset = new DataSet(); //创建数据集
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
try
{
if (this.textBox1.Text == "") // 验证是否输入了用户姓名
{
MessageBox.Show("用户名不能为空");
textBox1.Focus();
return;
} if (this.textBox2.Text == "")// 验证是否输入了密码
{
MessageBox.Show("请输入密码");
textBox2.Focus();
return;
}
if (radioButton1.Checked == true)
{
string OleDb= string.Format("select count(*) from Users where User_id='{0}' and User_password='{1}'and User_power='{2}'", this.textBox1.Text, this.textBox2.Text, 1);
OleDbCommand comm = new OleDbCommand(OleDb, conn); // 创建comm对象
int count = (int)comm.ExecuteScalar();//执行查询,并返回查询
if (count == 1)//判断是否存在这样一列
{
this.Hide();
Form2 f2 = new Form2();
f2.Show();
}
else
{
MessageBox.Show("不存在此用户");
}
}
if (radioButton2.Checked == true)
{
string OleDb= string.Format("select count(*) from Users where User_id='{0}' and User_password='{1}'and User_power='{2}'", this.textBox1.Text, this.textBox2.Text, 0);
OleDbCommand comm = new OleDbCommand(OleDb, conn); // 创建command对象
int count = (int)comm.ExecuteScalar();//执行查询,并返回查询
if (count == 1)//判断是否存在这样一列
{
this.Hide();
Form8 f8 = new Form8();
f8.passbb(textBox1.Text);
f8.Show();
}
else
{
MessageBox.Show("输入有误,请从新输入!");
}
}
}
catch(Exception ) {
throw new Exception("产生异常!");
}
} private void button2_Click(object sender, EventArgs e)
{
radioButton1.Checked = true;
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus(); } private void radioButton2_Click(object sender, EventArgs e)
{
textBox1.Focus();
} private void Form1_Click(object sender, EventArgs e)
{
} private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
}
}异常管理对象StringC#
这下面是代码:namespace 学管1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//设置连接字符串
string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\专业软件\学管__1\学2数据库.mdb";
DataSet dataset = new DataSet(); //创建数据集
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
try
{
if (this.textBox1.Text == "") // 验证是否输入了用户姓名
{
MessageBox.Show("用户名不能为空");
textBox1.Focus();
return;
} if (this.textBox2.Text == "")// 验证是否输入了密码
{
MessageBox.Show("请输入密码");
textBox2.Focus();
return;
}
if (radioButton1.Checked == true)
{
string OleDb= string.Format("select count(*) from Users where User_id='{0}' and User_password='{1}'and User_power='{2}'", this.textBox1.Text, this.textBox2.Text, 1);
OleDbCommand comm = new OleDbCommand(OleDb, conn); // 创建comm对象
int count = (int)comm.ExecuteScalar();//执行查询,并返回查询
if (count == 1)//判断是否存在这样一列
{
this.Hide();
Form2 f2 = new Form2();
f2.Show();
}
else
{
MessageBox.Show("不存在此用户");
}
}
if (radioButton2.Checked == true)
{
string OleDb= string.Format("select count(*) from Users where User_id='{0}' and User_password='{1}'and User_power='{2}'", this.textBox1.Text, this.textBox2.Text, 0);
OleDbCommand comm = new OleDbCommand(OleDb, conn); // 创建command对象
int count = (int)comm.ExecuteScalar();//执行查询,并返回查询
if (count == 1)//判断是否存在这样一列
{
this.Hide();
Form8 f8 = new Form8();
f8.passbb(textBox1.Text);
f8.Show();
}
else
{
MessageBox.Show("输入有误,请从新输入!");
}
}
}
catch(Exception ) {
throw new Exception("产生异常!");
}
} private void button2_Click(object sender, EventArgs e)
{
radioButton1.Checked = true;
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus(); } private void radioButton2_Click(object sender, EventArgs e)
{
textBox1.Focus();
} private void Form1_Click(object sender, EventArgs e)
{
} private void Form1_Load(object sender, EventArgs e)
{
textBox1.Focus();
}
}
}异常管理对象StringC#
throw new Exception("产生异常!");
}
=>
catch(Exception exp) {
MessageBox.Show(exp.Message);
}看看显示什么。
将SQL 语句放到 access 中运行一下 就知道哪里错了~ 目测是 User_id='{0}' 这里错了 应该改为 User_id={0} 好像 access 中ID值不需要单引号!
然后F10 得到OleDB的内容 放在数据库里面,看能不能运行,目测不能