I am connecting to a Database using following connection string.
Server=tcp:mydbserverURL,1433;Initial Catalog=mydb;Persist Security Info=False;User ID=sqluser;Password=mypassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;
There are two ways data is being accessed in my project.
- Using EF Core + Automapper. - Works Well. (using a generated DBContext.cs)
- Using ADO.NET Repository pattern for Accessing SPs(Does not work-fails complaining login failed for user sqluser/sqladmin)
I have tried the following to resolve this issue, but with no luck.
- Modifying connection string to include.
- Integrated Security=False - No Luck ;
- Trusted_Connection=True; - No Luck either.
- Connecting to a different DB With the same connection string (one without VNET integration) - No Luck here as well
- Created a new SQL User - No Luck
Snippet for ado.net - that is failing is below.
public DataTable GetDataByStoredProc(string storedProcName, IEnumerable<KeyValuePair<string, object>> parameters)
{
var connectionString = _dbContext.Database.GetDbConnection().ConnectionString;
DataTable dt = new DataTable();
try
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
using (SqlCommand sqlCommand = new SqlCommand(storedProcName, sqlConnection))
{
SqlDataAdapter adapt = new SqlDataAdapter(sqlCommand);
adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
if (parameters != null)
{
foreach (var parameter in parameters)
{
adapt.SelectCommand.Parameters.Add(new SqlParameter(parameter.Key, parameter.Value));
}
}
adapt.Fill(dt);
}
}
return dt;
}
catch (SqlException ex)
{
throw;
}
}
It is failing on Line SQLConnection.Open with the following error message.
Login failed for user SQLUser