c# - 序列不连续

标签 c# ms-access string-formatting oledbdatareader

来 self 之前的question我已经解决了如何显示 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;
                }
            }
        }

最佳答案

它们不是“序列号”而是“主键”。

键永远不应该改变,所以是的,删除行会产生间隙。没有内置的重新编号机制。

关于c# - 序列不连续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193207/

相关文章:

c# - "Parameter is not valid"System.Drawing.Image.FromStream() 方法异常

C# .NET 如何在不使用 StringBuilder.ToString() 的情况下将 StringBuilder 转换为字节数组

VBA 如果错误重试

c# - 错误 :Could not find installable ISAM

wpf - 如何编辑使用多重绑定(bind)和 string.format 的 WPF 文本框?

c# - 为什么两个新对象没有相同的哈希码?

c# - 使用 Linq 遍历 ConfigurationManager.ConnectionStrings?

sqlite - 无法通过 ODBC 连接 Access 到 SQLite

python - 从列表到元组

C# - 在字符串中插入可变数量的空格? (格式化输出文件)