使用ado访问sql server数据库的实验报告

时间:2013-01-02 06:17:58
【文件属性】:

文件名称:使用ado访问sql server数据库的实验报告

文件大小:2.27MB

文件格式:DOC

更新时间:2013-01-02 06:17:58

sql

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(); }


网友评论

  • 写得不够详细