Solved

C# Program With SQL Server joins from tables in different databases?

Posted on 2013-01-24
5
644 Views
Last Modified: 2013-02-01
Is there a way to run a sql query in C# that can process data from two different tables that are in different databases?  My C# is failing because it can't find the second database.  Both databases are on the same server and in the same SQL Server instance.

SELECT column1, column2, database2.dbo.table1.column1,database2.dbo.table1.column2      
FROM database1 Inner JOIN column1 on database2.dbo.table1.column1
WHERE database2.dbo.table1.column2 = 'SelectMatchingDataFromDatabase1'

Thank you
0
Comment
Question by:LesterJebson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 38816852
Is this query working at SQL server?
If so, you can add this query as a store procedure to one of your databases and call it in your C# code; as long as the SQL login used by your app (or the account of your AppPool if using Web and Integrated Security) has the necessary access to both databases.
0
 

Author Comment

by:LesterJebson
ID: 38817080
Interesting...  Maybe there is a way to do this through a stored procedure.  The SQL works as a query in SQL Server Management Studio.  The query above is a simplified version of the query I'm trying to run in the C Sharp program.  

The real query has some variables that are used in the where clause.  It's a simple GUI front-end so users can enter the values to be used in their query, then run the query with the click of a button.

My SQL Server connection string only points to one of database so the table in the other database is not found.  If the C Sharp query can call a stored procedure while inserting values for the variables then this could work.
0
 
LVL 10

Accepted Solution

by:
Monica P earned 500 total points
ID: 38817664
Storedprocedure can take the Parameters..You can specify the "variable value" as parameter to StoredProcedure and call SToredprocedure during button click..

solution may help u : http://forums.asp.net/t/1516593.aspx

Used Stored procedure
CREATE PROCEDURE [dbo].[AddUser]
(
@FName varchar(50),
@LName varchar(50),
)
AS
BEGIN

SET NOCOUNT ON;
INSERT INTO UserDetails (FName, LName)
VALUES (@FName, @LName
END

Open in new window


String ConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;  
 SqlConnection con = new SqlConnection(ConnString);  
 SqlCommand cmd = new SqlCommand();  
 cmd.CommandType = CommandType.StoredProcedure;  
 cmd.CommandText = "AddUser";  
 cmd.Parameters.Add("@FName",SqlDbType.VarChar).Value = txtFName.Text.Trim();  
 cmd.Parameters.Add("@LName", SqlDbType.VarChar).Value = txtLName.Text.Trim();  
 cmd.Connection = con;  
 try  
 {  
      con.Open();  
      cmd.ExecuteNonQuery();  
      lblMessage.Text = "Record inserted successfully";  
 }  
 catch (Exception ex)  
 {  
      throw ex;  
 }  
 finally  
 {  
      con.Close();  
      con.Dispose();  
 }  

Open in new window

0
 
LVL 3

Expert Comment

by:contactnaeem
ID: 38821720
See what sql server user you are using through c# application, Looks like this user does not have access to second database. Usually in sqlserver managemnt studio we use sa user which has higher previliges but we use some specific user in application, looks like this is the issue.
0
 

Author Closing Comment

by:LesterJebson
ID: 38843842
Thank you Akila.  This was a perfect solution to my C# query problem.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question