简体   繁体   中英

Where can i use my sql connection string in Webconfig Asp.net Login form?

I am trying to make a sql server connection. I have used ASP.net web form template which is having its own login and register Ui. I just have to use my sql Server name and the database name in the connection configuration. I had googled and read regarding the sql connection. I FOUND something like this in WEbconfig i have to alter..

<connectionStrings >
    <add
         name="myConnectionString" 
         connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
         providerName="System.Data.SqlClient"/>
</connectionStrings>

It does not work if i use it . Should i use SqlConnection con = new SqlConnection(strcon); in my login page. SqlConnection con = new SqlConnection(strcon); in my login page. if in case, then where ? While in this template i have something like..

protected void LogIn(object sender, EventArgs e)
{
    if (IsValid)
    {
        // Validate the user password
        var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

.....

I ma bit confused please educate me or guide me in the right way. I have a server name xyz-1-2, database name: data123, username: abcd123 and password: asdf123.

I am trying to connect to one database and visualize the data.

I assume you are using at least Visual Studio 2013 to create the web form project by using the default template available there.

Now, let's take a look on the database setup first, as it seems like this is the one you're asking about.

After the project generation, the default connection string in the web.config file, usually look like this :

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebMVCWithAuth-20180220020657.mdf;Initial Catalog=aspnet-WebMVCWithAuth-20180220020657;Integrated Security=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>

When will the connection string be used actually in the project? Please examine the screenshot below :

在此处输入图片说明

It's not necessary to change the connection's name here, but please do adjust the connectionString value, in the later stage.

So, where's the actual database file resides, you may ask? Actually, it is hidden in the App_Data folder, and you will not see it until click the "Show All Files" icon just below the "Solution Explorer" header, as shown in the following screenshot :

在此处输入图片说明

And how do you browse the content of this database? Look into the left-most of the VS editor and find a toolbox "Server Explorer" , and there you are. See this screenshot?

在此处输入图片说明

Now by the time you end reading this, you should have an idea how to have your own user manager database, based on this template.

Oh, and one more tip : you can actually copy this local database to a real server and attach it to the database server so you may continuously maintain the database in a more secure environment.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM