Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » UltraLite - .NET 编程 » 教程:构建 UltraLite.NET 应用程序

 

第 4 课:插入、更新和删除数据

在这节课中,您将向应用程序中添加代码以修改数据库中的数据。以下过程使用动态 SQL。也可以使用 Table API 执行同样的技术。

请参见使用 Table API 访问和操作数据

以下过程创建用于维护列表框的支持方法。在接下来的过程中创建的数据操作方法将用到此方法。

♦  添加维护列表框的代码
  1. 右击窗体,然后选择 [View Code]。

  2. 添加一个 Form1 类的方法,用于更新及填充列表框。此方法执行以下任务:

    • 清除列表框。

    • 实例化一个 ULCommand 对象并为其指派一个 SELECT 查询,该查询返回数据库中 Names 表的数据。

    • 执行查询,返回结果集作为 ULDataReader。

    • 实例化一个整数数组,其长度与结果集中的行数相同。

    • 用 ULDataReader 返回的名称填充列表框,并用 ULDataReader 返回的 ID 填充整数数组。

    • 关闭 ULDataReader。

    • 如果出现错误,则输出错误消息。对于 SQL 错误,该代码还会输出错误代码。

      请参见错误消息

    对于 C#,向应用程序中添加以下代码,创建 Form1 类的方法。

    //Visual C#
    private void RefreshListBox(){
        try{
            long NumRows;
            int I = 0;
            lbNames.Items.Clear();
            using( ULCommand cmd = Conn.CreateCommand() ){
                cmd.CommandText = "SELECT ID, Name FROM Names";
                using( ULDataReader dr = cmd.ExecuteReader()){
                    dr.MoveBeforeFirst();
                    NumRows = dr.RowCount;
                    ids = new int[ NumRows ];
                    while (dr.MoveNext())
                    {
                        lbNames.Items.Add(
                        dr.GetString(1));
                        ids[ I ] = dr.GetInt32(0);
                        I++;
                    }
                }
                txtName.Text = " ";   
            }
        }
        catch( Exception err ){
            MessageBox.Show(
            "Exception in RefreshListBox: " + err.Message );
        }
    }

    对于 Visual Basic,请将以下代码作为 Form1 类的一个方法添加到应用程序中。

    'Visual Basic
    Private Sub RefreshListBox()
        Try
            Dim cmd As ULCommand = Conn.CreateCommand()
            Dim I As Integer = 0
            lbNames.Items.Clear()
            cmd.CommandText = "SELECT ID, Name FROM Names"
            Dim dr As ULDataReader = cmd.ExecuteReader()
            ReDim ids(dr.RowCount)
            While (dr.MoveNext)
                lbNames.Items.Add(dr.GetString(1))
                ids(I) = dr.GetInt32(0)
                I = I + 1
            End While
            dr.Close()
            txtName.Text = " "   
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
  3. 生成项目。

    生成项目时不应产生错误。

♦  实现 INSERT、UPDATE 和 DELETE
  1. 在窗体设计选项卡上,双击 [Insert] 以创建 btnInsert_Click 方法。此方法执行以下任务:

    • 实例化一个 ULCommand 对象,并为其指派一个 INSERT 语句,该语句会将文本框中的值插入数据库。

    • 执行该语句。

    • 释放 ULCommand 对象。

    • 刷新列表框。

    • 如果出现错误,则输出错误消息。对于 SQL 错误,该代码还会输出错误代码。

      请参见错误消息

    对于 C#,请将以下代码添加到 btnInsert_Click 方法中。

    //Visual C#
    try {
        long RowsInserted;
        using( ULCommand cmd = Conn.CreateCommand() ) {
            cmd.CommandText = 
                "INSERT INTO Names(name) VALUES (?)";
            cmd.Parameters.Add("", txtName.Text);
            RowsInserted = cmd.ExecuteNonQuery();
        }
        RefreshListBox();
    } 
    catch( Exception err ) {
        MessageBox.Show("Exception: " + err.Message );
    }

    对于 Visual Basic,请将以下代码添加到 btnInsert_Click 方法中。

    'Visual Basic
    Try
        Dim RowsInserted As Long
        Dim cmd As ULCommand = Conn.CreateCommand()
        cmd.CommandText = "INSERT INTO Names(name) VALUES (?)"
        cmd.Parameters.Add("", txtName.Text)
        RowsInserted = cmd.ExecuteNonQuery()
        cmd.Dispose()
        RefreshListBox()
    Catch
        MsgBox("Exception: " + Err.Description)
    End Try
  2. 在窗体设计选项卡上,双击 [Update] 以创建 btnUpdate_Click 方法。此方法执行以下任务:

    • 实例化一个 ULCommand 对象,并为其指派一个 UPDATE 语句,该语句会根据关联的 ID 将文本框中的值插入数据库。

    • 执行该语句。

    • 释放 ULCommand 对象。

    • 刷新列表框。

    • 如果出现错误,则输出错误消息。对于 SQL 错误,该代码还会输出错误代码。

      请参见错误消息

    对于 C#,请将以下代码添加到 btnUpdate_Click 方法中。

    //Visual C#
    try {
        long RowsUpdated;
        int updateID = ids[ lbNames.SelectedIndex ];
        using( ULCommand cmd = Conn.CreateCommand() ){
            cmd.CommandText = 
                "UPDATE Names SET name = ? WHERE id = ?" ;
            cmd.Parameters.Add("", txtName.Text );
            cmd.Parameters.Add("", updateID);
            RowsUpdated = cmd.ExecuteNonQuery();
        }
        RefreshListBox();
    }
    catch( Exception err ) {
        MessageBox.Show(
            "Exception: " + err.Message);
    }

    对于 Visual Basic,请将以下代码添加到 btnUpdate_Click 方法中。

    'Visual Basic
    Try
        Dim RowsUpdated As Long
        Dim updateID As Integer = ids(lbNames.SelectedIndex)
        Dim cmd As ULCommand = Conn.CreateCommand()
        cmd.CommandText = "UPDATE Names SET name = ? WHERE id = ?"
        cmd.Parameters.Add("", txtName.Text)
        cmd.Parameters.Add("", updateID)
        RowsUpdated = cmd.ExecuteNonQuery()
        cmd.Dispose()
        RefreshListBox()
    Catch
        MsgBox("Exception: " + Err.Description)
    End Try
  3. 在窗体设计选项卡上,双击 [Delete] 以创建 btnDelete_Click 方法。添加代码以执行以下任务:

    • 实例化一个 ULCommand 对象,并为其指派一个 DELETE 语句。该 DELETE 语句会根据整数数组 ID 中的关联 ID,从数据库中删除选定的行。

    • 执行该语句。

    • 释放 ULCommand 对象。

    • 刷新列表框。

    • 如果出现错误,则显示错误消息。对于 SQL 错误,该代码还会显示错误代码。

      请参见错误消息

    对于 C#,请将以下代码添加到 btnDelete_Click 方法中。

    //Visual C#
    try{
        long RowsDeleted;
        int deleteID = ids[lbNames.SelectedIndex];
        using( ULCommand cmd = Conn.CreateCommand() ){
            cmd.CommandText = 
                "DELETE From Names WHERE id = ?" ;
            cmd.Parameters.Add("", deleteID);
            RowsDeleted = cmd.ExecuteNonQuery ();
        }
        RefreshListBox();
    } 
    catch( Exception err ) { 
        MessageBox.Show("Exception: " + err.Message );
    }

    对于 Visual Basic,请将以下代码添加到 btnDelete_Click 方法中。

    'Visual Basic
    Try
        Dim RowsDeleted As Long
        Dim deleteID As Integer = ids(lbNames.SelectedIndex)
        Dim cmd As ULCommand = Conn.CreateCommand()
        cmd.CommandText = "DELETE From Names WHERE id = ?"
        cmd.Parameters.Add("", deleteID)
        RowsDeleted = cmd.ExecuteNonQuery()
        cmd.Dispose()
        RefreshListBox()
    Catch
        MsgBox("Exception: " + Err.Description)
    End Try
  4. 生成应用程序,确认其编译正确。