序列不连续
从我之前的问题我已经解决了如何显示信息来自 MS Access DB 文件。问题是,在我删除了一些用作样本尝试的条目后,序列号变得混乱,现在项目的 ID 乱序了。这是它现在的样子,下面是我的代码。
=== Magazine 1 ===
People
Times Inc.
4.95
19.95
=== Magazine 2 ===
Car and Driver
Hachetter Inc.
3.95
19.99
=== Magazine 7 ===
a
b
1
2
按钮事件(我怀疑是代码错误):
private void btnShowMags_Click(object sender, EventArgs e)
{
// Creating new instance of the DisplayMags form.
DisplayMags displayMags = new DisplayMags();
// find the path where the executable resides
string dbPath = Application.StartupPath;
// Providing a path to the MS Access file.
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
+ dbPath + @"\..\..\..\..\Magazines.mdb; User Id=admin; Password=";
// Creating a new connection and assigning it to a variable.
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
// Creating a new instance for a command which we will use later.
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
// declare and instantiate the command
OleDbCommand cmdMagazines = new OleDbCommand();
cmdMagazines.CommandText = "select * from magazine";
cmdMagazines.Connection = conn;
OleDbDataReader drMagazines;
try
{
// open the connection
conn.Open();
// retrieve data from the data source to the data reader
drMagazines = cmdMagazines.ExecuteReader();
if (drMagazines.HasRows)
{
while (drMagazines.Read())
{
displayMags.txtDisplayMags.Text += "=== Magazine " +
drMagazines.GetValue(0) + " ===" + Environment.NewLine +
drMagazines.GetValue(1) + Environment.NewLine +
drMagazines.GetValue(2) + Environment.NewLine +
drMagazines.GetValue(3) + Environment.NewLine +
drMagazines.GetValue(4) + Environment.NewLine +
Environment.NewLine;
}
}
}
catch (Exception ex)
{
// Displaying any errors that might have occured.
MessageBox.Show("Error opening the connection: " + ex.Message);
}
finally
{
// Closing connection after task was completed.
conn.Close();
}
// Displaying DisplayMags form, assuring that earlier form
// will not be accessible. Show() let us access all forms.
displayMags.ShowDialog();
}
如何使序列号按顺序出现?
编辑我将这样计算所有条目:
try
{
// open the connection
conn.Open();
// retrieve data from the data source to the data reader
drMagazines = cmdMagazines.ExecuteReader();
int i = 0;
if (drMagazines.HasRows)
{
while (drMagazines.Read())
{
i++;
displayMags.txtDisplayMags.Text += "=== Magazine " +
i + " ===" + Environment.NewLine +
drMagazines.GetValue(1) + Environment.NewLine +
drMagazines.GetValue(2) + Environment.NewLine +
drMagazines.GetValue(3) + " / issue" + Environment.NewLine +
drMagazines.GetValue(4) + " / year" + Environment.NewLine +
Environment.NewLine;
}
}
}
From my previous question I have solved how to display information from MS Access DB file. The problem is that the sequence number is messing up after I have deleted some entries which I used as sample tries, and now the ID of items are out of order. Here is what it looks now, and following by code I have.
=== Magazine 1 ===
People
Times Inc.
4.95
19.95
=== Magazine 2 ===
Car and Driver
Hachetter Inc.
3.95
19.99
=== Magazine 7 ===
a
b
1
2
Button event (I doubt it's the codes fault):
private void btnShowMags_Click(object sender, EventArgs e)
{
// Creating new instance of the DisplayMags form.
DisplayMags displayMags = new DisplayMags();
// find the path where the executable resides
string dbPath = Application.StartupPath;
// Providing a path to the MS Access file.
string connString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
+ dbPath + @"\..\..\..\..\Magazines.mdb; User Id=admin; Password=";
// Creating a new connection and assigning it to a variable.
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = connString;
// Creating a new instance for a command which we will use later.
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
// declare and instantiate the command
OleDbCommand cmdMagazines = new OleDbCommand();
cmdMagazines.CommandText = "select * from magazine";
cmdMagazines.Connection = conn;
OleDbDataReader drMagazines;
try
{
// open the connection
conn.Open();
// retrieve data from the data source to the data reader
drMagazines = cmdMagazines.ExecuteReader();
if (drMagazines.HasRows)
{
while (drMagazines.Read())
{
displayMags.txtDisplayMags.Text += "=== Magazine " +
drMagazines.GetValue(0) + " ===" + Environment.NewLine +
drMagazines.GetValue(1) + Environment.NewLine +
drMagazines.GetValue(2) + Environment.NewLine +
drMagazines.GetValue(3) + Environment.NewLine +
drMagazines.GetValue(4) + Environment.NewLine +
Environment.NewLine;
}
}
}
catch (Exception ex)
{
// Displaying any errors that might have occured.
MessageBox.Show("Error opening the connection: " + ex.Message);
}
finally
{
// Closing connection after task was completed.
conn.Close();
}
// Displaying DisplayMags form, assuring that earlier form
// will not be accessible. Show() let us access all forms.
displayMags.ShowDialog();
}
How can I make the sequence number to appear in order?
EDIT I will count all entries this way:
try
{
// open the connection
conn.Open();
// retrieve data from the data source to the data reader
drMagazines = cmdMagazines.ExecuteReader();
int i = 0;
if (drMagazines.HasRows)
{
while (drMagazines.Read())
{
i++;
displayMags.txtDisplayMags.Text += "=== Magazine " +
i + " ===" + Environment.NewLine +
drMagazines.GetValue(1) + Environment.NewLine +
drMagazines.GetValue(2) + Environment.NewLine +
drMagazines.GetValue(3) + " / issue" + Environment.NewLine +
drMagazines.GetValue(4) + " / year" + Environment.NewLine +
Environment.NewLine;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们不是“序列号”而是“主键”。
键永远不应该改变,所以是的,删除行会产生间隙。没有内置的重新编号机制。
They are not "sequence numbers" but "primary keys".
Keys should never change so Yes, deleting rows will produce gaps. There is no built-in mechanism to renumber.
将 SQL 从: 更改
为:
Change your SQL from:
To: