Azure功能可同时连接到Blob和SQL存储 [英] Azure function to connect to both blob and sql storage
问题描述
我有一个我认为它将是Azure函数的完美介绍测试用例.
I have what I thought would be a perfect introduction test case for Azure functions.
我们在Azure(SQL数据库)中有一个SQL存储.
We have a SQL store in Azure (SQL database).
使用C#,VS 2017更新了Azure工作流项目.
Using C#, VS 2017, updated Azure workflow items.
我们还有一个Blob存储区. 每天晚上,我需要一个过程来访问SQL数据库,根据条件收集一组记录,然后对其进行处理,以生成将存储在Blob存储中的文件.
We also have a Blob storage. Each night I need a process to go to the SQL database, collect a group of records based on a criteria and then process them generating a file that will be stored in the blob storage.
我似乎无法克服完成这些任务中的任何一个的困难.所有这些教程似乎都是最基本的类型.
I cannot seem to get over the hump of getting either of these tasks done. All of the tutorials seem to be of the most basic type.
我在VS 2017中创建了一个函数,但第一步只是连接到SQL数据库.
I have a function created in VS 2017, but first step is just to connect to the SQL database.
我要添加一个新项目:ADO.NET实体数据模型,它的行为就像它正确创建了模型一样,但是没有数据上下文?
I went to add new item: ADO.NET Entity Data Model, and it acted like it created the model correct but there is not a data context for it?
因此,我决定尝试下一步-创建blob,然后仅使用经过硬编码的示例数据.再次...找不到有关如何执行此操作的良好指南.
So, I decided to try the next step -- creating the blob and just use sample data hard coded. Again...cannot find a good guide on how to do that.
我在local.setting.json文件中有一个"AzureWebJobsStorage"设置.
I have a "AzureWebJobsStorage" setting in the local.setting.json file.
我在下面具有计时器功能:
I have a timer function below:
public static class Function1
{
[FunctionName("Function1")]
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, TraceWriter log)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
只要有一些指向正确的方向,我就会继续猛击它...
Just some pointing in the right direction and I will keep banging away at it...
乔
推荐答案
因此,您需要一个具有以下功能的函数
So, you need a function with
- 计时器触发器-与您一样
- Blob输出绑定(以保存输出Blob)-请参阅使用Azure Functions连接到Azure SQL数据库提供了一个有关ADO.NET的示例.
- Timer trigger - as you already did
- Blob output binding (to save the output blob) - see "Using a blob output binding" section in Blob storage bindings
- SQL query - Azure function don't provide a binding for this, just use whatever you normally use in your C# projects. Use Azure Functions to connect to an Azure SQL Database gives an example with ADO.NET.
这是我为您准备的功能的草图:
Here is my sketch for your function:
[FunctionName("Function1")]
public static void Run(
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
[Blob("mycontainer/myblob.txt", FileAccess.Write)] out string outputBlob,
TraceWriter log)
{
var str = ConfigurationManager.ConnectionStrings["sqldb_connection"].ConnectionString;
using (var conn = new SqlConnection(str))
{
conn.Open();
var text = "SELECT count(*) FROM MyData";
using (var cmd = new SqlCommand(text, conn))
{
// Execute the command and log the # rows affected.
var rows = cmd.ExecuteScalar();
outputBlob = $"{rows} rows found";
}
}
}
您的问题有点开放性,请随时使用有关您的挣扎的更多详细信息进行更新.
Your question is a bit open-ended, so feel free to update it with more details about your struggles.
这篇关于Azure功能可同时连接到Blob和SQL存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!