question

SajoNez1510-5981 avatar image
0 Votes"
SajoNez1510-5981 asked SimpleSamples edited

Problem with configuration file

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>



dotnet-csharp
1506.png (180.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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



figure1.png (31.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples edited

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.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.