0

我有一个链接到数据库的表单,并且控制表单的按钮不起作用,我没有收到任何错误,只是没有发生任何事情。

DisplayRow 类

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

    }

应该移动到下一条记录的按钮

private void button1_Click(object sender, EventArgs e)
    {
        if (currentRecord < myDataTable.Rows.Count -1)
        {
            currentRecord++;
            DisplayRow(currentRecord);

        }

但就像我说的,当我运行应用程序时,什么都没有发生,没有错误,什么都没有。

编辑:按照要求为 MyDataTable 编码

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

        }

我正在运行的完整代码..

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 DataTable myDataTable;

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

    private void FillDataTable(string selectCommand)
    {
        currentRecord = 0;
        OleDbConnection myConnection = new OleDbConnection(access7ConnectionString);
        OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection);
        myDataTable = new DataTable();


        try
        {
            myConnection.Open();
            myAdapter.SelectCommand.CommandText = selectCommand;
            myAdapter.Fill(myDataTable);
            myConnection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
        }
        DisplayRow(currentRecord);

    }

    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)
            //resets the index to 0 when you get past the last record
            rowIndex = 0;
        //if rowIndex is less then 0 set it to the last row
        if (rowIndex < 0)
            rowIndex = myDataTable.Rows.Count - 1;


        // 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();
        string command = "SELECT * FROM Media";
        //the try catch is in the FillDataTable method
        FillDataTable(command);
    }




    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        //assuming this resets the data
        currentRecord = 0;
        this.DisplayRow(currentRecord);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }
}

}

4

4 回答 4

1

放置一个调试器书签private void DisplayRow(int rowIndex),看看在调试应用程序时书签是否突出显示。如果不是,则检查您的调用显示行的循环是否正确

于 2012-05-09T10:27:49.920 回答
0

问题是你的编号。我假设您的 currentRecord 以 1、2、3 等开头,如果您需要使用这种情况

if (currentRecord <= myDataTable.Rows.Count -1) 

这样,当您的 currentRecord 为 1 时,它将小于或等于 (totalcount 2 - 1 = 1)

于 2012-05-09T10:55:39.040 回答
0

在这里,我为您写了整个事情,正如在另一个线程中讨论的那样。希望它有效。

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 DataTable myDataTable;

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

private void FillDataTable(string selectCommand)
{
    currentRecord=0;    
    OleDbConnection myConnection = new OleDbConnection(access7ConnectionString);
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection);
    myDataTable = new DataTable();


    try
    {
        myConnection.Open();
        myAdapter.SelectCommand.CommandText = selectCommand;
        myAdapter.Fill(myDataTable);
        myConnection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
    }
    DisplayRow(currentRecord);

}

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)
        //resets the index to 0 when you get past the last record
        rowIndex=0;
    //if rowIndex is less then 0 set it to the last row
    if (rowIndex<0) 
        rowIndex = myDataTable.Rows.Count-1;


    // 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();
    string command = "SELECT * FROM Media";
    //the try catch is in the FillDataTable method
    FillDataTable(command);
}




private void label2_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
   //assuming this cycles through the data
    currentRecord++;
    DisplayRow(currentRecord);
}

private void button6_Click(object sender, EventArgs e)
{
   //assuming this resets the data
    currentRecord=0;
    this.DisplayRow(currentRecord);
}
}
于 2012-05-09T11:11:35.757 回答
0

我添加了一堆 MessageBox 来帮助调试,试试这个并告诉我你得到了什么消息?

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 DataTable myDataTable;

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

    private void FillDataTable(string selectCommand)
    {
        currentRecord = 0;
        OleDbConnection myConnection = new OleDbConnection(access7ConnectionString);
        OleDbDataAdapter myAdapter = new OleDbDataAdapter(selectCommand, myConnection);
        myDataTable = new DataTable();
        try
        {
            myConnection.Open();
            myAdapter.SelectCommand.CommandText = selectCommand;
            myAdapter.Fill(myDataTable);
            myConnection.Close();
            MessageBox.Show("We filled the dataTable");
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error in FillDataTable : \r\n" + ex.Message);
        }
        MessageBox.Show(myDataTable.Rows.Count.ToString());
        DisplayRow(currentRecord);

    }

    private void DisplayRow(int rowIndex)
    {
        // Check that we can retrieve the given row
        if (myDataTable.Rows.Count == 0)
        {
            MessageBox.Show("No rows to Display");
            return; // nothing to display
        }
        if (rowIndex >= myDataTable.Rows.Count)
            //resets the index to 0 when you get past the last record
            rowIndex = 0;
        //if rowIndex is less then 0 set it to the last row
        if (rowIndex < 0)
            rowIndex = myDataTable.Rows.Count - 1;


        // 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();
        string command = "SELECT * FROM Media";
        //the try catch is in the FillDataTable method
        FillDataTable(command);
    }


    private void button2_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        //assuming this resets the data
        currentRecord = 0;
        this.DisplayRow(currentRecord);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //assuming this cycles through the data
        currentRecord++;
        DisplayRow(currentRecord);
    }
}

}

于 2012-05-09T12:45:07.890 回答