OleDbConnection的问题 [英] Issue with OleDbConnection

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

问题描述



我收到以下错误.请帮我解决这个问题.

OleDbException由用户代码未处理
Microsoft Access数据库引擎无法打开或写入文件''.它已经由另一个用户专门打开,或者您需要权限才能查看和写入其数据


我到目前为止所做的,
1)我添加了文件上传控件.点击按钮中的代码,

Hi,

I am getting the below error. Please help me to solve this.

OleDbException was unhandled by user code
The Microsoft Access database engine cannot open or write to the file ''''. It is already opened exclusively by another user, or you need permission to view and write its data


What I did so far,
1) I have added an file upload control. Code in the button click,

protected void btnUpload_Click(object sender, EventArgs e)
 {
         string strFileExtension = Path.GetExtension(fuList.PostedFile.FileName).ToString().ToUpper();
         string connstr = string.Empty;
         string strFileName = (fuList.PostedFile.FileName).Replace("'", "");
         if (strFileExtension == ".XLSX")
             connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + (fuList.PostedFile.FileName).Replace("'", "") + "';Extended Properties=Excel 12.0";

         else if (strFileExtension == ".XLS")
             connstr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source='" + (fuList.PostedFile.FileName).Replace("'", "") + "';Extended Properties=Excel 8.0";
         OleDbConnection conn = new OleDbConnection(connstr);
         conn.Open();
}


我在conn.Open()遇到上述错误

2)我尝试了以下方法来解决这个问题,
(我正在使用64位Windows 7 OS和32位MS Office)
创建的目录"C:\ Windows \ SysWOW64 \ config \ systemprofile \ Desktop"
并添加了网络服务并给予了完全许可
(桌面属性>>>>添加了NETWORK SERVICE>>并授予了完全许可)


I got the above mentioned error at conn.Open()

2) I have tried the following method to solve this,
(I am using 64 bit windows 7 OS and MS Office 32 bit)
Created directory "C:\Windows\SysWOW64\config\systemprofile\Desktop "
And added NETWORK SERVICE and gave full permission
(Desktop Properties >> Security >> Added NETWORK SERVICE >> And gave full permission)

推荐答案

也许是另一个程序使用了文件,然后发生上述错误.
要查看是否可以独占访问该文件,可以使用此类:

maybe another program uses the files and then the above error occurs.
To see if you can have exclusive access to the file you can use this class:

public class FileCheck
    {
        public static Boolean FileCanBeCopied(String FolderAndFileName)
        {
            Boolean canBeCopied = false;
            if (!String.IsNullOrEmpty(FolderAndFileName))
            {
                FileInfo fi = new FileInfo(FolderAndFileName);
                if (fi.Exists)
                {
                    FileStream stream = null;
                    try
                    {
                        // check if the file can be opened exclusive. If that is possible, then the file can be copied otherwise in transfer
                        using (stream = File.Open(FolderAndFileName, FileMode.Open, FileAccess.Write, FileShare.None))
                        {
                            if (stream.CanWrite)
                                canBeCopied = true;
                        }
                    }
                    catch (Exception)
                    {
                        canBeCopied = false;
                    }
                }
            }
            return canBeCopied;
        }
    }


如果打开了访问文件,则它将锁定该文件,因此请在运行应用程序之前尝试关闭访问文件,

有时需要重新启动计算机以确保没有其他人正在与您一起使用访问文件,
if the access file is open, then it will lock the file, so try to close the access file before running your application,

some times it requires restarting your computer to make sure that no one else is using the access file with you,


这篇关于OleDbConnection的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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