Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从不同的数据库检索行_C#_Asp.net_Database - Fatal编程技术网

C# 从不同的数据库检索行

C# 从不同的数据库检索行,c#,asp.net,database,C#,Asp.net,Database,我需要从另一个数据库中检索一行。如何从差异数据库检索数据并合并到单个数据表中 我需要检索这些行并将它们导出到Excel 请帮我找到解决办法 这是我的密码: public void Execute(IJobExecutionContext context) { try { logger.InfoFormat("....blcExportExcel start run.... "); SqlCommand co

我需要从另一个数据库中检索一行。如何从差异数据库检索数据并合并到单个数据表中

我需要检索这些行并将它们导出到Excel

请帮我找到解决办法

这是我的密码:

  public void Execute(IJobExecutionContext context)
  {
        try
        {
            logger.InfoFormat("....blcExportExcel start run....  ");
            SqlCommand com1 = null;
            //SqlCommand comCount = null;
            SqlConnection con1 = null;

            //--for microsoft 2003--
            string strDownloadFileName = "E://lewre/excel/color/delete_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xls";
            string oleDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDownloadFileName + ";Extended Properties='Excel 8.0;HDR=Yes'";
            //**for microsoft 2003**

            //--for microsoft 2007--
            //string strDownloadFileName = "E://lewre/excel/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            //string oleDbConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strDownloadFileName + ";Extended Properties='Excel 12.0 Xml;HDR=Yes'";
            //**for microsoft 2007**

            //--Export product for icenter 25/7/2012--
            string query = @"

                   select lewre_article.SKU_CODE,icenter_acStockCompany.AcStockID
                from 
                [LEWREDB].[dbo].[LEWRE.PRODUCT] as lewre_article
                left join  [iCenter].[dbo].[AcStockCompany] as icenter_acStockCompany
                on (lewre_article.SKU_CODE = icenter_acStockCompany.AcStockID )
                where icenter_acStockCompany.AcStockID is  null
            ";

            //**Export product for icenter 25/7/2012**
            con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["iCenterConnectionString"].ConnectionString);
            con1.Open();

            com1 = new SqlCommand(query, con1);
            DataTable dt = new DataTable();
            SqlDataAdapter ada = new SqlDataAdapter(com1);
            ada.Fill(dt);

            //--If no record then return--
            if (dt.Rows.Count == 0)
            {
                return;
            }
            //**If no record then return**

            using (OleDbConnection conn = new OleDbConnection(oleDbConnection))
            {
                // Create a new sheet in the Excel spreadsheet.
                string createTable = " ";
                createTable += " create table Query( SKU_CODE varchar(50) , AcStockID varchar(50))";
                // Create a new sheet in the Excel spreadsheet.
                OleDbCommand cmd = new OleDbCommand(createTable, conn);

                // Open the connection.
                conn.Open();

                // Execute the OleDbCommand.
                cmd.ExecuteNonQuery();

                cmd.CommandText = @"INSERT INTO Query (
                 SKU_CODE, AcStockID ) 

                values (?,?) ";
                // Add the parameters.
                cmd.Parameters.Add("SKU_CODE", OleDbType.VarChar, 50, "SKU_CODE");
                cmd.Parameters.Add("AcStockID", OleDbType.VarChar, 50, "AcStockID");



                // Initialize an OleDBDataAdapter object.
                OleDbDataAdapter da = new OleDbDataAdapter("select * from Query ", conn);

                // Set the InsertCommand of OleDbDataAdapter, 
                // which is used to insert data.
                da.InsertCommand = cmd;
                // Changes the Rowstate()of each DataRow to Added,
                // so that OleDbDataAdapter will insert the rows.

                foreach (DataRow dr in dt.Rows)
                {
                    dr.SetAdded();
                }

                // Insert the data into the Excel spreadsheet.
                da.Update(dt);
            }

            JobKey jobKey = context.JobDetail.Key;
            logger.InfoFormat("blcPosExcel : {0} executing at {1}", jobKey, DateTime.Now.ToString("r"));
            logger.InfoFormat("excel post run finnish ");



        }
        catch (Exception ex)
        {
            logger.Error(ex.Message);
            throw;

        }
    }
}
我的连接字符串

   <!--Koo Testing Server-->
    <add name="LEWREDBConnectionString" connectionString="Data Source=NATE-PC\SQLEXPRESS2008R2;Initial Catalog=LEWREDB_WEB_TEST;User ID=user1;Password=user" providerName="System.Data.SqlClient" />
    <add name="LEWREDBEntities" connectionString="metadata=res://*/ClassModel.LinQLewre.csdl|res://*/ClassModel.LinQLewre.ssdl|res://*/ClassModel.LinQLewre.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=NATE-PC\SQLEXPRESS2008R2;Initial Catalog=LEWREDB_WEB_TEST;Persist Security Info=True;User ID=user1;Password=user;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    <add name="iCenterConnectionString" connectionString="Data Source= NATE-PC\SQLEXPRESS2008R2;Initial Catalog=iCenter_Testing;User ID=user2;Password=user" providerName="System.Data.SqlClient" />
    <!--Koo Testing Server-->

我得到的错误:

无法打开登录请求的数据库“iCenter\u Testing”。这个 登录失败。用户“user2”登录失败


检查
iCenter\u Testing
数据库中是否存在
user2
,以及用户是否具有正确的权限。如果连接到数据库,则可以运行以下代码以查看用户是否存在:

从master.dbo.syslogins中选择name,其中name='user2'

但是,如果您能够使用
user2
登录到数据库,则可能是没有对
iCenter\u测试
数据库的必要权限

SELECT有DBACCESS('iCenter\u Testing')

上述SQL将返回
1
0
1
表示用户可以访问数据库

要找出用户也可以访问哪些数据库,可以运行以下代码:

SELECT [Name] as DatabaseName 
FROM master.dbo.sysdatabases
WHERE ISNULL(HAS_DBACCESS([Name]),0)=1
ORDER BY [Name]

这意味着…在
iCenter\u测试中
DB
user2
无效…从SQL Server 2005开始,您应该停止使用
sysdatabases
和所有旧式视图(如
sysobjects
)。请改用目录视图架构中的
sys.databases
。@marc\s:注意了,但是没有提到正在使用哪个SQL Server版本,所以请注意安全。