I have many databases and the database is dynamically changed 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'.
I can't make another connection string for inserting purpose, because I don't know about databases.
My code:
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
conn1.Open();
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = conn1;
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.CommandText = "storedProcedure";
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 am using two connection strings, one for use the procedure and second for insert data into a specific database. I want insert data into 2nd connection string named connString1. But I don't know how can I do this.
My connection string:
<add name="connString"
connectionString="Data Source=DBSERVER-PC\SQL2012;Database=DB1;User ID=sa;Password=a "
providerName="System.Data.SqlClient" />
<add name="connString1"
connectionString="Data Source=DBSERVER-PC\SQL2012;Database=DB2;User ID=sa;Password=a "
providerName="System.Data.SqlClient" />
Any idea how I can solve this problem?