我试图在我的SQL Server express数据库中插入一条简单记录,该数据库是在EC2实例(非RDS)上手动安装的。但是,在发布到AWS lambda并尝试调用之后,我收到此错误:
{
"errorType": "SqlException",
"errorMessage": "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: 25 - Connection string is not valid)",
"stackTrace": [
"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 AWSDB.Function.FunctionHandler(String input, ILambdaContext context)",
"at lambda_method(Closure , Stream , Stream , ContextInfo )"
]
}
我还创建了一个简单的单独的.net核心控制台应用程序,并成功地将记录添加到数据库中。
以下是我使用的代码:
var connectionString = @"Server=tcp:[serverIP]\SQLEXPRESS,1433;Initial Catalog=apilambda; User ID=[user];Password=[password]";
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into accounts (Fname,Lname,number) values (@fname,@lname,@number)";
cmd.Parameters.AddWithValue("@fname", des.name.firstName);
cmd.Parameters.AddWithValue("@lname", des.name.lastName);
cmd.Parameters.AddWithValue("@number", des.number.ToString());
cmd.Connection = connection;
cmd.ExecuteNonQuery();
context.Logger.LogLine("****end order ****");
}