using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace MySchool
{
class DBHelp
{
private string connStr = "Data Source=.;Initial Catalog=SchoolDB;Integrated Security=True";
private SqlConnection connection;
public SqlConnection Connection
{
get
{
connection = new SqlConnection(connStr); ;
return connection;
}
}
public void OpenConnection()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if(Connection.State==ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
public void CloseConnection()
{
if(Connection.State==ConnectionState.Open||Connection.State==ConnectionState.Broken)
{
Connection.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace MySchool
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
public const string HINT = "提示";
private void btnLogin_Click(object sender, EventArgs e)
{
string name = txtLogin.Text;
string pwd = txtPwd.Text;
string type=cmbType.Text;
if(name.Equals(string.Empty))
{
MessageBox.Show("用户名不能为空",HINT,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtLogin.Focus();
}
else if (pwd.Equals(string.Empty))
{
MessageBox.Show("密码不能为空",HINT,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtPwd.Focus();
}
else
{
//string connStr = "Data Source=.;Initial Catalog=SchoolDB;User ID=sa;Password=1972";
//SqlConnection connection = new SqlConnection(connStr);
//connection.Open();
DBHelp help = new DBHelp();
help.OpenConnection();
string sqlStr = "select count(*) from Administracter where UserName='" + name + "' and LoginPwd='" + pwd + "'";
SqlCommand cmd = new SqlCommand(sqlStr,help.Connection);
int iRet = (int)cmd.ExecuteScalar();
if (iRet != 0)
{
Teacher tch = new Teacher();
tch.name = name;
tch.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名密码不匹配", HINT, MessageBoxButtons.OK,MessageBoxIcon.Error);
}
help.CloseConnection();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult dlg =MessageBox.Show("确定吗","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (dlg == DialogResult.Yes)
{
this.Close();
}
}
private void frmLogin_Load(object sender, EventArgs e)
{
cmbType.SelectedIndex = 0;
}
}
}
这多半是你数据库的配置的问题,看下错误编号,如果是26 40等,可以检查sql server配置