gpt4 book ai didi

azure - 如何在Azure Function中使用LinQ lambda表达式并将其与Azure SQL连接

转载 作者:行者123 更新时间:2023-12-03 01:41:40 25 4
gpt4 key购买 nike

这是我的问题。我有 Azure Function,它与 Azure SQL 连接正常。我在 Azure SQL 中有 1 个表是 A

A => ID |姓名值为 1 |德克

当我尝试在 azure 函数中使用 LinQ 时,例如

    var x="";
int i=1;
using(MyDB db=new MyDB())
{
x=db.A.where(x=>x.Id == i).select(x=>x.name).FirstOrDefault();
}

我希望它显示“x = Duc”。但它没有显示任何内容?

我的AzureFunction是由VS 2017版本部署的

net461 v1

感谢您的阅读。

最佳答案

您最好提供更多详细信息,例如您的数据映射类和 function.cs 中的详细信息。

为了确保连接字符串使用正确,您可以使用 log.Info(connectionstring) 来检查它是否使用正确。

我用下面的简单代码进行了测试,可以从azure sql数据库正确获取数据。(VS 2017和函数v1 net461)。

第1步:在azure数据库中创建一个表: enter image description here

第2步:创建azure function v1项目:

2.1:以下是数据库映射代码,添加一个名为test的新.cs文件:

using System.Data.Linq.Mapping;

namespace FunctionApp11
{
[Table(Name ="test")]
public class test
{
private short _id;
[Column(IsPrimaryKey = true, Storage = "_id")]
public short id
{
get
{
return this._id;
}
set
{
this._id = value;
}

}

private string _name;
[Column(Storage = "_name")]
public string name
{
get
{
return this._name;
}
set
{
this._name = value;
}
}

}
}

2.2:Function类中的代码(我只是使用硬代码作为连接字符串):

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System;
using System.Data.Linq;
using System.Linq;

namespace FunctionApp11
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([TimerTrigger("*/30 * * * * *")]TimerInfo myTimer, TraceWriter log)
{
string str = "Server=tcp:your_server_name.database.windows.net,1433;Initial Catalog=your_db_name;Persist Security Info=False;User ID=user_name;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
int i = 3;

DataContext db = new DataContext(str);
Table<test> students = db.GetTable<test>();
var query = students.Where(x => x.id == i).Select(x => x.name).FirstOrDefault();

log.Info($"the first student is:{query}");

log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
}

3.发布到azure,并开始运行,可以看到数据被抓取了。 enter image description here

关于azure - 如何在Azure Function中使用LinQ lambda表达式并将其与Azure SQL连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52756715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com