OLEDB连接打开方法无法正常工作 [英] OLEDB Connection open method not functioning

查看:104
本文介绍了OLEDB连接打开方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码从excel表绑定GridView。该文件保存在该文件夹中,但 oconn.open 未执行,因此未填充gridview。我也试过了ACE.OLEDB.12.0。任何人都可以帮我解决这个问题吗?



提前致谢。

  string  path = UploadExp.FileName; 
UploadExp.SaveAs(Server.MapPath( 。\\Uploadfiles\\ + path));
OleDbConnection oconn = new OleDbConnection( @ Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + Server.MapPath( 。 \\\UploadFiles \\ + path)+ ;扩展属性= Excel 8.0< /跨度>);
oconn.Open();
OleDbCommand ocmd = new OleDbCommand( 选择*来自[Sheet1 $],oconn);
OleDbDataAdapter odap = new OleDbDataAdapter(ocmd);
dl.sqldset = new DataSet();
odap.Fill(dl.sqldset);
GridView1.DataSource = dl.sqldset;
GridView1.DataBind();
oconn.Close();

解决方案

,oconn);
OleDbDataAdapter odap = new OleDbDataAdapter(ocmd);
dl.sqldset = new DataSet();
odap.Fill(dl.sqldset);
GridView1.DataSource = dl.sqldset;
GridView1.DataBind();
oconn.Close();


您可以使用以下代码查看服务器上连接excel文件是否有任何问题。如果没有问题,您应该查找文件存在或权限等。

我希望在发生此类文件问题时抛出异常,但是你说没有捕获异常。我仍然建议你将所有异常记录到某个位置(文本文件,数据库等)到看看是怎么回事,而不是在UI上将它们显示为错误消息。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Data.OleDb;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 OleDbTest1
{
class Program
{
静态 void Main()
{
尝试
{
// 查看任何可用的Jet提供商
Test1();

// 将excel与Jet提供商连接。
Test2();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}

// 连接测试
< span class =code-keyword> private static void Test2()
{
string connStr = @ 提供商= Microsoft.Jet.OLEDB.4.0;数据源= {0};扩展属性=Excel 8.0; HDR =否; IMEX = 1;

// 可以是server.map
connStr = < span class =code-keyword> string .Format(connStr, @ D:\ tmp \book1.xls);
使用(OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
Console.WriteLine( db ='{0}'ds ='{1}',conn.Database,conn.DataSource);
}
}

// 查看可用的ole db提供商
private static void Test1()
{
OleDbDataReader reader = OleDbEnumerator.GetRootEnumerator();
DisplayData(读者);
}

静态 void DisplayData(OleDbDataReader reader)
{
// SOURCES_NAME = Microsoft.Jet.OLEDB.4.0
while (reader.Read())
{
if (!reader [ SOURCES_NAME]。ToString()。包含( Jet))
continue ;
for int i = 0 ; i < reader.FieldCount; i ++)
{
Console.WriteLine( {0} = {1}
reader.GetName(i),reader.GetValue(i));
}
Console.WriteLine( ============ ======================);
}
}
}
}


尝试以下代码

< pre lang =c#> 尝试
{
if (UploadExp.HasFile )
{
string path = UploadExp.FileName;
UploadExp.SaveAs(Server.MapPath( 。\\Uploadfiles\\ + path));
使用(OleDbConnection oconn = new OleDbConnection( @ Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + Server.MapPath( 。\\UploadFiles \\ + path)+ ;扩展属性= Excel 8.0))
{
oconn.Open();
使用(OleDbCommand ocmd = new OleDbCommand( 从[Sheet1

中选择*

I have the following code to bind a GridView from an excel sheet. The file gets saved in the folder, but oconn.open does not get executed and therefore gridview is not populated. I tried with ACE.OLEDB.12.0 too. Can anyone help me to resolve this?

Thanks in advance.

string path = UploadExp.FileName;
UploadExp.SaveAs(Server.MapPath(".\\Uploadfiles\\" + path));
OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".\\UploadFiles\\" + path) + ";Extended Properties=Excel 8.0");
oconn.Open();
OleDbCommand ocmd = new OleDbCommand("Select * from [Sheet1$]", oconn);
OleDbDataAdapter odap = new OleDbDataAdapter(ocmd);
dl.sqldset = new DataSet();
odap.Fill(dl.sqldset);
GridView1.DataSource = dl.sqldset;
GridView1.DataBind();
oconn.Close();

解决方案

", oconn); OleDbDataAdapter odap = new OleDbDataAdapter(ocmd); dl.sqldset = new DataSet(); odap.Fill(dl.sqldset); GridView1.DataSource = dl.sqldset; GridView1.DataBind(); oconn.Close();


you can use the following code to see if there is any problem to connect an excel file on your server. If there is no problem with that, you should look for file existence or permissions etc.
I expect that an exception is thrown in case of such file issues, but you say no exception is caught. I still advice you to log all exceptions into some location (a text file, database etc) to see what is going on, instead of displaying them as error messages on UI.

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OleDbTest1
{
    class Program
    {
        static void Main()
        {
            try
            {
                // to see any available Jet provider
                Test1();

                // to connect an excel with a Jet provider.
                Test2();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadKey();
        }

        // connection test
        private static void Test2()
        {
            string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties=""Excel 8.0;HDR=No;IMEX=1""";

            // can be server.map
            connStr = string.Format(connStr, @"D:\tmp\book1.xls");
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                Console.WriteLine("db='{0}' ds='{1}'", conn.Database, conn.DataSource);
            }
        }

        // to see available ole db providers
        private static void Test1()
        {
            OleDbDataReader reader = OleDbEnumerator.GetRootEnumerator();
            DisplayData(reader);
        }

        static void DisplayData(OleDbDataReader reader)
        {
            // SOURCES_NAME = Microsoft.Jet.OLEDB.4.0
            while (reader.Read())
            {
                if (!reader["SOURCES_NAME"].ToString().Contains("Jet"))
                    continue;
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    Console.WriteLine("{0} = {1}",
                     reader.GetName(i), reader.GetValue(i));
                }
                Console.WriteLine("==================================");
            }
        }
    }
}


try below code

try
{
    if (UploadExp.HasFile)
    {
        string path = UploadExp.FileName;
        UploadExp.SaveAs(Server.MapPath(".\\Uploadfiles\\" + path));
        using (OleDbConnection oconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".\\UploadFiles\\" + path) + ";Extended Properties=Excel 8.0"))
        {
            oconn.Open();
            using (OleDbCommand ocmd = new OleDbCommand("Select * from [Sheet1


这篇关于OLEDB连接打开方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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