Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have many databases and database is dynamically change by input in connection string. But I have procedure in one database suppose named 'DB1' and want to insert data into another database named 'DB2'.

C#
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
conn1.Open();
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = conn1;
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.CommandText = "storeProcedure";
cmd3.Parameters.AddWithValue("@Col0", string.IsNullOrEmpty(index[0].ToString()) ? (object)DBNull.Value : index[0].ToString());    
cmd3.Parameters.AddWithValue("@Col1", string.IsNullOrEmpty(index[1].ToString()) ? (object)DBNull.Value : index[1].ToString());
cmd3.Parameters.AddWithValue("@Col2", string.IsNullOrEmpty(index[2].ToString()) ? (object)DBNull.Value : index[2].ToString());
cmd3.Parameters.AddWithValue("@Col3", string.IsNullOrEmpty(index[3].ToString()) ? (object)DBNull.Value : index[3].ToString());
cmd3.ExecuteNonQuery();


I can't make another connectonString for inserting purpose, because I don't know about no of databases.

What I have tried:

I am using two connectionString, one for use the procedure and second for insert data into specific database. I want insert data into 2nd connectionstring named connString1. But i dont know how can i do this. My connectionString:
<add name="connString" connectionString="Data Source=DBSERVER-PC\SQL2012;Database=DB1;User ID=sa;Password=a "providerName="System.Data.SqlClient" />

Any idea, how can I solve this problem?
Posted
Updated 4-Oct-19 2:12am
v2

There are a few ways that this could be accomplished; however, there is not enough information in the original question to know which method will work for you.

If the databases are on the same server AND the sql user has permission AND the second database to be accessed is constant you can alter the stored procedure to do the insert into the second DB
SQL
ALTER PROCEDURE dbo.SP1 (
  @Value1 INT,
  @Value2 INT
) AS
BEGIN
  INSERT LocalTable (Col1, Col2) 
  VALUES (@Value1, @Value2)

  INSERT SecondDatabase.dbo.RemoteTable (Col1, Col2)
  VALUES (@Value1, @Value2)
END


If this is not the case, then you are going to need to bring values back from the stored procedure via OUTPUT parameters, AND retrieve them AND open up your second connection and execute another command
SQL
ALTER PROCEDURE dbo.SP1 (
  @Value1 INT,
  @Value2 INT,
  @Value3 INT OUTPUT
) AS
BEGIN
  INSERT LocalTable (Col1, Col2) 
  VALUES (@Value1, @Value2)

  INSERT SecondDatabase.dbo.RemoteTable (Col1, Col2)
  VALUES (@Value1, @Value2)

  SET @Value3 = @Value1 + @Value2 + @Value3
END
and your calling code would need to add in the new parameter, and set it to OUTPUT or INPUT/OUTPUT
Note: I skipped the usings,try, catch, open, close for brevity
C#
SqlConnection conn1 = new SqlConnection(ConnectionString1);
SqlCommand cmd1 = new SqlCommand(CommandText1, conn1) {CommandType = CommandType.StoredProcedure};

// Input parameters
cmd1.Parameters.AddWithValue("@Value1", Value1);
cmd1.Parameters.AddWithValue("@Value2", Value2);

// Input/Output parameter
SqlParameter Param3 = new SqlParameter("@Value3", Value3);
Param3.Direction = ParameterDirection.InputOutput;
cmd1.Parameters.Add(Param3);

cmd1.ExecuteNonQuery();
int NewValue3 = (int)Param3.value;

SqlConnection conn2 = new SqlConnection(ConnectionString2);
SqlCommand cmd2 = new SqlCommand(CommandText2, conn2) {CommandType= CommandType.StoredProcedure};

// all input params this time
cmd2.Parameters.AddWithValue("@Value1", Value1);
cmd2.Parameters.AddWithValue("@Value2", Value2);
cmd2.Parameters.AddWithValue("@Value2", Value3);

cmd2.ExecuteNonQuery();

I hope all this helps
 
Share this answer
 
I'm not sure to fully understand your issue.
You can declare several connection strings:
XML
<add name="sourceDb" connectionString="Data Source=DBSERVER-PC\SQL2012;Database=DB1;User ID=sa;Password=a" providerName="System.Data.SqlClient" />

<add name="targetDb1" connectionString="Data Source=DBSERVER-PC\SQL2016;Database=DB1;User ID=sa;Password=b" providerName="System.Data.SqlClient" />

<add name="targetDb2" connectionString="Data Source=DBSERVER2-PC\SQL2013;Database=DB2;User ID=sa;Password=c" providerName="System.Data.SqlClient" />

And later refer to these connection strings this way:
C#
SqlConnection sourceConn = new SqlConnection
(
   ConfigurationManager.ConnectionStrings["sourceDb"].ConnectionString
);

SqlConnection targetConn1 = new SqlConnection
(
   ConfigurationManager.ConnectionStrings["targetDb1"].ConnectionString
);

SqlConnection targetConn2 = new SqlConnection
(
   ConfigurationManager.ConnectionStrings["targetDb2"].ConnectionString
);

You can even place your target connections into a list, and iterate over this list when you need to:
C#
List<SqlConnection> targetConnections = new List<SqlConnection>
(
   new SqlConnection[] { targetConn1, targetConn2 }
);

// ...

foreach (SqlConnection conn in targetConnections)
{
   // Do whatever you have to with each target...
}
 
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