C#连接SQL2005的登陆系统有问题,求大神指点
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;
namespace DP_Capability
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void Login_Activated(object sender, EventArgs e)
{
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
textBox2.Focus();
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
button1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
//字符串赋值:用户名和密码
string username = textBox1.Text.Trim();
string userpwd = textBox2.Text.Trim();
//定义数据库连接语句
string consqlserver = "Data Source=.;Initial Catalog=DP Capability;Integrated Security=True;";
//定义SQL查询语句:用户名和密码
string sql = "select * from login where uid='" + username + "' and pwd='" + userpwd + "' ";
//定义SQL Server连接对象 打开数据库
SqlConnection Connection = new SqlConnection(consqlserver);
Connection.Open();
//定义查询命令:表示对数据库执行一个SQL语句或存储过程
SqlCommand com = new SqlCommand(sql, Connection);
//执行查询:提供一种读取数据库行的方式
SqlDataReader sread = com.ExecuteReader();
try
{
//如果存在用户名和密码正确数据执行进入系统操作
if (sread.Read())
{
//MessageBox.Show("登录成功");
this.Close();
NavalModel NM = new NavalModel();
NM.Show();
}
else
{
MessageBox.Show("请输入正确的用户名和密码");
}
}
catch (Exception msg)
{
throw new Exception(msg.ToString()); //处理异常信息
}
finally
{
Connection.Close(); //关闭连接
Connection.Dispose(); //释放连接
sread.Dispose(); //释放资源
}
}
}
}