Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

What is the best way to use connection string ?

$
0
0

I declared my connection string in webconfig like this , <configuration>
    <connectionStrings>
      <add name="Dbconnection"
           connectionString="Server=USER-PC\SHUKLAJI; Database=OnlineExamination; Integrated Security=True; pooling=false; "
           providerName="System.Data.SqlClient" />
    </connectionStrings>
  <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>
 and then I am using like this ,

 public class DALRegister
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Dbconnection"].ToString());
 public int UserDetails(StudentRegistration sr)
        {
            try
            {
                SqlCommand cmd = new SqlCommand("prcregisteration", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", sr.Name);
                cmd.Parameters.AddWithValue("@username", sr.UserName);
                cmd.Parameters.AddWithValue("@password", sr.Password);
                cmd.Parameters.AddWithValue("@email", sr.Email);
                cmd.Parameters.AddWithValue("@phone", sr.Phone);
                con.Open();
                int result = cmd.ExecuteNonQuery();
                cmd.Dispose();
                return result;
            }
            catch(Exception ex)
            {
                throw;
            }
        }
    }

Is it not the best way to use Dbconnection? how to use it, in the best possible manner ? please reply soon. Thanks


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>