user11943580
user11943580

Reputation:

Save user Data into DB Visual Studio

I have created a database and a simple web form with couple of text-filled and a submit button in visual studio 2019(Asp.net c#). now I'm trying to save user data into the tables, since I'm not using sql server management do I still need use SqlConnection if so what is the best way to create a connecting string?

Upvotes: 1

Views: 195

Answers (1)

Rohan Rao
Rohan Rao

Reputation: 2603

The best way to create a connection string is to write it in the Web.Config file which you will find it at the bottom of your Project in Solution Explorer.

Web.config file is basically an xml file that contains the configuration of your websites which includes your connection string.

 <connectionStrings>
<add name="YourConnectionStringNameHere" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUsernamehere; Password=Yourpasswordhere; " providerName="System.Data.SqlClient" />
</connectionStrings>

Why to write the connection string in Web.config?

  • You don't have to declare the connection string again and again and again in each .cs file. This is tedious. You will have one connection string at one safe place.
  • This is required because if some other guy handles your project and wants to change the connection name according to the requirement (in future) then it would serve a sole area as the developer don't have hassles here and there to find your connection string. This provides a clean code to understand not only you but also to other developers who will be handling your projects in future.

The SQL connection would still be required:

The SQLConnection would be required in order to connect to the database no matter whether you use SSMS or not. Even in the internal database that Visual Studio provides, you will be required to provide this connection string reference in your .cs file.

Hope this helps.

Upvotes: 2

Related Questions