欢迎来到天天文库
浏览记录
ID:306726
大小:416.50 KB
页数:11页
时间:2017-07-21
《毕业设计中C#操作数据库》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、在做毕业设计,同时也在学习C#用到DataGridView时开始觉得很难,经过朋友的帮助和自己的努力,稍微的明白了一些,现在就把我写的方法写下来,大家一起学习,写的不好的地方也请指证。 DataGridView做了新的数据显示控件加入到了.Net05中,其强大的编辑能力让其成为了数据显示中必不可少的控件。目前对于DataGridView中的更新讲的挺多的,但直接的插入数据好像讲的不是太多,下面就以我的例子说明一下。 1、首先新建一个项目。 2、建立一个数据库连接类LinkDataBase。因为数据库操作有很多都是重复性工作,
2、所以我们写一个类来简化对数据库的操作。using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Data.Sql;namespace Test...{ class LinkDataBase ...{ //设置连接字符串 private string strSQL; //与数据库连接
3、 private string connectionString = "Data Source=Localhost;Initial Catalog=Test;Integrated Security=True"; private SqlConnection myConnection; private SqlCommandBuilder sqlCmdBld; private DataSet ds = new DataSet(); private SqlDataAdapter d
4、a; public LinkDataBase() ...{ } //根据输入的SQL语句检索数据库数据 public DataSet SelectDataBase(string tempStrSQL, string tempTableName) ...{ this.strSQL = tempStrSQL; this.myConnection = new SqlConnection(connectionSt
5、ring); this.da = new SqlDataAdapter(this.strSQL, this.myConnection); this.ds.Clear(); this.da.Fill(ds, tempStrSQL); //返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名 return ds; } //数据库数据更新(传DataSet和DataT
6、able的对象) public DataSet UpdateDataBase(DataSet changedDataSet, string tableName) ...{ this.myConnection = new SqlConnection(connectionString); this.da = new SqlDataAdapter(this.strSQL, this.myConnection); this.sqlCmdBld =
7、 new SqlCommandBuilder(da); this.da.Update(changedDataSet, tableName); //返回更新过的数据库表 return changedDataSet; } //检索数据库数据(传字符串,直接操作数据库) public DataTable SelectDataBase(string tempStrSQL) ...{ this.my
8、Connection = new SqlConnection(connectionString); DataSet tempDataSet = new DataSet(); this.da = new S
此文档下载收益归作者所有