Solved

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

Posted on 2013-01-24
5
627 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now