qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

ADO连接数据库的增删改查方法

 1:添加命名空间System.Data.SqlClient中的SQL Server访问类;
  2:与SQL Server数据库建立连接,ADO.NET提供Connection对象用于建立与SQL Server数据库的连接
  string connectionStr = "Data source=服务器名;Initial Catalog=数据库名称; uid=用户名;pwd=密码()";        //  定义连接字符串
  // Integrated Security=True    集成身份验证
  //uid=xxx;Pwd=xxx    用户名密码登陆
  SqlConnection connection1 = new SqlConnection(connectionStr);           ///实例化Connection对象用于连接数据源
  connection1.Open();                                                                          ///打开数据库连接
  ……………
  connection1.Close();                                                                         ///关闭数据库连接
  3:与SQL Server数据库建立连接后,使用命令对象SqlCommand类直接对数据库进行操作
  (1)增加、删除、修改操作
  SqlConnection connection1 = new SqlConnection(connectionStr);            //建立连接
  connection1.Open();                                                                            //打开数据库连接
  string sqlStr = "(SQL执行语句,例如  insert into A  values('abc',1))";       //定义相关的执行语句,相当于写好命令
  SqlCommand command1 = new SqlCommand(sqlStr, connection1);          //构造函数指定命令对象所使用的连接对象connection1以及命令文本sqlStr ,相当于让系统接受命令。
  command1.ExecuteNonQuery();                                                             //ExecuteNonQuery()方法返回值为一整数,代表操作所影响到的行数,注意ExecuteNonQuery()方法一般用于执行                                                                                                                           // UPDATE、INSERT、DELETE等非查询语句,可以理解为让系统执行命令
  connection1.Close();     ///关闭数据库连接
  示例1:删除的Course表中课程编号为003的记录:
string connectionStr = "Data source=.;Initial Catalog=Student; Integrated Security=True";
SqlConnection connections = new SqlConnection(connectionStr);
string sqlstr = "delete from Course where Cno='006' ";
SqlCommand command1 = new SqlCommand(sqlstr, connectionss);
conn.Open();
if (command1.ExecuteNonQuery() > 0)
{
MessageBox.Show("删除课程成功!");
};
connections .Close();
  示例2:向Course表中增加一门课程,课程信息由前台输入
string connectionStr = "Data source=.;Initial Catalog=Student;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionStr);
int Credit = Convert.ToInt32(txtCredit.Text); /TextBox.text是string类型,需要用到强制转换方法“Convert.ToInt32”将string类型转化为int类型
string sqlStr = "insert into Course values('" + txtCno.Text + "','" + txtCname.Text + "'," + Credit + ")";//因为字符串的组成部分为需要从前台读取的变量,所以在这里需要用到字符串拼接,
//拼接字符:‘ “+字符串变量+” ’,拼接数字:“+数字变量+”
SqlCommand command1= new SqlCommand(sqlStr, connection);
connection.Open();
if (command1.ExecuteNonQuery() > 0)
{
MessageBox.Show("课程添加成功!");
};
connection.Close();
示例3:把课程“线性代数”的学分修改为5分
string connectionStr = "Data source=.;Initial Catalog=Student; Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionStr);
string sqlStr = "update Course set Ccredit=5 where Cname='线性代数'";
SqlCommand command1= new SqlCommand(sqlStr, connection);
connection .Open();
if (command1.ExecuteNonQuery() > 0)
{
MessageBox.Show("学分修改成功!");
};
connection .Close();
  (2)查询数据库,用ExecuteScalar()方法,返回单个值(Object)(查询结果第一行第一列的值)
  示例4:从Student表中查询学号为201244111学生的姓名:
string connectionStr = "Data source=.;Initial Catalog=Student;
Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionStr);
string sqlstr = "select Sname from student where Sno='201244111' ";
SqlCommand command1 = new SqlCommand(sqlstr, connection);
connection.Open();
string studentName = command1.ExecuteScalar().ToString();
MessageBox.Show(studentName);
connection.Close();
  使用DataReader读取多行数据,逐行读取,每次读一行
  示例5:运用DataReader逐行读出student表中的第一列数据
string connectionStr = "Data source=.;Initial Catalog=Student;
Integrated Security=True"; SqlConnection connection = new SqlConnection(connectionStr);
string sqlstr = "select *from student";
SqlCommand command1 = new SqlCommand(sqlstr, connection);
connection.Open();
SqlDataReader dataReader1 = command1.ExecuteReader();
// DataReader类没有构造函数,不能实例化,需要通过调用Command对象的command1的ExecuteReader()方法
while (dataReader1.Read())  ///DataReader的Read()方法用于读取数据,每执行一次该语句,DataReader就向前读取一行数据;如果遇到末尾,就返回False,否则为True
{
MessageBox.Show(dataReader1[0].ToString());
}
connection.Close();
  4.使用SqlDataAdapter数据适配器类访问数据库 ,注意:它既可以将数据库中数据传给数据集中的表,又可将数据集中的表传到数据库中。简言之,数据适配器类用于数据源与数据集间交换数据
  (链接语句略)
  connection1.Open();      ///打开数据库连接
  string sqlStr = "SELECT * FROM A";                                                                ///从A表中选择所有数据的SQL语句
  SqlDataAdapter dataAdapter1 = new dataAdapter(sqlStr, connection1);            ///构造名为dataAdapter1的数据适配器对象,            并指定连接对象connection1以及SELECT语句
  DataSet dataSet1 = new DataSet();                                                            ///构造名为dataSet1的数据集对象 dataAdapter1.Fill(dataSet1);
  ………………………………
  ///使用SqlDataAdapter类中的Fill()方法将数据填充到数据集中,注意:SqlDataAdapter类中的Fill()方法和Update()方法可用于将数据填充到单个数据表或数据集中
  connection1.Close();
  示例6:将Student表中的数据全部查询出来
string connectionStr = "Data source=.;Initial Catalog=Student;
Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionStr);
string sqlstr = "select *from student";
connection.Open();
SqlDataAdapter dataAdapter1 = new SqlDataAdapter(sqlstr, connection);
DataSet dataSet1 = new DataSet();
dataAdapter1.Fill(dataSet1); ///使用SqlDataAdapter类中的Fill()方法将数据填充到数据集中,相当于程序的临时数据库
DataTable dt1 = dataSet1.Tables[0];
///获取数据集的第一张表
this.dataGridView1.DataSource = dt1;
connection.Close();

posted on 2014-09-29 10:08 顺其自然EVO 阅读(196) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜