为什么我的数据库导航按钮在c#中不起作用?
我有一个简单的表单,它连接到access数据库并显示记录,这段代码显示了发生了什么,为什么我试图编码的按钮不起作用?我猜这与我没有正确解释的变量有关为什么我的数据库导航按钮在c#中不起作用?,c#,winforms,methods,parameters,dataset,C#,Winforms,Methods,Parameters,Dataset,我有一个简单的表单,它连接到access数据库并显示记录,这段代码显示了发生了什么,为什么我试图编码的按钮不起作用?我猜这与我没有正确解释的变量有关 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Window
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方法时希望这样做:
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);
}
}
private void DisplayRow()
{
//检查是否可以检索给定的行
if(myDataTable.Rows.Count==0)
return;//不显示任何内容
if(rowIndex>=myDataTable.Rows.Count)
return;//索引超出范围
//如果我们走到这一步,那么我们可以检索数据
尝试
{
DataRow row=myDataTable.Rows[m_rowPosition];//如果我理解您在调用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);
}
}
private void DisplayRow()
{
//检查是否可以检索给定的行
if(myDataTable.Rows.Count==0)
return;//不显示任何内容
if(rowIndex>=myDataTable.Rows.Count)
return;//索引超出范围
//如果我们走到这一步,那么我们可以检索数据
尝试
{
DataRow row=myDataTable.Rows[m_rowPosition];//这将在中讨论
这一点正在中讨论
您是否看到错误?方法“DisplayRow”没有重载接受0个参数C:\Users\Max\Documents\Visual Studio 2010\Projects\MediaPlayer\MediaPlayer\Media.cs 121 13 MediaPlayer您的方法接受一个参数,您需要传递要显示的行。Brillaint,我该怎么做?您需要这个。DisplayRow(m\u rowPosition);您看到错误了吗?方法“DisplayRow”没有重载接受0个参数C:\Users\Max\Documents\Visual Studio 2010\Projects\MediaPlayer\MediaPlayer\Media.cs 121 13 MediaPlayer您的方法接受一个参数,您需要传递要显示的行。Brillaint,我该怎么做?您需要这个。DisplayRow(m\u rowPosition);我正在使用DisplayRow(m_rowPosition);它已经停止了错误,现在为了让它移动,我需要检查它右边的记录?抱歉,它没有显示太多错误,只是没有发生任何事情它不会移动到下一个记录?如果(m_rowPositionm_rowPosition=0;
更改为m_rowPosition++
。请注意,这会起作用,并会将用户带回第一行,对不起,但最后一个问题是,正确的语法是什么,它会移动到下一行?'code'if(m_rowPositionm_rowPosition=0;
更改为m_rowPosition++
。这很好,会让用户回到第一行,抱歉,但最后一个问题是正确的语法是什么,这样它会移动到下一行?'code'if(m_rowPosition