C#知识点总结3

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

{

  1. 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

{

  1. 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.利用窗体更新数据库

                                                                                 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值