OleDb 数据库到 DataSet 并在 C# 中返回?

发布于 2024-08-26 06:19:44 字数 957 浏览 11 评论 0原文

我正在编写一个程序,允许用户:

  1. 连接到用户指定的(任意)数据库
  2. 在单独的 DataGridView 中查看该数据库中的所有表
  3. 在程序中编辑它们,生成随机数据,然后查看结果
  4. 选择提交因此

,我发现了 DataSet 类,它看起来能够保存数据库所需的所有内容,并且我认为这里最好的做法是将所有内容加载到一个数据集中,让用户对其进行编辑,然后然后将数据集保存回数据库。问题是,我能找到的加载数据库表的唯一方法是:

set = new DataSet();
DataTable schema = connection.GetOleDbSchemaTable(
    OleDbSchemaGuid.Tables,
    new string[] { null, null, null, "TABLE" });
foreach (DataRow row in schema.Rows)
{
    string tableName = row.Field<string>("TABLE_NAME");
    DataTable dTable = new DataTable();
    new OleDbDataAdapter("SELECT * FROM " + tableName, connection).Fill(dTable);
    dTable.TableName = tableName;
    set.Tables.Add(dTable);
}

虽然似乎应该有一种更简单的方法,因为数据集似乎正是为此目的而设计的。但真正的问题是当我尝试保存这些东西时。为了使用 OleDbDataAdapter.Update() 方法,我被告知必须提供有效的 INSERT 查询。这不是否定了让一个类为我处理这些东西的全部意义吗?

不管怎样,我希望有人可以解释如何将数据库加载和保存到数据集中,或者让我更好地了解如何做我想做的事情。我总是可以自己解析这些命令,但这似乎不是最好的解决方案。

I'm writing a program that lets a user:

  1. Connect to an (arbitrary) database that the user specifies
  2. View all of the tables in that database in separate DataGridViews
  3. Edit them in the program, generate random data, and see the results
  4. Choose to commit those changes or revert

So I discovered the DataSet class, which looks like it's capable of holding everything that a database would, and I decided that the best thing to do here would be to load everything into one dataset, let the user edit it, and then save the dataset back to the database. The problem is that the only way I can find to load the database tables is this:

set = new DataSet();
DataTable schema = connection.GetOleDbSchemaTable(
    OleDbSchemaGuid.Tables,
    new string[] { null, null, null, "TABLE" });
foreach (DataRow row in schema.Rows)
{
    string tableName = row.Field<string>("TABLE_NAME");
    DataTable dTable = new DataTable();
    new OleDbDataAdapter("SELECT * FROM " + tableName, connection).Fill(dTable);
    dTable.TableName = tableName;
    set.Tables.Add(dTable);
}

while it seems like there should be a simpler way given that datasets appear to be designed for exactly this purpose. The real problem though is when I try saving these things. In order to use the OleDbDataAdapter.Update() method, I'm told that I have to provide valid INSERT queries. Doesn't that kind of negate the whole point of having a class to handle this stuff for me?

Anyway, I'm hoping somebody can either explain how to load and save a database into a dataset or maybe give me a better idea of how to do what I'm trying to do. I could always parse the commands together myself, but that doesn't seem like the best solution.

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

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

发布评论

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

评论(1

小梨窩很甜 2024-09-02 06:19:44

您可以创建一个OleDbAdapter,然后配置它来处理记录,但是您应该给出Insert、Update和Delete语句,因为DataSet本身不知道数据来自哪里;它可以是 Access 数据库、Xml 或文本文件。

会是这样的(显然没有测试过)

DataSet ds = new DataSet("myData");
var da = new OleDbDataAdapter("SELECT * FROM employee", connection);
// filling your dataset
da.Fill(ds);

// Setting up the dataAdapter, could be a stored procedure
da.InsertCommand = new OleDbCommand("INSERT employee (id, name) VALUES (@id, @name)");
da.UpdateCommand = new OleDbCommand("UPDATE employee SET name = @name WHERE id = @id");

// Updating Database from data on DataSet
da.Update(ds);

You can create an OleDbAdapter and then configure it to treat the records, but you should give the Insert, Update and Delete statements, because the DataSet per se doesn't know where the data comes from; it could be a Access Database or a Xml or text file.

It would be something like this (not tested obviously)

DataSet ds = new DataSet("myData");
var da = new OleDbDataAdapter("SELECT * FROM employee", connection);
// filling your dataset
da.Fill(ds);

// Setting up the dataAdapter, could be a stored procedure
da.InsertCommand = new OleDbCommand("INSERT employee (id, name) VALUES (@id, @name)");
da.UpdateCommand = new OleDbCommand("UPDATE employee SET name = @name WHERE id = @id");

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