使用 ODBC 在 .NET 中上传 CSV [英] CSV upload in .NET using ODBC

查看:20
本文介绍了使用 ODBC 在 .NET 中上传 CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .net 中有 CSV 上传代码

Hi i have CSV upload code in .net

在 C# 中

 string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=D:\\csv;Extensions=asc,csv,tab,txt;Persist Security Info=False";
            DataSet ds;
            using (OdbcConnection oConn = new OdbcConnection(strConnString))
            {
                using (OdbcCommand oCmd = new OdbcCommand())
                {
                    oCmd.Connection = oConn;
                    oCmd.CommandType = System.Data.CommandType.Text;
                    oCmd.CommandText = "select * from [my.csv]";

                    OdbcDataAdapter oAdap = new OdbcDataAdapter();
                    oAdap.SelectCommand = oCmd;

                    ds = new DataSet();
                    oAdap.Fill(ds, "my");
                    oAdap.Dispose();                     
                    ds.Dispose();
                }
            }

在 .VB 中

  Dim strConnString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=D:\\csv;Extensions=asc,csv,tab,txt;Persist Security Info=False"

        Dim lOdbcConnection As New OdbcConnection(strConnString)
        '            lOdbcConnection.ConnectionString = strConnString
        'lOdbcConnection.Open()
        Using lOdbcCommand As New OdbcCommand, lOdbcDataAdapter As New OdbcDataAdapter
            lOdbcCommand.Connection = lOdbcConnection
            lOdbcCommand.CommandType = System.Data.CommandType.Text
            lOdbcCommand.CommandText = "select * from [my.csv]"

            lOdbcDataAdapter.SelectCommand = lOdbcCommand
            Dim ds As New DataSet()
            lOdbcDataAdapter.Fill(ds, "my")
            ds.Dispose()

            lOdbcDataAdapter.Dispose()

在 C# 中工作正常但是在 .VB 中,它在填充数据集时出现错误.-

In C# is working fine But in .VB its giving error when filling the dataset .-

ERROR [IM002] [Microsoft][ODBC Driver Manager] 未找到数据源名称且未指定默认驱动程序

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

我做错了什么?

推荐答案

你不需要转义 VB.NET 字符串中的 \ 字符,所以 "Dbq=D:\\csv" 应该是 "Dbq=D:\.csv".这就是找不到您的数据源的原因.

You don't need to escape the \ character in VB.NET strings so "Dbq=D:\\csv" should be "Dbq=D:\csv". That's why your data source isn't found.

这篇关于使用 ODBC 在 .NET 中上传 CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆