1.数据库连接步骤:Connection对象
//1.构造连接字符串
string strconn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
//2.构造连接对象
SqlConnection s = new SqlConnection(strconn);
//try防止出现异常
try
{
//3.打开连接
s.Open();
MessageBox.Show("数据库打开");
}
//出现异常显示出来
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//数据库关闭
finally
{
s.Close();
MessageBox.Show("数据库关闭");
}
2.数据库操作:Command对象-登入框连接数据库
1.数据库连接
string strconn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
SqlConnection conn = =new SqlConnection(strconn);
//接收文本框的输入用户名、密码:
string name = txtUserName.Text;
string pwd = txtUserPwd.Text;
try
{
conn.Open();
2.定义SQL语句
string sqlstr=string.Format("select * from tb_User where UserName='{0}' and UserPasswd='{1}'",name,pwd);
3.创建Command的对象//封装了所有对外部数据源的操作(包括增删改查)
方法1
SqlCommand comm = new SqlCommand();
comm.Connection = conn;//Connection属性设置获取Command对象使用的Connection对象
comm.CommandText = sqlstr;//CommandText属性用来设置或获取对数据源执行的SQL语句
方法2
SqlCommand comm2 = new SqlCommand(sqlstr, conn);
//执行查询,
if (comm.ExecuteScalar() == null)// ExecuteScalar()方法执行查询,并返回查询结果查询结果集中第一行第一列(object类型),如果找不到则返回null
{
MessageBox.Show("用户名与密码不匹配,登录失败");
}
else
{
//密码正确登录到别的framain主窗体页面
frmMain frmmain = new frmMain();
frmmain.Show();//显示主窗体
}
}
catch (Exception ex)//出现异常显示
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();//关闭数据库连接
}
}
3再窗体中往数据库添加信息
string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
SqlConnection a = new SqlConnection(conn);
if (txtCollegeNo.Text == "" || txtCollegeName.Text == "")//判断窗体输入框是否为空
{
MessageBox.Show("院系编号或院系名称不能为空");
}
else
{
try
{
- Open();//数据库打开
//添加SQL语句
string sqlstr = string.Format("insert into tb_College values('{0}','{1}')", txtCollegeNo.Text, txtCollegeName.Text);
//Command对象
SqlCommand comm = new SqlCommand();
comm.Connection = a;
comm.CommandText = sqlstr;
int result = comm.ExecuteNonQuery();//执行ExecuteNonQuary方法返回一个int值,返回值为该命令所影响的行数
if (result != 0)
{
MessageBox.Show("插入成功");
}
else
{
MessageBox.Show("插?入¨?失º¡ì败㨹");
}
}
catch (Exception ex)
{
//显示异常
MessageBox.Show(ex.Message);
}
finally
{
a.Close();//数据库关闭
}
4.输入院系查询信息,并显示在窗体框中
string conn = "Data Source=.;Initial Catalog=studentInf;Integrated Security=True";
SqlConnection a = new SqlConnection(conn);
try
{
- Open();
//查询语句
string sqlstr = string.Format("select * from tb_College where DepartmentID='{0}'", txtCollegeNo.Text);
SqlCommand comm = new SqlCommand();
comm.Connection = a;
comm.CommandText = sqlstr;
//创建DataReader对象,DataReader对象不能直接使用构造函数实例化,必须通过Command对象的ExecuteReader()方法来生成。
SqlDataReader read = comm.ExecuteReader();
if (read.Read())//调用Read()方法,查询出来
{
//将查询的第二列数据在txtCollegeName框显示出来
txtCollegeName.Text = read[1].ToString();
}
else
{
//如果查询不到输出
MessageBox.Show("查询结果不存在");
txtCollegeName.Text = "";//清空查询结果
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
a.Close();
}
4.删除查询出来的数据
DBhelp是帮助类,可以直接调用
try
{
DBhelp.conn.Open();
//SQL删除语句
string sqlstr = string.Format("delete from tb_College where DepartmentID='{0}'and DepartmentName='{1}' ", txtCollegeNo.Text, txtCollegeName.Text);
DBhelp.comm.CommandText = sqlstr;
DBhelp.comm.Connection = DBhelp.conn;
if ((int)DBhelp.comm.ExecuteNonQuery() > 0)//调用Command对象ExecuteNonQuery()方法返回影响的行数
{
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
catch (Exception ex)
{
//显示异常
MessageBox.Show(ex.Message);
}
finally
{
//关闭数据库连接
DBhelp.conn.Close();
}
5.利用窗体更新数据库
…