1.sql约束
where sname like ‘张_’
where sname like ‘张[0-9]’
int num not null check(num<=30)
sex char(2) not null check(sex in(‘男’,’女’))
check([EmpSex]=’男’ or [EmpSex]=’女’)
2.DBHelperSQL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//1.先导入必要的命名空间,3个
using System.Data;//DataTable
using System.Data.SqlClient;//SqlConnection的命名空间
using System.Windows.Forms;//MessageBox
namespace Demo2016
{
public class DBHelperSQL
{
/// <summary>
/// 连接字符串
/// </summary>
public static string connStr = @"server=WIN-RDJDM79VJSM;database=test2016;integrated security=true;";
//加入@可以防止在转义的时候发生错误
/// <summary>
/// 连接实例初始值为空
/// </summary>
public static SqlConnection conn = null;
/// <summary>
/// 增删改
/// </summary>
/// <param name="sql"></param>
/// <param name="para"></param>
public static void TableChange(string sql, SqlParameter[] para = null)
{
try
{
SqlCommand cmd = new SqlCommand(sql, DBHelperSQL.conn);
if (para != null)
cmd.Parameters.AddRange(para);阿
if (cmd.ExecuteNonQuery() > 0)//影响行数
{
MessageBox.Show("操作成功!");
}
else
{
MessageBox.Show("操作失败!\n请检查数据库中是否有该条数据!");
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sql"></param>
/// <param name="para"></param>
/// <returns></returns>
public static DataTable TableSelect(string sql, SqlParameter[] para = null)
{
try
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sql, DBHelperSQL.conn);
if (para != null)
cmd.Parameters.AddRange(para);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
return null;//出现异常,返回空
}
}
}
}
3.program.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Demo2016
{
static class Program
{
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
//产生一个实例
DBHelperSQL.conn=new System.Data.SqlClient.SqlConnection(DBHelperSQL.connStr);
DBHelperSQL.conn.Open();//打开数据库
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Application.Run(new FormMain());
DBHelperSQL.conn.Close();
}
}
}
4.formMain.cs代码
namespace Demo2016
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void 信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormSearch fs = new FormSearch();
fs.MdiParent = this;// 指定查询界面的依赖容器是这个
fs.Show();
}
private void 信息统计ToolStripMenuItem_Click(object sender, EventArgs e)
{
FormStatistics fss = new FormStatistics();
fss.MdiParent = this;
fss.Show();
}
private void 学生表维护ToolStripMenuItem_Click(object sender, EventArgs e)
{
btnUpdate fst = new btnUpdate();
fst.MdiParent = this;
fst.Show();
}
private void 退出?ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
5.学生表维护代码
using System.Data.SqlClient;//添加命名空间
namespace Demo2016
{
public partial class btnUpdate : Form
{
public btnUpdate()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string sql = @"INSERT INTO [test2016].[dbo].[Student]
([SNo]
,[SName]
,[Date]
,[ClassNo])
VALUES(@SNo,@SName,@Date,@ClassNo)";
SqlParameter[] para ={new SqlParameter("@SNo",txtSNo.Text),
new SqlParameter("@SName",txtSName.Text),
new SqlParameter("@Date",txtSDate.Text),
new SqlParameter("@ClassNo",txtClassNo.Text)};
DBHelperSQL.TableChange(sql,para);
MyShow();
}
private void btnDelete_Click(object sender, EventArgs e)
{
string sql = @"DELETE FROM [test2016].[dbo].[Student]
WHERE [SNo] = @SNo";
SqlParameter[] para ={new SqlParameter("@SNo",txtSNo.Text)};
DBHelperSQL.TableChange(sql, para);
MyShow();
}
private void btnSelect_Click(object sender, EventArgs e)
{
MyShow();
}
private void btnUpdate1_Click(object sender, EventArgs e)
{
string sql = @"UPDATE [test2016].[dbo].[Student]
SET [SName] = @SName
,[Date] = @Date
,[ClassNo] = @ClassNo
WHERE [SNo] = @SNo";
SqlParameter[] para ={new SqlParameter("@SNo",txtSNo.Text),
new SqlParameter("@SName",txtSName.Text),
new SqlParameter("@Date",txtSDate.Text),
new SqlParameter("@ClassNo",txtClassNo.Text)};
DBHelperSQL.TableChange(sql, para);
MyShow();
}
public void MyShow()
{
string sql = @"SELECT [SNo]
,[SName]
,[Date]
,[ClassNo]
FROM [test2016].[dbo].[Student]";
dv.DataSource = DBHelperSQL.TableSelect(sql);
}
private void btnUpdate_Load(object sender, EventArgs e)
{
}
}
}
6.查询统计代码
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;
namespace Demo2016
{
public partial class FormStatistics : Form
{
public FormStatistics()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
string sql = @"select a.DeptNo 系编号,b.DeptName 系名称,SUM(a.Num) 总人数
from Class a,Department b
where a.DeptNo=b.DeptNo
group by a.DeptNo,b.DeptName";
dv.DataSource = DBHelperSQL.TableSelect(sql);
}
private void btn2_Click(object sender, EventArgs e)
{
string sql = @"select CYear 入学年份,SUM(Num)总人数
from Class
group by CYear";
dv.DataSource = DBHelperSQL.TableSelect(sql);
}
}
}
7.任意查询,组合查询
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;//添加
namespace Demo2016
{
public partial class FormSearch : Form
{
public FormSearch()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
string sql = string.Format(@"select
a.SName 名字,
b.SpecName 专业名称,
b.DeptNo 系编号
from Student a,Class b
where a.ClassNo=b.CNo and a.SName like '%{0}%'",txtSName.Text);
if (chbSpecName.CheckState == CheckState.Checked)
{
sql += string.Format(@"and b.SpecName='{0}'", cboSpecName.SelectedValue.ToString());
}
if (chbDeptNo.CheckState == CheckState.Checked)
{
sql = sql + string.Format(@"and b.DeptNo='{0}'", cboDeptNo.SelectedValue.ToString());
}
if(chbName.CheckState==CheckState.Checked)
{
sql+=string.Format(@"and a.SName like '%{0}%'",txtSName.Text);
}
if (chbSpecName.CheckState == CheckState.Unchecked && chbDeptNo.CheckState == CheckState.Unchecked && chbName.CheckState == CheckState.Unchecked)
{
MessageBox.Show("您未选择任何勾选项!");
}
dv.DataSource = DBHelperSQL.TableSelect(sql);
}
private void FormSearch_Load(object sender, EventArgs e)
{
string sqlD = @"SELECT [DeptNo]
,[DeptName]
FROM [test2016].[dbo].[Department]";
DataTable dtD = DBHelperSQL.TableSelect(sqlD);
cboDeptNo.DataSource = dtD;
cboDeptNo.DisplayMember = dtD.Columns["DeptNo"].ToString();
cboDeptNo.ValueMember = dtD.Columns["DeptNo"].ToString();
cboDeptNo.SelectedIndex = 0;
string sqlS = @"SELECT [CNo]
,[SpecName]
,[Num]
,[CYear]
,[DeptNo]
FROM [test2016].[dbo].[Class]";
DataTable dtS = DBHelperSQL.TableSelect(sqlS);
cboSpecName.DataSource = dtS;
cboSpecName.DisplayMember = dtS.Columns["SpecName"].ToString();
cboSpecName.ValueMember = dtS.Columns["SpecName"].ToString();
cboSpecName.SelectedIndex = 0;
}
}
}
8.readme
1.附加数据库test201603.mdf
2.SQL文件夹下为数据库的所有操作
3.Info为数据库设计文档
4.修改DBhelperSQL.cs文件下的连接字符串使之适合自己的电脑
5.backinfo.bak为数据库备份文件
6.连接字符串:@”server=你的计算机全名;database=你的数据库名;integrated security=true;”;
9.info
基本信息
计算机全名:WIN-RDJDM79VJSM
windows身份验证方式
连接字符串:@”server=WIN-RDJDM79VJSM;database=test201603;integrated security=true;”;
数据库创建