zoukankan      html  css  js  c++  java
  • ADO.NET事务处理,初始回调函数,多张表的数据在同一个DataGridView中展示

        执行ADO.NET事务包含四个步骤,接下来以Transaction对象为例介绍。

    (1)调用SQLConnection对象的BeginTransaction()方法,创建一个SQLTransaction对象,标志事务开始

    (2)将创建的SQLTransaction对象分配给要执行的SqlCommand的Transaction属性。

    (3)调用相应的方法执行SQLCommand命令。

    (4)调用SQLTransaction的Commit()方法完成事务,或调用Rollback()方法终止事务。

    string str = "data source=.;initial catalog=MySchool;uid=sa";
                SqlConnection con = new SqlConnection(str);
                string sql = "insrt into grad valves(@gname)";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlParameter para = new SqlParameter("@gname", textBox1.Text);
                SqlTransaction tran = con.BeginTransaction();
                cmd.Parameters.Add(para);
                con.Open();
                cmd.Transaction = tran;
                int count = 0;
                try
                {
                    count = cmd.ExecuteNonQuery();
                    tran.Commit();
                }
                catch (Exception ex)
                {
    
                    tran.Rollback();
                    MessageBox.Show(ex.Message);
                }
                if (count > 0)
                {
                    MessageBox.Show("成功");
                }
                con.Close();

    注意:在调用BeginTransaction()方法开始事务前,要打开数据库连接,否则将出现异常。

        回调函数

      回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。

      机制:

      ⑴定义一个回调函数;
      ⑵提供函数实现的一方在初始化的时候,将回调函数的函数指针注册给调用者;
      ⑶当特定的事件或条件发生的时候,调用者使用函数指针调用回调函数对事件进行处理。
      意义:
      因为可以把调用者与被调用者分开,所以调用者不关心谁是被调用者。它只需知道存在一个具有特定原型和限制条件的被调用函数。简而言之,回调函数就是允许用户把需要调用的方法的指针作为参数传递给一个函数,以便该函数在处理相似事件的时候可以灵活的使用不同的方法
     
        多张表的数据在同一个DataGridView中展示
      
    private void FrmMain_Load(object sender, EventArgs e)
            {
                List<Entity> list=loagingInfo();
                dgvList.DataSource = list;
            }
            public List<Entity> loagingInfo()
            {
                List<Entity> stuList = new List<Entity>();
                
                //连接数据库
                string str = "Data Source=.;initial catalog=MySchool;user id=sa;pwd=347222";
                SqlConnection con = new SqlConnection(str);
                string sql = @"select studentname,subjectname,StudentResult,ExamDate
                                from Student,Subject,Result
                                where student.StudentNo=Result.StudentNo
                                and result.SubjectId=Subject.SubjectId";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                con.Close();
                foreach (DataRow item in dt.Rows)
                {
                    Entity entity = new Entity();
                    entity.studentName = item["studentname"].ToString();
                    entity.subjectName = item["subjectname"].ToString();
                    entity.result = Convert.ToInt32(item["StudentResult"]);
                    entity.examDate = Convert.ToDateTime(item["ExamDate"]);
                    stuList.Add(entity);
                }
                
                return stuList;
            }

  • 相关阅读:
    Numpy
    Homer SIP capture and VoIP Monitoring Install Guide
    How to install FreeSWITCH in Centos 7
    Media Samples
    lavfi
    Jitsi Sip Communicator Source Code Build Guide for Windows
    How to install Asterisk 13 with WebRTC support in CentOS
    How to build PJSIP with WebRTC
    WebRTC & SIP: The Demo
    WebRTC tutorial using SIPML5
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/5337523.html
Copyright © 2011-2022 走看看