IErrorInfo.GetDescription因E_FAIL(0x80004005)而失败。 [英] IErrorInfo.GetDescription failed with E_FAIL(0x80004005).

查看:68
本文介绍了IErrorInfo.GetDescription因E_FAIL(0x80004005)而失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行查询以将数据库从msaccess传输到SQL时,出现了'IErrorInfo.GetDescription使用E_FAIL(0x80004005)'失败的错误。





我写了这段代码



 DbProviderFactory factory = DbProviderFactories.GetFactory(  System.Data.OleDb); 
DataTable userTables = null ;
使用(连接)
{
string mappath = dataGridView1。 CurrentRow.Cells [ Path]。Value.ToString();
string [] filePaths = Directory.GetFiles( @ + mappath + * .mdb,SearchOption.TopDirectoryOnly);
// c:\\\test.mdb
foreach string tr in filePaths)
{
connection.ConnectionString = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + tr + ;
string [] restrictions = new string [ 4 ];
限制[ 3 ] = ;
connection.Open();
userTables = connection.GetSchema( ,限制条件);
List< string> tableNames = new List< string>();
for int i = 0 ; i < userTables.Rows.Count; i ++)
tableNames.Add(userTables.Rows [i] [ 2 ]的ToString());
尝试
{
foreach string tableName in tableNames)
{
// cn1 = new SqlConnection(con);
// if(cn1.State!= ConnectionState.Open){cn1.Open(); }
// SqlCommand cmd = new SqlCommand(select * into [+ tableName +来自OPENROWSET的]('Microsoft.Jet.OLEDB.4.0','+ tr +';'Admin';'',[+ tableName +]));
< span class =code-comment> //
cmd.Connection = cn1;
/ / cmd.ExecuteNonQuery();
OleDbCommand cmd = new OleDbCommand(< span class =code-string>
select * into [ + tableName + ] IN + con + FROM [ + tableName + ],connection);
cmd.ExecuteNonQuery();
connection.Close();
}
}
catch (Exception Ex){connection.Close(); }
connection.Close();
}
}
}
catch (例外情况){}





查询是这样的



 选择 * 进入 [ACTYPE]  IN  ODBC; Driver = {SQL Server}; Server = '  PC01';初始目录= TARR;持续安全性 Info = True; 用户 ID = '  sa';密码= '  123'  FROM  [ACTYPE] 







你愿意吗我知道如果你使用 OPENROWSET [ ^ ]并在Sql Server中执行语句。查询可能看起来像

  INSERT   INTO  SqlServerTableName(col1,col2,...)
SELECT col1,col2,...
FROM OPENROWSET ' Microsoft.Jet .OLEDB.4.0'
' mdb文件的路径';
' admin'; ' '
AccessTableName);



如果您不想使用OPENROWSET,您可以在Sql Server上创建一个指向Access数据库的链接服务器



为了创建一个链接服务器,看看 sp_addlinkedserver [ ^ ]。



因此创建服务器可能类似于

  EXEC  sp_addlinkedserver 
@ server = ' mydatainaccess'
@ provider = ' Microsoft.Jet.OLEDB.4.0'
@ srvproduct = ' Jet'的OLE DB提供程序
@datasrc = ' mdb文件的路径'



所以你只能在SQL Serv上创建一次之后,您可以通过在Sql Server上执行INSERT INTO SELECT语句来复制数据。类似于:

  INSERT   INTO  SqlServerTableName (col1,col2,...)
SELECT col1,col2,... FROM mydatainaccess ... AccessTableName


最后我通过使用链接服务器将数据库从msaccess导入到sql来编写代码



  foreach  string  tableName  in  tableNames)
{
string acc = tableName + DateTime.Now.Millisecond;
string str = EXEC sp_addlinkedserver +
@ server =' + acc + ', +
@ provider ='Microsoft.Jet.OLEDB.4.0', +
@ srvproduct ='用于Jet的OLE DB提供程序', +
@datasrc =' + tr + ', +
@ provstr ='';
try
{
cn1 = new SqlConnection(con);
if (cn1.State!= ConnectionState.Open){cn1.Open(); }
cmd = new SqlCommand(str);
cmd.Connection = cn1;
cmd.ExecuteNonQuery();
}
catch (Exception Ex){}
str = exec sp_addlinkedsrvlogin @ rmtsrvname = N' + acc + ', +
@ useself = N'false', +
@ locallogin = NULL, +
@ rmtuser = N'Admin', +
@ rmtpassword = NULL;
尝试
{
if (cn1.State!= ConnectionState .Open){cn1.Open(); }
cmd = new SqlCommand(str);
cmd.Connection = cn1;
cmd.ExecuteNonQuery();
}
catch (例外EX){}
尝试
{
cn1 = new SqlConnection(con);
if (cn1.State!= ConnectionState.Open){cn1.Open(); }
cmd = new SqlCommand( select *从 + acc + + tableName + -string> ... + tableName + );
cmd.Connection = cn1;
cmd.CommandTimeout = 180 ;
cmd.ExecuteNonQuery(); ------- ERROR
}
catch (Exception EX){}
try
{
cn1 = new SqlConnection(con);
if (cn1.State!= ConnectionState.Open){cn1.Open(); }
cmd = new SqlCommand( IF EXISTS(SELECT * FROM sys.servers WHERE name = N' + acc + ')EXEC master .sys.sp_dropserver' + acc + ','droplogins');
cmd.Connection = cn1;
cmd.ExecuteNonQuery();
}
catch (Exception EX){}
// OleDbCommand cmd = new OleDbCommand(select * into [+ tableName +] IN+ con +FROM [+ tableName +],connection);
/// / OleDbCommand cmd = new OleDbCommand(select [[+ tableName +]。* ]到[+ con +]。[+ tableName +] From [+ tableName +]);
// cmd.Connection = connection;
// cmd.ExecuteNonQuery();
// connection.Close();
cn1.Close();
}
}
catch (Exception Ex){connection.Close(); cn1.Close(); }
connection.Close();





但是我收到这样的错误'无法初始化OLE的数据源对象数据库提供程序Microsoft.Jet.OLEDB.4.0用于链接服务器ClientAc723。

OLE DB提供程序Microsoft.Jet.OLEDB.4.0用于链接服务器ClientAc723返回消息无法使用'';文件已在使用中。。'在select * into查询



如果您的任何帮助将不胜感激。


I got the error like this 'IErrorInfo.GetDescription failed with E_FAIL(0x80004005)'.
while executing the query to transfer the database from msaccess to SQL.

I have written this code

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
               DataTable userTables = null;
               using (connection)
               {
                   string mappath = dataGridView1.CurrentRow.Cells["Path"].Value.ToString();
                   string[] filePaths = Directory.GetFiles(@"" + mappath + "", "*.mdb", SearchOption.TopDirectoryOnly);
                   // c:\test\test.mdb
                   foreach (string tr in filePaths)
                   {
                       connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tr + "";
                       string[] restrictions = new string[4];
                       restrictions[3] = "Table";
                       connection.Open();
                       userTables = connection.GetSchema("Tables", restrictions);
                       List<string> tableNames = new List<string>();
                       for (int i = 0; i < userTables.Rows.Count; i++)
                           tableNames.Add(userTables.Rows[i][2].ToString());
                       try
                       {
                           foreach (string tableName in tableNames)
                           {
                               //cn1 = new SqlConnection(con);
                               //if (cn1.State != ConnectionState.Open) { cn1.Open(); }
                               //SqlCommand cmd = new SqlCommand("select * into [" + tableName + "] from OPENROWSET('Microsoft.Jet.OLEDB.4.0','" + tr + "';'Admin';'',[" + tableName + "])");
                               //cmd.Connection = cn1;
                               //cmd.ExecuteNonQuery();
                               OleDbCommand cmd = new OleDbCommand("select * into ["+tableName+"] IN "+con+" FROM ["+tableName+"]",connection);
                               cmd.ExecuteNonQuery();
                               connection.Close();
                           }
                       }
                       catch (Exception Ex) { connection.Close(); }
                       connection.Close();
                   }
               }
           }
           catch (Exception Ex) { }



And the query is like this

select * into [ACTYPE] IN ODBC; Driver={SQL Server};Server='PC01';Initial Catalog=TARR;Persist Security Info=True;User ID= 'sa';Password='123' FROM [ACTYPE]




Would u pls resolve this error.

解决方案

I believe this would be simpler if you would use OPENROWSET[^] and execute the statement in Sql Server. The query could look like

INSERT INTO SqlServerTableName (col1, col2, ...) 
SELECT col1, col2, ...
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
                'path to the mdb file';
                'admin';'',
                AccessTableName);


If you don't want to use OPENROWSET you can create a linked server on the Sql Server which would point to the Access database.

In order to create a linked server, have a look at sp_addlinkedserver[^].

So creating the server could be something like

EXEC sp_addlinkedserver 
        @server     = 'mydatainaccess', 
        @provider   = 'Microsoft.Jet.OLEDB.4.0', 
        @srvproduct = 'OLE DB Provider for Jet',
        @datasrc    =  'path to the mdb file'


So you'd create this only once on the SQL Server and after that you can copy the data by executing the INSERT INTO SELECT statements on the Sql Server. Something like:

INSERT INTO SqlServerTableName (col1, col2, ...) 
SELECT col1, col2, ... FROM mydatainaccess...AccessTableName


Finally I have written code by using linked server to import db from msaccess to sql

foreach (string tableName in tableNames)
                          {
                              string acc = tableName + DateTime.Now.Millisecond;
                              string str = "EXEC sp_addlinkedserver "+
                              "@server     = '"+acc+"', "+
                              "@provider   = 'Microsoft.Jet.OLEDB.4.0', "+
                              "@srvproduct = 'OLE DB Provider for Jet',"+
                              "@datasrc    =  '"+tr+"',"+
                              "@provstr=''";
                              try
                              {
                                  cn1 = new SqlConnection(con);
                                  if (cn1.State != ConnectionState.Open) { cn1.Open(); }
                                  cmd = new SqlCommand(str);
                                  cmd.Connection = cn1;
                                  cmd.ExecuteNonQuery();
                              }
                              catch (Exception Ex) { }
                              str = "exec sp_addlinkedsrvlogin @rmtsrvname=N'" + acc + "', " +
                                         "@useself = N'false'," +
                                         "@locallogin = NULL," +
                                         "@rmtuser = N'Admin'," +
                                         "@rmtpassword = NULL";
                               try
                               {
                                   if (cn1.State != ConnectionState.Open) { cn1.Open(); }
                                   cmd = new SqlCommand(str);
                                   cmd.Connection = cn1;
                                   cmd.ExecuteNonQuery();
                               }
                               catch (Exception EX) { }
                               try
                               {
                                   cn1 = new SqlConnection(con);
                                   if (cn1.State != ConnectionState.Open) { cn1.Open(); }
                                   cmd = new SqlCommand("select * into " + tableName + " from " + acc + "..." + tableName + "");
                                   cmd.Connection = cn1;
                                   cmd.CommandTimeout = 180;
                                   cmd.ExecuteNonQuery();-------ERROR 
                               }
                               catch (Exception EX) { }
                               try
                               {
                                   cn1 = new SqlConnection(con);
                                   if (cn1.State != ConnectionState.Open) { cn1.Open(); }
                                   cmd = new SqlCommand("IF EXISTS(SELECT * FROM sys.servers WHERE name = N'" + acc + "') EXEC master.sys.sp_dropserver '" + acc + "','droplogins'");
                                   cmd.Connection = cn1;
                                   cmd.ExecuteNonQuery();
                               }
                               catch (Exception EX) { }
                              //OleDbCommand cmd = new OleDbCommand("select * into [" + tableName + "] IN " + con + " FROM [" + tableName + "]", connection);
                              ////OleDbCommand cmd = new OleDbCommand("select [[" + tableName + "].*] into  [" + con + "].[" + tableName + "] From [" + tableName + "]");
                              //cmd.Connection = connection;
                              //cmd.ExecuteNonQuery();
                              //connection.Close();
                               cn1.Close();
                          }
                      }
                      catch (Exception Ex) { connection.Close(); cn1.Close(); }
                      connection.Close();



But I am getting error like this 'Cannot initialize the data source object of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ClientAc723".
OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "ClientAc723" returned message "Could not use ''; file already in use.".' at select * into query

If any help from you would be much appreciated.


这篇关于IErrorInfo.GetDescription因E_FAIL(0x80004005)而失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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