1

Access データベースに接続してレコードを表示する単純なフォームがあります。このコードは何が起こっているかを示しています。私は正しく宣言していない変数と関係があると思いますか?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace MediaPlayer
{
public partial class Media : Form
{

    // Use this connection string if your database has the extension .accdb
    private const String access7ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MediaDatabase.accdb";
    // Use this connection string if your database has the extension .mdb
    private const String access2003ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\MediaDatabase.mdb";

    // Data components
    private OleDbConnection myConnection;
    private DataTable myDataTable;
    private OleDbDataAdapter myAdapter;
    private OleDbCommandBuilder myCommandBuilder;

    // Index of the current record
    private int currentRecord = 0;

    private void FillDataTable(String selectCommand)
    {
        try
        {
            myConnection.Open();
            myAdapter.SelectCommand.CommandText = selectCommand;
            // Fill the datatable with the rows reurned by the select command
            myAdapter.Fill(myDataTable);
            myConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
        }
    }

    private void DisplayRow(int rowIndex)
    {
        // Check that we can retrieve the given row
        if (myDataTable.Rows.Count == 0)
            return; // nothing to display
        if (rowIndex >= myDataTable.Rows.Count)
            return; // the index is out of range

        // If we get this far then we can retrieve the data
        try
        {
            DataRow row = myDataTable.Rows[rowIndex];
            textBox1.Text = row["FilePath"].ToString();
            textBox2.Text = row["Subject"].ToString();
            textBox3.Text = row["Title"].ToString();
            textBox4.Text = row["Keywords"].ToString();
            textBox5.Text = row["MediaType"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message);
        }

    }



    public Media()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        {
            String command = "SELECT * FROM Media";
            try
            {
                myConnection = new OleDbConnection(access7ConnectionString);
                myAdapter = new OleDbDataAdapter(access7ConnectionString, myConnection);
                myCommandBuilder = new OleDbCommandBuilder(myAdapter);
                myDataTable = new DataTable();
                FillDataTable(command);
                DisplayRow(currentRecord);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

    }

    int m_rowPosition = 0; 

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        //if not at the first row, go back one row and show the record.
        if (m_rowPosition !=0)
        {
            m_rowPosition---;
            this.DisplayRow();
        }
    }

    private void button6_Click(object sender, EventArgs e)
    {
        //move to first row of data and show data
        m_rowPosition = 0;
        this.DisplayRow();
    }
}

}

4

2 に答える 2

1

DisplayRowメソッドを呼び出すときにこれを実行したいことがわかった場合:

DisplayRow(m_rowPosition);

または、メソッドを次のように変更できます。

private void DisplayRow()
{
    // Check that we can retrieve the given row
    if (myDataTable.Rows.Count == 0)
        return; // nothing to display
    if (rowIndex >= myDataTable.Rows.Count)
        return; // the index is out of range

    // If we get this far then we can retrieve the data
    try
    {
        DataRow row = myDataTable.Rows[m_rowPosition];//<- here you using index which value is changed on button click
        textBox1.Text = row["FilePath"].ToString();
        textBox2.Text = row["Subject"].ToString();
        textBox3.Text = row["Title"].ToString();
        textBox4.Text = row["Keywords"].ToString();
        textBox5.Text = row["MediaType"].ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error in DisplayRow : \r\n" + ex.Message);
    }

}
于 2012-05-09T09:10:55.947 に答える
0

これはで議論されています

C# アプリケーションでボタンが機能しないのはなぜですか

于 2012-05-09T12:05:30.450 に答える