Solved

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

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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

16 Experts available now in Live!

Get 1:1 Help Now