سؤال

I am working on login by following this article Creating a custom user login form with .NET C# MVC 4 Razor

So in that I have followed all those steps as mentioned I that article now form that I am fetching the problem with open database connection..

In Models

public class User
{
    public string UserName { get; set; }

    public string Password { get; set; }

    public boolRememberMe { get; set; }

    public bool IsValid(string _username, string _password)
    {
        using (var conn = newSqlConnection("Data Source=(LocalDb)\v11.0;AttachDBFilename='E:\\TodayData\\DatabaseLogin\\LoginExample\\LoginExample\\App_Data\\DatabaseLoginExample.mdf';Integrated Security=SSPI;"))
        {
            string _sql = "SELECT [Username] FROM [dbo].[LoginInformation] WHERE [Username] = @u AND [Password] = @p";
            var cmd = newSqlCommand(_sql, conn);
            cmd.Parameters.Add(newSqlParameter("@u", SqlDbType.NVarChar)).Value = _username;
            cmd.Parameters.Add(newSqlParameter("@p", SqlDbType.NVarChar)).Value = SHA1.Encode(_password);

            conn.Open();

            var reader = cmd.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Dispose();
                cmd.Dispose();
                return true;
            }
            else
            {
                reader.Dispose();
                cmd.Dispose();
                return false;
            }
        }
    }
}

In conn.Open() I am getting this Error

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

SqlException Was Unhandeled by User Code..

Can anyone help me to solve this

هل كانت مفيدة؟

المحلول

Just modify your connection string with the following code:

using (var conn = new SqlConnection(@"Data Source=(LocalDb)\v11.0;AttachDBFilename='E:\TodayData\DatabaseLogin\LoginExample\LoginExample\App_Data\DatabaseLoginExample.mdf';Integrated Security=True"))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top