C#窗体连接数据库实现登陆,但是在调试时随便输个账号密码都能登陆怎么回事,数据库没有此账号密码,程序未出错。求教大神
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.Text.Trim() == "" || txtUserPassword.Text.Trim() == "")
{
MessageBox.Show("用户名或密码不能为空!");
txtUserName.Focus();
return;
}
string connString = @"Data Source=.;Initial Catalog=AddressList;uid=sa;pwd=123456";
string sqlStr = string.Format("select count (*) from [User] where UserName='{0}'and Password='{1}'", txtUserName.Text.Trim(), txtUserPassword.Text.Trim());
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand cmd = new SqlCommand(sqlStr, conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
MessageBox.Show("登陆成功!");
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入!", "错误");
txtUserName.Text = "";
txtUserPassword.Text = "";
txtUserName.Focus();
}
sdr.Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}