任何人都可以建议一种在 .NET Web 应用程序中使用 CSV 文件上传来动态创建 SQL 表的简单方法吗?

发布于 2024-08-11 04:34:40 字数 209 浏览 10 评论 0原文

我已经检查了一些答案,但无法找到有关如何使用用户通过 CSV 文件上传的自定义元数据(或 CSV 文件中的列)创建 SQL 表的具体详细信息。 www.dabbledb.com 提供的一项功能,例如为自定义信息创建在线数据库。 它将是一个基于 Web 的 .NET 应用程序,使用 SQL Server 2005 或 2008 作为后端数据库。

期待您的来信。

谢谢 SA

I have checked a few answers but was unable to find specific details on how we can create a SQL table using custom metadata (or Columns from a CSV file) which is uploaded by a user through a CSV file.
A feature available with www.dabbledb.com, like creating online database for custom information.
It will be a web based .NET application using SQL Server 2005 or 2008 as the backend db.

Look forward to hearing from you.

Thank you
SA

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

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

发布评论

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

评论(4

阳光下的泡沫是彩色的 2024-08-18 04:34:40

我最近在做一些非常接近您的要求的事情。就我而言,上传到服务器的 CSV 文件中每一行的数据都存储为“blob”。我使用了 Lumen Works CSV reader 来解析上传的 CSV 文件。它能够读取列标题,您可以去除其中的空格,也可以读取 SQL 数据库表列。

希望这有帮助
印地弗罗兹

I recently worked on something quite close to your requirements. In my case, the data from each row in a CSV file that is uploaded in to the server is stored as a 'blob'. I have used the Lumen Works CSV reader to parse the uploaded CSV file. It has the ability to read the column headers, which you can strip of whitespaces and may to SQL database table columns.

Hope this helps
indyfromoz

淡淡的优雅 2024-08-18 04:34:40

如果您使用的是实体属性值数据库,那么您只需在链接到实体的主属性表中添加新列,然后只需在实体的属性值表中分配值。

但是,如果这没有意义,那么您将需要使用 ALTER TABLE 命令并添加新列,但如果您要动态更改表结构,那么您可能需要考虑EAV 数据库结构。

If you are using an entity-attribute-value database then you can just add the new columns in the master attribute table, linked to the entity, then just assign values in the attribute-value table for the entity.

But, if that doesn't make sense then you will want to use the ALTER TABLE command and add new columns, but if you are going to be dynamically changing the table structure then you may want to consider an EAV database structure.

冷…雨湿花 2024-08-18 04:34:40

我们刚刚编写了一个 Perl 程序,它读取 csv,分析每一列中的数据,首先尝试相信第一行是列名称,然后拼凑出一个创建表语句和一堆 0f 插入。我们可以从命令环境或 PHP 内部的系统调用运行它。

We just wrote a perl program that reads the csv, analyzes the data in each column, tries at first to believe that the first line is column names, then cobbles up a create table statement and a bunch 0f inserts. We can run it from a command environment or a system call inside PHP.

Oo萌小芽oO 2024-08-18 04:34:40

这只是我如何执行此操作的一个示例(这意味着它可能充满错误并且错误......)

CSV 文件:

Column:ColumnType:size:n, Column:Type:size:n, Column:Type:size:y
C1Data, C2Data, C3Data

伪代码:

StringBuilder sql = new StringBuilder();
sb.append("CREATE TABLE " + someTableName+ "(" + Environment.NewLine);
for(string s in csvlines) {
 if(first_line) {

    // pull apart our columns
    string[] cols = s.Split(new char[] { ',' });

    // iterate over the list of columns
    for(string col in cols) {

      // pull apart the column name, type, size and nullable flag
      string[] ci = col.Split(new char[] { ':' });

      // add our column name and add the datatype
      sb.Append("[" + ci[0] + "]" + " [" + ci[1] + "] ");

      // only put a size if value greater than 0
      if(ci[2] != "0") {
        sb.Append("(" + ci[2] + ") ");
      }

      // put our NULLABLE flag on NULL columns only
      if(ci[3] == "y") {
        sb.Append("NULL," + Environment.NewLine);
      } else {
        sb.Append("NOT NULL," + Environment.NewLine);
      }
    }

    // remember the above has a bug where the last column has a rouge ,
    sb.Append(")");
 } else  {
     // create our insert statement and insert as appropriate
 }
}

可能很恶心,可能很脏,可能只是做你需要的!

This is just an example how I would do this (which means it possibly is bug ridden and wrong...)

CSV File:

Column:ColumnType:size:n, Column:Type:size:n, Column:Type:size:y
C1Data, C2Data, C3Data

Pseudo Code:

StringBuilder sql = new StringBuilder();
sb.append("CREATE TABLE " + someTableName+ "(" + Environment.NewLine);
for(string s in csvlines) {
 if(first_line) {

    // pull apart our columns
    string[] cols = s.Split(new char[] { ',' });

    // iterate over the list of columns
    for(string col in cols) {

      // pull apart the column name, type, size and nullable flag
      string[] ci = col.Split(new char[] { ':' });

      // add our column name and add the datatype
      sb.Append("[" + ci[0] + "]" + " [" + ci[1] + "] ");

      // only put a size if value greater than 0
      if(ci[2] != "0") {
        sb.Append("(" + ci[2] + ") ");
      }

      // put our NULLABLE flag on NULL columns only
      if(ci[3] == "y") {
        sb.Append("NULL," + Environment.NewLine);
      } else {
        sb.Append("NOT NULL," + Environment.NewLine);
      }
    }

    // remember the above has a bug where the last column has a rouge ,
    sb.Append(")");
 } else  {
     // create our insert statement and insert as appropriate
 }
}

May be disgusting, maybe dirty, may just do what you require!

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