所以我无法使用 dotnetcore 1.1 从 ubuntu 14.0.4 连接到 sql server。它是那些 sql 实例之一,它类似于 ipaddress\nameofinstance,其中省略 nameofinstance 会导致您进入不同的 sql server 实例。
代码:
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);
}
}
连接字符串(擦洗):
Data Source=255.255.255.255\\nameofinstance;User Id=UserName;Password=Password;Initial Catalog=DatabaseOne;MultipleActiveResultSets=True;
堆栈跟踪:
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
欢迎任何意见!
编辑:我想补充一点,此代码在连接到 ipaddress\nameofinstance 的 Windows 上完美执行。
编辑 2:我决定尝试使用另一种语言连接到数据库,并取得了成功。
这是我用来建立连接和执行过程的代码(Golang for 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")
}