使用ado访问sql server数据库的实验报告
1,主界面 2查询功能 ‘ private void chaxun_Click(object sender, System.EventArgs e) { SqlConnection thisConnection=new SqlConnection("Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=李梦然07060021"); //表示到SQL Server的一个实例的连接 SqlCommand thisCommand=new SqlCommand("select * from student where sno='"+textBox1.Text+"'",thisConnection); SqlDataAdapter thisAdapter=new SqlDataAdapter(); thisAdapter.SelectCommand=thisCommand; DataSet thisDataSet=new DataSet(); thisConnection.Open(); thisAdapter.Fill(thisDataSet, "student"); //在运行时设置 dataGrid1的数据源和数据成员属性,即在dataGrid1中显示数据集中的数据 dataGrid1.SetDataBinding(thisDataSet,"student"); thisConnection.Close(); } 3浏览功能 private void liulan_Click(object sender, System.EventArgs e) { //用SqlConnection对象连接SQL Server数据库魏菊丽20086666 SqlConnection thisConnection=new SqlConnection("Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=李梦然07060021"); SqlDataAdapter thisAdapter=new SqlDataAdapter(); DataSet thisDataSet=new DataSet(); //创建并返回一个与SqlConnection相关联的SqlCommand 对象 SqlCommand thisCommand=thisConnection.CreateCommand(); thisCommand.CommandText="select * from student";//获取或设置要对数据源执行的SQL语句 thisAdapter.SelectCommand =thisCommand ;//获取一个SQL语句,用于在数据源中选择记录 thisConnection.Open();//打开本次设置的数据库连接 thisAdapter.Fill(thisDataSet,"student");//将以上在数据源student中选择的记录的所有行填充到数据集中。 thisConnection.Close();//断开本次数据库连接 //在运行时设置 dataGrid1的数据源和数据成员属性,即在dataGrid1中显示数据集中的数据 dataGrid1.SetDataBinding(thisDataSet,"student"); } 4,插入一个新列 private void button1_Click(object sender, System.EventArgs e) { SqlConnection thisConnection=new SqlConnection("Data Source=LocalHost;Integrated Security=SSPI;Initial Catalog=李梦然07060021"); SqlDataAdapter thisAdapter=new SqlDataAdapter(); DataSet thisDataSet=new DataSet(); SqlCommand thisCommand=thisConnection.CreateCommand(); thisCommand.CommandText="select * from student "; thisAdapter.SelectCommand =thisCommand ; thisConnection.Open(); SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter); thisAdapter.Fill(thisDataSet, "student"); DataRow thisRow=thisDataSet.Tables["student"].NewRow();//在 数据集的student Table中创建新行 thisRow["sno"]="21";thisRow["sname"]="李梦然";thisRow["ssex"]="男";thisRow["thirthday"]="1987-7-31";thisRow["class"]="95001";//设置新行中的个字段值 thisDataSet.Tables["student"].Rows.Add(thisRow);//将新行添加到数据集的student Table中 thisAdapter.Update(thisDataSet,"student");// 修改数据库表 //以下显示添加后表中的数据 thisCommand.CommandText="select * from student "; thisAdapter.SelectCommand =thisCommand ; dataGrid1.SetDataBinding(thisDataSet,"student"); thisConnection.Close(); }
- 粉丝: 9
- 资源: 6
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 27页智慧街道信息化建设综合解决方案.pptx
- 计算机二级Ms-Office选择题汇总.doc
- 单链表的插入和删除实验报告 (2).docx
- 单链表的插入和删除实验报告.pdf
- 物联网智能终端项目设备管理方案.pdf
- 如何打造品牌的模式.doc
- 样式控制与页面布局.pdf
- 武汉理工Java实验报告(二).docx
- 2021线上新品消费趋势报告.pdf
- 第3章 Matlab中的矩阵及其运算.docx
- 基于Web的人力资源管理系统的必要性和可行性.doc
- 基于一阶倒立摆的matlab仿真实验.doc
- 速运公司物流管理模式研究教材
- 大数据与管理.pptx
- 单片机课程设计之步进电机.doc
- 大数据与数据挖掘.pptx
评论1