zoukankan      html  css  js  c++  java
  • c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--跨表操作)

    上篇文章我们介绍了如何利用DataSet 和 DataAdaper对象来对单张表进行操作。

    本文我们将介绍如何进行跨表操作。

    我们通过具体例子方式进行演示,例子涉及到三张表。

    1)student表(学生信息表),有 studentno和studentname两个字段,其中studentno是关键字

    2)course表(课程表),有 courseno和coursename两个字段,其中courseno是关键字

    3)score表(学生课程考试得分表),有 studentno,couseno,score三个字段,其中studentno,couseno组合为关键字。

    例子代码如下:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    namespace DbExample
    {
        class ActorMoreTable
        {
            public void fetchData()
            {
                DataSet dataSet = new DataSet();
                SqlDataAdapter adapter = null;
                SqlConnection conn = getConnection();
                try
                {
                    conn.Open();
                    string sql = "select course.courseno as courseno,coursename,student.studentno as studentno, studentname,score " +
                                "from score ,student,course "+
                                "where score.courseno = course.courseno and score.studentno = student.studentno";
                    adapter = new SqlDataAdapter(sql, conn);
    
                    adapter.InsertCommand = new SqlCommand(
                        "insert into score(studentno,courseno,score) values(@studentno,@courseno,@score)", conn);
                    adapter.InsertCommand.Parameters.Add("@studentno", SqlDbType.Int, 4, "studentno");
                    adapter.InsertCommand.Parameters.Add("@courseno", SqlDbType.Int, 4, "courseno");
                    adapter.InsertCommand.Parameters.Add("@score", SqlDbType.Int, 4, "score");
    
                    adapter.Fill(dataSet, "score");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "操作失败");
                }
                finally
                {
                    conn.Close();
                }
                updateData(dataSet, adapter);
                MessageBox.Show("操作成功");
            }
    
            private void updateData(DataSet dataSet, SqlDataAdapter adapter)
            {
                DataTable table = dataSet.Tables["score"];
                object[] values = new object[5] { 2, null, 4, null, 65 };
                table.Rows.Add(values);
                adapter.Update(dataSet, "score"); //同步到数据库
            }
    
            private SqlConnection getConnection()
            {
                string strConnection = @"Data Source = localhostSQLEXPRESS; Initial Catalog = mydb; User Id = sa; Password = 12345678;";
                SqlConnection conn = new SqlConnection(strConnection);
                return conn;
            }
        }
    }

    上面代码,我们绑定了一个insert命令。可以看出,关键就是将需要更新的字段与对应的dataset中的字段关联。

  • 相关阅读:
    判断一个表里面有没有相同的数据
    ASP.NET面试题公司必考<1>
    jQuery 实现三级联动
    javascript 面试大全
    Javascript 实现倒计时跳转页面代码
    SQL删除重复数据只保留一条 .
    编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数
    Silverlight 和javascript 之间的调用
    delphi 开放数组参数
    SPCOMM控件在Delphi7.0串口通信中的应用
  • 原文地址:https://www.cnblogs.com/51kata/p/5321652.html
Copyright © 2011-2022 走看看