Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-24
5
Medium Priority
?
648 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 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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