1

In my MVC application web config file has three or multiple sql database connection strings with different database. My question is based user login enable one database rest of two should be disable. Is this possible in Mvc without using Entity framework???

My web config file is,

<add key="SqlConnection" value="Data Source=10.10.9.89;Initial Catalog=ABC;Persist Security Info=True;User ID=sa;Password=Pass1234"/>
   <add key="SqlConnection2" value="Data Source=10.10.9.89;Initial Catalog=XYZ;Persist Security Info=True;User ID=sa;Password=Pass1234"/>
   <add key="SqlConnection3" value="Data Source=10.10.9.89;Initial Catalog=PQR;Persist Security Info=True;User ID=sa;Password=Pass1234"/>

I have try this code,

[HttpPost]
        public ActionResult Index(LoginDetails log)
        {
            if (log.UserId == "Admin")
            {
               SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString);

            }
            else if (log.UserId == "Customer")
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnection1"].ConnectionString);
            }
            else
            {
                SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["SqlConnection2"].ConnectionString);
            }
            return View();            
        }

1 Answer 1

2

Even if there are multiple connection string in the web.config, it depends on with which connection string the SqlConnection object is initialized.

Suppose there are three connection string, myConnectionString1, myConnectionString2 and myConnectionString3 in web.config, and you want to use second one, then -

SqlConnection con = new SqlConnection(
 WebConfigurationManager.ConnectionStrings["myConnectionString2"].ConnectionString);
Sign up to request clarification or add additional context in comments.

4 Comments

If admin login myConnectionString1 need to enable rest of two connection string should be disable and if user login myConnectionString2 is enable and rest of two is disable . Is correct??
@Parameswaran - All 3 connection string will be there in web.config file. It is just that when an admin logs in you initialize it with myConnectionString1 and if normal user logs in, you initialize it with myConnectionString2
I wrote the code based on condition it throw the error
@Parameswaran - Please edit your question, and show the code that you have written for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.