【问题标题】:Why are my database navigation buttons not working in c#?为什么我的数据库导航按钮在 C# 中不起作用?
【发布时间】:2012-05-17 18:45:15
【问题描述】:

我有一个连接到访问数据库并显示记录的简单表单,此代码显示发生了什么,为什么我尝试编写代码的按钮不起作用?我猜它与我没有正确清除的变量有关?

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

}

【问题讨论】:

  • 您看到错误了吗?
  • “DisplayRow”方法没有重载需要 0 个参数 C:\Users\Max\Documents\Visual Studio 2010\Projects\MediaPlayer\MediaPlayer\Media.cs 121 13 MediaPlayer
  • 你的方法需要一个参数来传递你想要显示的行。
  • Brillaint,我该怎么办?
  • 你需要这个.DisplayRow(m_rowPosition);

标签: c# winforms methods parameters dataset


【解决方案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);
    }

}

【讨论】:

  • 我正在使用 DisplayRow(m_rowPosition);它已经停止了错误,现在为了让它移动我需要检查它的正确记录?
  • 对不起,它没有显示太多错误,只是什么也没发生,它不会移动到下一条记录?这就是我正在尝试的 if (m_rowPosition
  • 好的,我明白了。在 button6 点击方法中将m_rowPosition = 0; 更改为m_rowPosition++;
  • Brillaint 这行得通,并将用户带回第一行,对不起,最后一个问题是什么是正确的语法,所以它移动到下一行?
  • 'code' if (m_rowPosition
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多