Solved

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

Posted on 2013-01-24
5
634 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
5 Comments
 
LVL 35

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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