Safet Nezic

Safet Nezic

  • NA
  • 17
  • 586

Why I can not get requested data when I open connection?

Jul 19 2021 4:28 PM

Hello. I want to open a connection to a local server, the NORTHWND database. When I open the connection it does not print the desired data I requested below. Here is the code and images of the code and code execution

using System;
using System.Linq;
using System.Configuration;

using System.Data.SqlClient;
using Microsoft.IdentityModel.Protocols;


namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
            try
            {
                using (sqlConnection)
                {
                    sqlConnection.Open();
                   Console. WriteLine("State of connection {0}", sqlConnection.State.ToString());
                    Console.WriteLine("Name of Base {0}", sqlConnection.Database);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                sqlConnection.Close();
            }
            
        }
    }
}

Here is the code of configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="Northwind" connectionString="Data Source=.;Initial Catalog=NORTHWND;Integrated Security=SSPI"/>
        
    </connectionStrings>
    </configuration>


Attachment: ConsoleApp2zip.zip

Answers (4)