Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure函数连接到本地sql访问被拒绝_C#_Sql Server_Azure_Azure Functions_Azure Storage Blobs - Fatal编程技术网

C# Azure函数连接到本地sql访问被拒绝

C# Azure函数连接到本地sql访问被拒绝,c#,sql-server,azure,azure-functions,azure-storage-blobs,C#,Sql Server,Azure,Azure Functions,Azure Storage Blobs,我遇到了一个问题,即一旦发布到Azure,我就无法通过Azure function应用程序连接到本地SQL数据库。但是,当我在VisualStudio代码中本地运行它时,它工作得非常好 该函数的目的是将存储在Azure blob存储中的图像调整为缩略图,并将其存储在表字段中 以下是我收到的错误消息: 2020-12-15T15:33:52.058 [Information] A network-related or instance-specific error occurred while e

我遇到了一个问题,即一旦发布到Azure,我就无法通过Azure function应用程序连接到本地SQL数据库。但是,当我在VisualStudio代码中本地运行它时,它工作得非常好

该函数的目的是将存储在Azure blob存储中的图像调整为缩略图,并将其存储在表字段中

以下是我收到的错误消息:

2020-12-15T15:33:52.058 [Information] 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)

2020-12-15T15:33:52.093 [Error] Executed 'BlobTrigger_MQS_Resize' (Failed, Id=346672c6-820c-43f6-b950-d5059e44697e, Duration=19570ms)
Access is denied.
我通过SQL登录连接到SQL数据库,这非常有效。连接字符串位于local.settings.json中:

{
“IsEncrypted”:错误,
“价值观”:{
...,
“连接\u SQL\u数据库\u MQS”:“数据源=MyServer;初始目录=MyDB;用户ID=MyUser;密码=MyPassword;连接超时=30;加密=False;信任服务器证书=False;应用内容=ReadWrite;多子网故障转移=False”
}
}
代码(用C#表示),我将其简化为问题的核心:

public static class BlobTrigger_MQS_Resize
    {
        [FunctionName("BlobTrigger_MQS_Resize")]
        public static async Task Run([BlobTrigger("mqs-media/{name}", Connection = "hqdevstorage_STORAGE")]Stream input, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {input.Length} Bytes");

            try
            {
                var connectionString = Environment.GetEnvironmentVariable("Connection_SQL_Database_MQS", EnvironmentVariableTarget.Process);

                using (var output = new MemoryStream())
                using (Image<Rgba32> image = Image.Load(input))
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    // Code that resizes images to 'output', this works also when published
                    image.Resize(...);           
                    image.Save(output, encoder);

                    //PROBLEM IS HERE: Write resized image to SQL Database
                    conn.Open();
                    var query = "UPDATE dbo.MyTable" 
                    + " SET Blob = @output"  
                    + " WHERE File_Name = '" + name + "'";

                    using(SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@output", output);
                        var rows = await cmd.ExecuteNonQueryAsync();
                        log.LogInformation($"{rows} rows were updated");
                    }
                }
                
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
                throw;
            }
             
        }
    }
公共静态类BlobTrigger\u MQS\u Resize
{
[FunctionName(“BlobTrigger_MQS_Resize”)]
公共静态异步任务运行([BlobTrigger(“mqs media/{name}”,Connection=“hqdevstorage_STORAGE”)]流输入,字符串名称,ILogger日志)
{
log.LogInformation($“C#Blob触发器函数已处理Blob\n名称:{Name}\n大小:{input.Length}字节”);
尝试
{
var connectionString=Environment.GetEnvironmentVariable(“连接\ SQL \数据库\ MQS”,EnvironmentVariableTarget.Process);
使用(var output=newmemoryStream())
使用(Image=Image.Load(输入))
使用(SqlConnection conn=newsqlconnection(connectionString))
{
//将图像大小调整为“输出”的代码,在发布时也可以使用
图像。调整大小(…);
图像保存(输出、编码器);
//问题在于:将调整大小的图像写入SQL数据库
conn.Open();
var query=“更新dbo.MyTable”
+“设置Blob=@output”
+“其中文件名='”+Name+“'”;
使用(SqlCommand cmd=newsqlcommand(查询,连接))
{
cmd.Parameters.AddWithValue(“@output”,output);
var rows=await cmd.ExecuteNonQueryAsync();
log.LogInformation($“{rows}行已更新”);
}
}
}
捕获(例外情况除外)
{
日志、登录信息(例如消息);
投掷;
}
}
}
有人能帮我吗?我不知道为什么它在本地工作,但在Azure上不工作

找不到服务器或无法访问服务器

这是一个网络连接问题,而不是权限问题。Azure函数无法将SQL Server的主机名解析为IP地址,如果可以,将无法连接到该IP地址

您的函数需要访问on-prem连接的VNet,或者使用混合连接来访问on-prem SQL Server

找不到服务器或无法访问服务器

这是一个网络连接问题,而不是权限问题。Azure函数无法将SQL Server的主机名解析为IP地址,如果可以,将无法连接到该IP地址

您的函数需要访问on-prem连接的VNet,或者使用混合连接来访问on-prem SQL Server


首先,请修复您的巨大安全漏洞。你清楚地知道参数化,那么为什么你要参数化
@output
,而注入
name
?@Larnu,你是对的。谢谢你的提醒,首先,请修复你的巨大安全漏洞。你清楚地知道参数化,那么为什么你要参数化
@output
,而注入
name
?@Larnu,你是对的。谢谢你的提醒