namespace _日记本
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("用户名不能为空!", "提示");
else if (textBox2.Text == "")
MessageBox.Show("密码不能为空!", "提示");
try //try...catch...异常处理语句
{
string name, pass;
bool flag = false;
name = textBox1.Text;
pass = textBox2.Text; //获取用户名,密码
string str = Properties.Settings.Default.Database1ConnectionString;//连接字符串
SqlConnection Conn = new SqlConnection(str);//建立到数据库的连接
Conn.Open(); //将连接打开
//SQL语句:从数据库的登录表中搜索登录名,密码
string sqlstring = "select * from dbo.Table where Id ='" + name + "'and Password ='" + pass + "'";
//执行con对象的函数,返回一个SqlCommand类型的对象
SqlCommand command = new SqlCommand(sqlstring, Conn);
//用cmd的函数执行语句,返回SqlDataReader对象thisReader,thisReader就是返回的结果集(也就是数据库中查询到的表数据)
SqlDataReader thisReader = command.ExecuteReader();
//判断用户名及密码是否正确,对flag进行赋值
while (thisReader.Read())
{
if ((thisReader.GetValue(0).ToString().Trim()) == (name.ToString().Trim()))
{
if (thisReader.GetValue(1).ToString().Trim() == pass.ToString().Trim())
{
flag = true;
}
}
}
//用完后关闭连接,以免影响其他程序访问
Conn.Close();
if (flag)
{
MessageBox.Show("登陆成功!");
Form2 F = new Form2(); //显示主页面
F.Show();
this.Hide();
}
else
{
MessageBox.Show("请检查你的用户名和密码!");
textBox1.Focus();
}
}
catch (Exception ex2)
{
MessageBox.Show("连接远程SQL数据库发生错误:" + ex2.ToString(), "错误!");
}
}
}
}
表里就一个Id一个Password报错是这样的,是不是我的sql语句有语法错误,查了半天查不出来,小弟还是新手,请各位大手子指点一下,不胜感激