通过sql server 语句对数据进行增删查改实现一个简易的学生管理系统的。
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.Configuration;
using System.Data.SqlClient;
namespace _2012._2._22人员管理
{
public partial class Manager : Form
{
public Manager()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
string str = "select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where ";
//cmd.CommandText="select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where "+comboBox1.Text+" like '%"+textBox1.Text+" %' ";
string strs = ReturnSqlWhere(cmd);
cmd.CommandText = str + strs;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows.Count > 0)
{
this.dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show("数据不存在!");
}
}
private string ReturnSqlWhere(SqlCommand cmd)
{
string sqlwhere = string.Empty;
switch (comboBox1.Text)
{
case "姓名":
sqlwhere = "B.Name like @name";
cmd.Parameters.AddWithValue("@name", "%" + textBox1.Text + "%");
break;
case "用户名":
sqlwhere = "A.UserName like @username";
cmd.Parameters.AddWithValue("@username", "%" + textBox1.Text + "%");
break;
case "电话":
sqlwhere = "B.Tell like @tell"; cmd.Parameters.AddWithValue("@tell", "%" + textBox1.Text + "%");
break;
case "地址":
sqlwhere = "B.Address like @address";
cmd.Parameters.AddWithValue("@address", "%" + textBox1.Text + "%");
break;
default:
sqlwhere = "B.Sfzh like @sfzh";
cmd.Parameters.AddWithValue("@sfzh", "%" + textBox1.Text + "%");
break;
}
return sqlwhere;
}
private void button2_Click(object sender, EventArgs e)
{
add a = new add();
a.Show();
}
private void button3_Click(object sender, EventArgs e)
{
string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
int cou =Convert.ToInt32(this.dataGridView1.SelectedCells[0].Value);
cmd.CommandText = "update T_User set Error=0 where UserId=@userid";
cmd.Parameters.AddWithValue("@userid",cou);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("解锁成功");
}
else
{
MessageBox.Show("解锁失败");
}
}
private void button4_Click(object sender, EventArgs e)
{
string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
string str = "select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where ";
//cmd.CommandText="select A.UserId,A.UserName,A.Error,B.Name,isnull(B.Sfzh,'还未填写')Sfzh,B.Tell,B.Address,B.Age,(case B.General when 1 then '男' when 0 then '女' end)性别 from T_User A LEFT OUTER JOIN T_UserInfo B on A.UserId=B.UserId where "+comboBox1.Text+" like '%"+textBox1.Text+" %' ";
string strs = ReturnSqlWhere(cmd);
cmd.CommandText = str + strs;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
if (dt.Rows.Count > 0)
{
this.dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show("数据不存在!");
}
}
}
}
以上包括查和增,还有对用户的解锁,当一个用户登陆时三次错误密码,账户就会在一定被锁定,这时可以找管理员进行解锁。解锁就是对数据库中的数据进行修改,所以以上代码基本就包括了数据库的操作增、查、改!