TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Pankaj Singh
NA
348
95k
Not able to connect Sql Server(Local) from App in Docker Container
Oct 26 2020 9:23 AM
I have created a simple .NET Core 3.1 Console app to connect to SQL Server using ADO.NET. It is working fine with dotnet run and returns the result. But when I deployed this app on docker, I get a network-related error.
My console app code is as follows:
using
System;
using
System. Data;
using
System.Data.SqlClient;
namespace
app1
{
class
Program
{
static
void
Main(
string
[] args)
{
SqlConnection con =
new
SqlConnection(
"Data Source=host.docker.internal,1433;Initial Catalog = SampleDb;Persist Security Info=False;User Id=sa;Password=password0;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;"
);
con.Open();
SqlDataAdapter ad =
new
SqlDataAdapter(
"select * from employee"
, con);
con.Close();
DataTable dt =
new
DataTable();
ad.Fill(dt);
for
(
int
i = 0; i < dt.Rows.Count; i++)
{
int
a = Convert.ToInt32(dt.Rows[i][0]);
string
b = dt.Rows[i][1].ToString();
string
c = dt.Rows[i][2].ToString();
string
d = dt.Rows[i][3].ToString();
int
e = Convert.ToInt32(dt.Rows[i][4]);
Console.WriteLine(
"{0}\t{1}\t{2}\t{3}\t{4}"
,a,b,c,d,e);
}
}
}
}
and Dockerfile code is:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /app
COPY
--from
=
build
-env /app/out .
ENTRYPOINT [ "dotnet","app1.dll" ]
Error is :
Unhandled exception.
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
System.ComponentModel.Win32Exception (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at app1.Program.Main(String[] args) in C:\app\Program.cs:line 15 ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:10060,State:0,Class:20
Reply
Answers (
1
)
How to get data from multiple textboxes
What is the use of Indexers in C# and How to create one.