修改数据表

发布于 2024-08-02 04:12:46 字数 1858 浏览 2 评论 0原文

情况:

您好! 我正在尝试使用 MS Access 数据库填充 WPF 工具包 DataGrid。

这是我现在所拥有的(它可以工作):

//Load the datagrid with the database
    private void LoadDataGrid(string filename, string path)
    {
        string databaseConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                              "Data Source=" + path + "\\" + filename,
               tableName ="";
        OleDbConnection conn = null;
        DataTable schemaTable,
                  table = new DataTable();

        try
        {
            conn = new OleDbConnection(databaseConn);
            try
            {
                conn.Open();
                schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                                                       new object[] { null, null, null, "TABLE" });
                tableName = "[" + schemaTable.Rows[0].ItemArray[2].ToString() + "];";
                string sqlQuery = "SELECT * FROM " + tableName;
                OleDbCommand command = new OleDbCommand(sqlQuery, conn);
                OleDbDataReader reader;
                reader = command.ExecuteReader();
                table.Load(reader);
                DataGrid_.ItemsSource = table.DefaultView;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message); 
            }
            finally
            {
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message); 
        }
    }

上面的代码示例在 MS Access 数据库的帮助下加载 WPF 工具包 DataGrid。

我想做的是能够在一开始就在 DataGrid 中插入一列。 该列将用于写入行号。 我认为可行的是修改 table 变量(这是一个 DataTable 对象)。


问题:

那么,如何在 table 变量中插入一列,为该新列中的每行添加行号,并将数据库中的所有数据存储在 DataGrid 中?

Situation:

Hello! I am trying to populate a WPF toolkit DataGrid with a MS Access database.

Here is what I have right now (it works):

//Load the datagrid with the database
    private void LoadDataGrid(string filename, string path)
    {
        string databaseConn = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                              "Data Source=" + path + "\\" + filename,
               tableName ="";
        OleDbConnection conn = null;
        DataTable schemaTable,
                  table = new DataTable();

        try
        {
            conn = new OleDbConnection(databaseConn);
            try
            {
                conn.Open();
                schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
                                                       new object[] { null, null, null, "TABLE" });
                tableName = "[" + schemaTable.Rows[0].ItemArray[2].ToString() + "];";
                string sqlQuery = "SELECT * FROM " + tableName;
                OleDbCommand command = new OleDbCommand(sqlQuery, conn);
                OleDbDataReader reader;
                reader = command.ExecuteReader();
                table.Load(reader);
                DataGrid_.ItemsSource = table.DefaultView;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message); 
            }
            finally
            {
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message); 
        }
    }

The code sample above loads a WPF toolkit DataGrid with the help of a MS Access database.

What I would like to do is be able to insert a column in the DataGrid at the very beginning. This column would be used to write the row number. What I think could work is to modify the table variable (which is a DataTable object).


Question:

So, how can I insert a column in the table variable, add the row number for each rows in that new column, and have all the data from the database in the DataGrid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望笑 2024-08-09 04:12:46

另一种方法是在将 IDataReader 加载到 DataTable 中之前先在 DataTable 上创建一个列。


// the rest of your code
//
DataTable table = new DataTable();
DataColumn col = table.Columns.Add("RowNumber", typeof(int));
col.AutoIncrementSeed = 1;
col.AutoIncrement = true;

//
// the rest of your code
//

table.Load(reader)
//
// the rest of your code

下面的代码片段演示了问题上下文之外的技术


//Simulates data coming from a database or another data source
DataTable origin = new DataTable(); 
DataColumnCollection columns = origin.Columns; 
columns.Add("Id", typeof(int)); 
columns.Add("Name", typeof(string)); 
origin.Rows.Add(55, "Foo"); 
origin.Rows.Add(14, "Bar"); 
IDataReader reader = origin.CreateDataReader();

DataTable table = new DataTable(); 

//Sets up your target table to include a new column for displaying row numbers
//These are the three lines that make it all happen.
DataColumn col = table.Columns.Add("RowNumber", typeof(int)); 
col.AutoIncrementSeed = 1; 
col.AutoIncrement = true; 

//Simulates loading data from the database
table.Load(reader); 

// Examine table through the debugger. Is will have the contents of "origin" with the column "RowNumber" prepended

An alternative is to create a column on the DataTable before loading the IDataReader into it.


// the rest of your code
//
DataTable table = new DataTable();
DataColumn col = table.Columns.Add("RowNumber", typeof(int));
col.AutoIncrementSeed = 1;
col.AutoIncrement = true;

//
// the rest of your code
//

table.Load(reader)
//
// the rest of your code

The code snippet bellow demonstrates the technique out of the question's context


//Simulates data coming from a database or another data source
DataTable origin = new DataTable(); 
DataColumnCollection columns = origin.Columns; 
columns.Add("Id", typeof(int)); 
columns.Add("Name", typeof(string)); 
origin.Rows.Add(55, "Foo"); 
origin.Rows.Add(14, "Bar"); 
IDataReader reader = origin.CreateDataReader();

DataTable table = new DataTable(); 

//Sets up your target table to include a new column for displaying row numbers
//These are the three lines that make it all happen.
DataColumn col = table.Columns.Add("RowNumber", typeof(int)); 
col.AutoIncrementSeed = 1; 
col.AutoIncrement = true; 

//Simulates loading data from the database
table.Load(reader); 

// Examine table through the debugger. Is will have the contents of "origin" with the column "RowNumber" prepended
奶茶白久 2024-08-09 04:12:46

最简单的解决方案是修改代码以在原始 SELECT 查询中包含“虚拟”RowNumber 字段,如下所示:

SELECT ROWNUM AS ROWNUMBER, * FROM TABLE1

不幸的是,Access 没有类似 ROWNUM 函数的功能,因此我认为最简单的解决方案是添加 RowNumber SELECT 查询中的列如下所示:

SELECT 0 AS ROWNUMBER, * FROM TABLE1

它将在开头添加一个包含全零的列,然后迭代生成的 DataTable 并设置行号,如下所示:

int rownumber = 1;
foreach (DataRow row in table.Rows)
{
    row["ROWNUMBER"] = rownumber;
    rownumber++;
}

然后将 DataTable 转储到网格中。

Your easiest solution would be to modify your code to include a "virtual" RowNumber field in your original SELECT query, like so:

SELECT ROWNUM AS ROWNUMBER, * FROM TABLE1

Unfortunately, Access doesn't have anything like a ROWNUM function, so I think the easiest solution is to add a RowNumber column in the SELECT query like this:

SELECT 0 AS ROWNUMBER, * FROM TABLE1

which will add a column containing all zeroes at the beginning, and then iterate through the resulting DataTable and set the row number, like this:

int rownumber = 1;
foreach (DataRow row in table.Rows)
{
    row["ROWNUMBER"] = rownumber;
    rownumber++;
}

and then dump the DataTable into the grid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文