OleDbCommand ExecuteNonQuery正在更改数据库的文件路径(Access .accdb)

时间:2018-05-10 15:35:51

标签: c# ms-access oledbcommand

仅在尝试更新Access DB时抛出错误

我有这个问题我似乎无法克服。 我有一个使用MS Access数据库(版本2010 .accdb)的多层WPF应用程序 注意 - 我可以从数据库中读取而没有任何问题。但是,当我尝试更新数据库时,更改了数据库的文件路径并抛出了错误。

正确路径
“\\ hcpclrfs1w01 \ LakefrontShared \ EmployeeInfo \ EmployeeData.accdb”

抛出内部异常
OleDbException:找不到文件'C:\ Users \ trice \ AllDevProjects \ QaDashboard \ QaDashboard_Presentation \ bin \ Debug \ dbo.mdb'。
 消息执行查询时出错

在研究了这个问题几天之后,我能找到的最接近的建议是将“复制到输出直接”下的文件属性字段更改为值复制如果更新
请参阅此LINK

条件清单:

  • 访问2010/32位应用程序
  • 操作系统 - Windows 7 Enterprise / 32位
  • VS 2017 / c#
  • 尝试了不同的构建配置(任何CPU,x86)
  • 文件属性(在“复制到输出直接”下)设置为“如果更新则复制
  • 数据库受密码保护
  • 数据库未导入项目(但不应该需要)
  • 数据库中没有链接表

有人知道解决这个问题吗? 以下是我正在使用的更新方法:

public void Update(Employee dto)
{
    AccessDao dao = new AccessDao();
    string sql = string.Format(@"UPDATE [dbo].[{0}]
        SET [EmployeeName] = @EmployeeName 
           ,[OutLookFullName] = @OutLookFullName 
           ,[EmployeeAlias] = @EmployeeAlias 
           ,[EmployeeTEAM] = @EmployeeTEAM 
           ,[SpecificTeam] = @SpecificTeam 
           ,[SpecialAssignment] = @SpecialAssignment 
           ,[EmailAddress] = @EmailAddress 
           ,[ShiftStartTime] = @ShiftStartTime 
           ,[ShiftEndTime] = @ShiftEndTime 
           ,[EmployeeSHIFT] = @EmployeeSHIFT 
           ,[JobAssignment] = @JobAssignment 
           ,[NOTES] = @NOTES 
       WHERE ID = @ID ", EmpTable);

OleDbCommand command = dao.GetAccessCommand(sql);

command.Parameters.AddWithValue("@EmployeeName", dto.EmployeeName);
command.Parameters.AddWithValue("@OutLookFullName", dto.OutlookFullName);
command.Parameters.AddWithValue("@EmployeeAlias", dto.EmployeeAlias);
command.Parameters.AddWithValue("@EmployeeTEAM", dto.EmployeeTeam);
command.Parameters.AddWithValue("@SpecificTeam", dto.SpecificTeam);
command.Parameters.AddWithValue("@SpecialAssignment", dto.SpecialAssignment);
command.Parameters.AddWithValue("@EmailAddress", dto.EmailAddress);
command.Parameters.AddWithValue("@ShiftStartTime", dto.ShiftStartTime);
command.Parameters.AddWithValue("@ShiftEndTime", dto.ShiftEndTime);
command.Parameters.AddWithValue("@EmployeeSHIFT", dto.EmployeeShift);
command.Parameters.AddWithValue("@JobAssignment", dto.JobAssignment);
command.Parameters.AddWithValue("@NOTES", dto.Notes);
command.Parameters.AddWithValue("@ID", dto.EmployeeRecordId);
dao.ExecuteNonQuery(command);
}

连接字符串位于App.config文件中,如下所示:

  <add name="EmployeeCONN" 
              connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\hcpclrfs1w01\LakefrontShared\EmployeeInfo\EmployeeData.accdb;Jet OLEDB:Database Password= password;" 
              providerName="Microsoft.ACE.OLEDB.12.0" />

连接和工作由一个单独的数据访问类完成,如下所示:

 public class AccessDao
    {

        //TODO: TEMP While Testing
        private const string TestConnection = "EmployeeCONN";

        #region "Database Helper Methods"            

        // Connection
        private OleDbConnection _sharedConnection;
        public OleDbConnection SharedConnection
        {
            get
            {
                if (_sharedConnection == null)
                {
                    _sharedConnection = new
                        OleDbConnection(ConfigurationManager.ConnectionStrings[TestConnection].ConnectionString);
                }
                return _sharedConnection;
            }
            set { _sharedConnection = value; }
        }


        // Constructors
        public AccessDao() { }


   public void ExecuteNonQuery(OleDbCommand command)
        {
            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception("Error executing query", e);
            }
            finally
            {
                command.Connection.Close();
            }
        }

 public OleDbCommand  GetAccessCommand(string sqlQuery)
        {
            OleDbCommand command = new OleDbCommand();
            command.Connection = SharedConnection;
            command.CommandType = CommandType.Text;
            command.CommandText = sqlQuery;
            return command;
        }

0 个答案:

没有答案