We have a SQL Server DB setup on Azure VM. We are trying to create an Azure Function App with PowerShell timerTrigger that will make an API call and store result of that into our DB.
Able to make the API call via Azure function successfully and read that data, but getting issues while writing into DB.
ERROR Snippet:
ERROR: Exception calling "Open" with "0" argument(s): "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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
CODE Snippet:
# Define connection parameters
$serverName = "XXX"
$databaseName = "XXX"
$userId = "XXX"
$password = "XXX"
$connectionString = "Server=$serverName;Initial Catalog=$databaseName;User Id=$userId;Password=$password;"
# Open the SQL Server connection
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
# Define the SQL query to insert data
$query = "TRUNCATE TABLE [dbo].[Table_1]; INSERT INTO [dbo].[Table_1] ([Test]) VALUES ('" + $accessTokenTrimmed + "');"
# Run query
$command = $connection.CreateCommand()
$command.CommandText = $query
$command.ExecuteNonQuery()
$connection.Close()


