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:
Safet NezicPosted Jul 21, 2021, 7:28 PM
I tried that but still I have the same problem.
Arnab MukherjeePosted Jul 21, 2021, 1:52 PM
Safet NezicPosted Jul 21, 2021, 1:22 PM
Arnab MukherjeePosted Jul 20, 2021, 6:34 PM
Hi, I have tested your C# code and it is working as expected. I have used a different database to test it with the below connection string.
Please try below steps to fix the issue.
1. First check, the database name 'NORTHWND' is correct or not which you have mentioned in your Config file.
2. Check if the Database can be accessable via SQL Server Management Studio.
3. You need to enable TCP/IP connection for the SQL Server edition which you are using. Your code is working for me because the TCP/IP is enabled for me and I am using SQL Server 2019 Express Edition.
Here is the steps and a link to enable TCP/IP for the SQL Server edition. Hope this will help you.
https://www.sqlshack.com/how-to-install-sql-server-express-edition/