读取并检查.csv文件,然后将其上传到sql server 2008 r2

发布于 2024-09-25 07:58:09 字数 221 浏览 4 评论 0原文

我有一个页面,其中有一个文件上传选项,我必须在其中上传/导入 csv 文件。我想做的是检查上传的文件格式是否正确,例如如果上传了 csv 文件以外的任何文件,系统应该给出错误消息。另外,我需要做的是检查 csv 文件的某些字段,例如 csv 文件中应该有一些必填字段,例如名称、邮政编码,如何检查这些字段是否不为空。执行这些任务后,系统应自动将 csv 文件上传到 sql sever 2008。任何想法或教程...将不胜感激。

I have got a page in which there is a file upload option where I have to upload/import the csv file. What I want to do is to check if the correct format of file is uploaded for instance if any other than csv file is uploaded, the system should give an error message. Also what I need to do is to check certain fields of the csv file for instance there are some mandatory fields in the csv file which should be there like name , postcode, How can I check that these fields are not empty . After performing these task, the system should automatically upload the csv file onto the sql sever 2008. Any ideas or tutorial ... will be highly appreciated.

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

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

发布评论

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

评论(3

南汐寒笙箫 2024-10-02 07:58:09

查看此线程:

Parsing CSV files in C#, with header

我相信这会让您可以根据需要提取它,然后轻松地将其推入 SQL Server 中的表中。

Check out this thread:

Parsing CSV files in C#, with header

I believe this will let you extract it as you want and then easily shove it into a table in SQL Server.

冧九 2024-10-02 07:58:09

尝试使用Oledb。这个codenippet可以帮助您将csv文件读入数据表中,该数据表应该很容易转储到数据库中。

public static DataTable ParseCSV(string path)
{
  if (!File.Exists(path))
    return null;

  string full = Path.GetFullPath(path);
  string file = Path.GetFileName(full);
  string dir = Path.GetDirectoryName(full);

  //create the "database" connection string
  string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
    + "Data Source=\"" + dir + "\\\";"
    + "Extended Properties=\"text;HDR=No;FMT=Delimited\"";

  //create the database query
  string query = "SELECT * FROM " + file;

  //create a DataTable to hold the query results
  DataTable dTable = new DataTable();

  //create an OleDbDataAdapter to execute the query
  OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

  try
  {
    //fill the DataTable
    dAdapter.Fill(dTable);
  }
  catch (InvalidOperationException /*e*/)
  { }

  dAdapter.Dispose();

  return dTable;
}

try using Oledb. this codesnippet helps you read your csv file into a datatable which should be pretty easy to dump to a database.

public static DataTable ParseCSV(string path)
{
  if (!File.Exists(path))
    return null;

  string full = Path.GetFullPath(path);
  string file = Path.GetFileName(full);
  string dir = Path.GetDirectoryName(full);

  //create the "database" connection string
  string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
    + "Data Source=\"" + dir + "\\\";"
    + "Extended Properties=\"text;HDR=No;FMT=Delimited\"";

  //create the database query
  string query = "SELECT * FROM " + file;

  //create a DataTable to hold the query results
  DataTable dTable = new DataTable();

  //create an OleDbDataAdapter to execute the query
  OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

  try
  {
    //fill the DataTable
    dAdapter.Fill(dTable);
  }
  catch (InvalidOperationException /*e*/)
  { }

  dAdapter.Dispose();

  return dTable;
}
谁的新欢旧爱 2024-10-02 07:58:09

在 codeproject.com 上查看 快速 CSV 阅读器

另一种选择是使用SSIS。

Check out the fast CSV reader over at codeproject.com

Another option would be to use SSIS.

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