c#问题,求高手指教
系统提示,我的result=cmd.ExecuteReader();总是异常,一直出现对不起!数据查询失败!,但是连接好险没有问题的啊
下面是完整代码,就是写的一个登录验证程序
using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace supermarket_management_system
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
string strcon = @"Data source=(local);Initial Catalog=supermarket management;Integrated Security=True";
SqlConnection conn = new SqlConnection();
SqlDataReader result;
public bool confirm(string user, string password, string type)
{
conn.ConnectionString = strcon;
string sql = "select * from USER where user_id='" + user + "'and user_password='" + password + "'and user_type='" + type + "'";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.Connection.Open();
try
{
result = cmd.ExecuteReader();
}
catch (Exception)
{
MessageBox.Show("对不起!数据查询失败!", "提示");
}[color=#FF0000][/color] if (result != null)
{
if (result.Read())
return true;
result.Close();
}
cmd.Connection.Close();
return false;
}
private void button1_Click(object sender, EventArgs e)
{
int count=0;
string user = textBox1.Text.Trim();
string password = textBox2.Text.Trim();
string type = null;
if (radioButton1.Checked)
type = "店长";
else if (radioButton2.Checked)
type = "保管员";
else
type = "收银员";
if ((textBox1.Text != "") && (textBox2.Text != ""))
if (confirm(user, password, type) == false)
{
if (count < 3)
{
count++;
MessageBox.Show("用户登录失败!!!" + "你还有" + (3-count) + "次机会");
}
else
this.Dispose();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
------解决方案--------------------
USER 是关键字加[],and前空格
string sql = "select * from [USER] where user_id='" + user + "' and user_password='" + password + "' and user_type='" + type + "'";
数据库名为supermarket management嘛?