So I am having trouble connecting to sql server from ubuntu 14.0.4 with dotnetcore 1.1. It is one of those sql instances where it is like ipaddress\nameofinstance where leaving out nameofinstance leads you to a different instance of sql server.
Code:
public void SetStatus(long id, LRStatus status, bool increment = false)
{
DynamicParameters param = new DynamicParameters();
param.Add("@id", id);
param.Add("@status", status == LRStatus.Initial ? null : status.ToString());
param.Add("@increment", id);
using (IDbConnection con = new SqlConnection(connectionStr))
{
con.Open();
con.ExecuteScalar<LRStaging>(sql: "usp_LR_SetStatus", param: param, commandType: CommandType.StoredProcedure);
}
}
Connection string(scrubbed):
Data Source=255.255.255.255\\nameofinstance;User Id=UserName;Password=Password;Initial Catalog=DatabaseOne;MultipleActiveResultSets=True;
Stacktrace:
System.Data.SqlClient.SqlException: 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: 35 - An internal exception was caught) ---> System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult)
at System.Net.Dns.<>c.<GetHostAddressesAsync>b__14_1(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.SqlClient.SNI.SNITCPHandle.<ConnectAsync>d__22.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait(TimeSpan timeout)
at System.Data.SqlClient.SNI.SNITCPHandle..ctor(String serverName, Int32 port, Int64 timerExpire, Object callbackObject, Boolean parallel)
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
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, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Shared.DataLayer.Db.SetStatus(Int64 id, LRStatus status, Boolean increment) in /home/name/Github/sln/Shared/DataLayer/Db.cs:line 41
at Worker.Automata.Automata.OnTransitionedAction(Transition transition) in /home/name/Github/sln/Worker/Automata/Automata.cs:line 307
at Stateless.StateMachine`2.InternalFireOne(TTrigger trigger, Object[] args)
at Stateless.StateMachine`2.InternalFire(TTrigger trigger, Object[] args)
at Worker.Automata.Automata.Start(LRStaging job) in /home/name/Github/sln/Worker/Automata/Automata.cs:line 95
at Worker.Program.processJob(Object sender, BasicDeliverEventArgs e) in /home/name/Github/sln/Worker/Program.cs:line 98
ClientConnectionId:00000000-0000-0000-0000-000000000000
Any input is welcome!
Edit: I would like to add that this code executes flawlessly on windows connecting to ipaddress\nameofinstance.
Edit 2: I decided to try and connect to the db using another language and it was a success.
Here is the code I used to establish a connection and to execute a procedure(Golang for the win).
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/denisenkom/go-mssqldb"
)
func main() {
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;database=%s", "255.255.255.255\\instance", "user", "password", "database")
fmt.Printf(" connString:%s\n", connString)
db, err := sql.Open("mssql", connString)
if err != nil {
log.Print("Open error")
log.Print(err.Error())
}
defer db.Close()
perr := db.Ping()
if perr == nil {
log.Print("No error")
} else {
log.Print("Ping error")
log.Print(perr.Error())
}
rows, err := db.Query("using IQDataOps; exec usp_GetMachineKeyValue ?, ?", "Machine1", "key")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var Value string
for rows.Next() {
err := rows.Scan(&Value)
if err != nil {
log.Fatal(err)
}
log.Println(Value)
}
log.Print("Complete")
}