You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TableAdapter实现数据库记录计数的问题求助

Fixing Record Count & Insert Functionality in Your C# Database Project

Hey Rick, let's break down why your record count code isn't working and get that friend addition feature up and running smoothly!

The Core Issue: TableAdapter vs. DataTable

Your main problem is that FriendsTableTableAdapter doesn't have a Rows property—that property belongs to the DataTable that the adapter fetches from the database. When you call FTTA.Rows.Count(), you're trying to access a member that doesn't exist on the adapter itself, which is why it's failing silently (or throwing an exception that your generic catch block is hiding).

Fixed Code Example

Here's how to adjust your code to correctly get the record count, plus some improvements for robustness:

private void AddButton_Click(object sender, EventArgs e) 
{ 
    int RecordCount = 0; 
    try 
    { 
        // Use 'using' to ensure the TableAdapter is properly disposed
        using (vriendenkringDataSetTableAdapters.FriendsTableTableAdapter FTTA = 
               new vriendenkringDataSetTableAdapters.FriendsTableTableAdapter())
        {
            // Get the full dataset from the database to count rows
            vriendenkringDataSet.FriendsTableDataTable friendTable = FTTA.GetData();
            RecordCount = friendTable.Rows.Count;

            // Insert the new friend (simplified boolean conversion)
            FTTA.Insert(RecordCount, NicknameTextBox.Text, BirthdayTimePicker.Value, 
                       EmailTextbox.Text, 100, true); 
            
            MessageBox.Show($"Friend added successfully! Total records now: {RecordCount + 1}");
        }
    } 
    catch (Exception ex) 
    { 
        // Catch specific exceptions to see exactly what went wrong
        MessageBox.Show($"Something went wrong: {ex.Message}"); 
        MessageBox.Show($"Record count at time of error: {RecordCount}"); 
    } 
}

Key Improvements & Notes

  • Proper Record Count: We use FTTA.GetData() to fetch the full DataTable, then access its Rows.Count property to get the current number of records.
  • Resource Management: The using statement ensures the TableAdapter is cleaned up after use, preventing memory leaks.
  • Better Error Debugging: By catching Exception ex, you'll get a specific error message (like duplicate primary key, invalid email format, etc.) instead of a generic message—this is critical for troubleshooting.
  • Avoid Primary Key Conflicts: Right now you're using RecordCount as the primary key for the new friend. If multiple users (or even multiple quick clicks) happen at the same time, this will cause duplicate key errors. A better approach is to set your database's FriendsTable primary key as an auto-increment identity column:
    1. In your database, set the primary key column's identity property to "Yes" (auto-increment).
    2. Update your DataSet to reflect this—set the column's AutoIncrement property to true.
    3. You can then remove the RecordCount parameter from your FTTA.Insert() call, since the database will handle generating the unique key automatically.

内容的提问来源于stack exchange,提问作者Rick Smeenk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:25:50