Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to use multiple database in a C Sharp Project. Can anyone help me, please?
Posted

Set up different SqlConnection to each.
You can then access them independently.
 
Share this answer
 
Comments
Sumon562 13-Jun-13 3:39am    
I want to use only one method to do it. Is it possible? If possible how can I do that?
OriginalGriff 13-Jun-13 5:40am    
Yes - you can create as many different SqlConnection objects as you need (provided the connections are available on the DBs).

Exactly how you do this will depend on exactly what you are trying to achieve.
Hello,

In your app.config, you can define several connection strings :
XML
<configuration>
  <connectionstrings>
    <add name="cs_local">
    connectionString="Data Source=(local);Initial Catalog=YourDB;Integrated Security=True"/>
    <add name="cs_remote">
    connectionString="Data Source=YourServer;Initial Catalog=YourDB;Integrated Security=True"/>
  </add></add></connectionstrings>
</configuration>

And then, in your code-behind, you can do that :
C#
string cs_name = string.Empty;
if (your condition) 
    cs_name = "cs_local";
else
    cs_name = "cs_remote";
var connectionString = ConfigurationManager.ConnectionStrings[cs_name].ConnectionString;

Don't forget to add System.Configuration in your Using Directives ;)
 
Share this answer
 
The use of multiple database is very gud option when we performing some operation on multiple tables with different databases. For using you have to make some stored procedure which performing with two database tables accessing the database as like

Here, I created two databse 1.Database1 2.Databse2

1.Tables of Database1

tblData containing with fields are: id,name

2.Tables of Databse2

tblDemo containing with field are: Contact

Now, I inserting record in two difference table located to different database with stored procedure

Stored Procedure in Database1:

Create procedure InserData
(
@id int,
@name varchar(50),
@contact varchar(50)
)
as
begin

Insert into tblData values(@id,@name)

exec test.dbo.InsertData @contact

end

First Create stored procedure inside of Databse2

Create procedure InserData
(
@contact varchar(50)
)
as
begin
Insert into tblDemo values(@contact)
end


As per, above example you can be able to work with multiple database to stored data in multiple tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900