Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:05:46+00:00 2026-06-10T16:05:46+00:00

I need to retrieve a row from a different database. How can I retrieve

  • 0

I need to retrieve a row from a different database. How can I retrieve data from difference database and merge into one single data table?

I need to retrieve those rows and export them to Excel

Please help me find a solution.

Here is my code:

  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;

        }
    }
}

my connectionstring

   <!--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-->

Error that I’m getting:

Cannot open database “iCenter_Testing” requested by the login. The
login failed. Login failed for user ‘user2’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:05:47+00:00Added an answer on June 10, 2026 at 4:05 pm

    Check that user2 exists in the iCenter_Testing database and that the user has the correct permissions. If you connect to the database, you can run the following code to see if the user exists:

    SELECT name FROM master.dbo.syslogins WHERE name = 'user2'

    But, if you are able to log in to the database using the user2, it could be a case of not having the necessary permission to the iCenter_Testing database.

    SELECT HAS_DBACCESS('iCenter_Testing');

    The above SQL will return a 1 or 0. The 1 means the user has access to the database.

    To find out which databases a user has access too, you can run the following code:

    SELECT [Name] as DatabaseName 
    FROM master.dbo.sysdatabases
    WHERE ISNULL(HAS_DBACCESS([Name]),0)=1
    ORDER BY [Name]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I need to retrieve a single row of data from the database, why
i need to retrieve single row from table, and i was interested what approach
Goal: To retrieve a single record (row) from a MySQL database for a user
i'm doing a project where i need to retrieve data from one field of
I need to retrieve data from several rows and then insert the results into
I need to retrieve a value from a website (can vary and I have
I need to retrieve random rows from SQL Server database. I am looking for
I need to retrieve data from two tables. the first is a list of
I need to write a query where I need to retrieve the data from
I need to bring back only one of the records from a duplicated row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.