Problem with configuration file

Sajo_Nez1510 21 Reputation points
2021-06-15T13:06:26.563+00:00

I want to create a connection to the server. The base is called NORTHWND. I wanted to create a connection using a configuration file. I created a configuration file. I want to get information about the version of the server I am using through the console application. However when I run the code I only get the following. It seems that I can´t log into database.
105851-1506.png

Here is the main code
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Data.SqlClient;
using System.Configuration;

namespace ConsoleApp1  
{  
   
    class Program  
    {  
        static void Main(string[] args)  
        {  
            
            SqlConnection conn = new SqlConnection();  
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;  
            try  
            {  
                using (conn)  
                {  
                    // Try to open the connection.  
                    conn.Open();  
                    Console.WriteLine("Server Version: " + conn.ServerVersion);  
                  
                }  
            }  
            catch (Exception err)  
            {  
                // Handle an error by displaying the information.  
                Console.WriteLine("Error reading the database. ");  
                Console.WriteLine(err.Message);  
            }  
            conn.Close();  
            Console.WriteLine("Now Connection Is:" + conn.State.ToString());  
  
        }  
    }  
}  

Here is the code of configuration file

<?xml version="1.0" encoding="utf-8" ?>  
  
  
<configuration>  
    <connectionStrings>  
        <add name="Northwind" connectionString="Data Source=localhost;Initial Catalog=NORTHWND;Integrated Security=SSPI"/>  
    </connectionStrings>  
  
</configuration>  
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-06-15T15:43:32.9+00:00

    Try creating a user then use the following with your user id and password

    <connectionStrings>  
    	<add name="Northwind" connectionString="Data Source=localhost;Initial Catalog=NORTHWND;User Id=user;Password=pwd;"/>  
    </connectionStrings>  
    

    Create the user in SSMS

    105901-figure1.png

    Or configure via IIS

    0 comments No comments

  2. Sam of Simple Samples 5,546 Reputation points
    2021-06-16T18:56:51.683+00:00

    Your code works for me. I get:

    Server Version: 14.00.2037
    Now Connection Is:Closed
    

    Are you sure your database is configured for Windows Authentication? In SSMS go to the database's properties then in the lower left click on View connection properties. Is the Authentication Method Windows Authentication? If this database was created for online use then it likely does not use Windows Authentication and you need to provide a user and password (probably the same as online) in the connection string, not Integrated Security=SSPI.

    If that is not the problem then the cause is somewhere other than what has already been posted.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.