Problem with configuration file

Sajo

Member
Joined
Jul 22, 2020
Messages
17
Programming Experience
Beginner
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.
1506.PNG

Here is the main code and code of config file
Main code
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
Config 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>
 

cbreemer

Well-known member
Joined
Dec 1, 2021
Messages
102
Programming Experience
10+
No, the database name is NORTHWND.
Well, the SQL server sample database is called Northwind, so I would also suspect a typo here.
But if you are sure it should be NORTHWND, does this database actually exist ? The error message suggests that it doesn't. Have you tried accessing this db, with the same credentials, within SQL server ?
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,083
Location
Chesapeake, VA
Programming Experience
10+
The screenshot on the OP seems to show that he is connected to the database under the "Data Connections" node on the left pane of Visual Studio. Unless he had to enter different credentials to make that connection, I would guess that he used his current identity just like in the connection string.

Also, the script for creating and populating the sample database uses the 8.3 name "northwnd.mdf":

All the OLEDB connection strings I found on Google use "NORTHWND", but all the SQL connection strings I've found use "Northwind". Since our OP is trying to use SQL, I would suggest trying "Northwind" just like @Neal and @cbreemer said.
 
Last edited:

cbreemer

Well-known member
Joined
Dec 1, 2021
Messages
102
Programming Experience
10+
The screenshot on the OP seems to show that he is connected to the database under the "Data Connections" node on the left pane of Visual Studio. Unless he had to enter different credentials to make that connection, I would guess that he used his current identity just like in the connection string.

Also, the script for creating and populating the sample database uses the 8.3 name "northwnd.mdf":
Yes indeed, I stand corrected. My SQL server expertise is getting a tad rusty since retirement :rolleyes:

So it seems like VS can log on to this database, but the C# code can't. Spot the differences ?
The connection string says Data Source=localhost . Does the ODBC data source localhost exist, and if yes, how does is that configured ?
If the problem is not apparent, it could be an idea to enable ODBC logging and check the log for more detail.
 
Top Bottom