我创建了一个Azure函数(Http触发器),该函数通过HttpRequestMessage从CSV文件读取数据。为了向您展示我的读者代码,我将其包含在这篇文章中。
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req, TraceWriter log) {
try
{
// This will read the CSV Data
var multipart = await req.Content.ReadAsMultipartAsync();
// This gets CSV names
foreach (var content in multipart.Contents)
{
using (StreamReader sr = new StreamReader(await content.ReadAsStreamAsync()))
{
string line;
while ((line = sr.ReadLine()) != null)
{
log.Info(line);
}
}
}
}
我可以通过以下方式进行测试:将我的CSV文件上传到邮递员中,然后点击端点以获取包含CSV中所有数据的响应。我创建了还反映所有表关系的Azure SQL数据库。我现在想将从此函数读取的数据写入相应的表中。我不知道是否可以在同一Azure函数中执行此操作。
谢谢您的帮助!
答案 0 :(得分:0)
尝试使以下代码适合您的功能:
var cnnString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
using(var connection = new SqlConnection(cnnString))
{
connection.Open();
// You read your CSV
var rLog = await req.Content.ReadAsAsync<LogRequest>();
connection.Execute("INSERT INTO [dbo].[LogRequest] ([Log]) VALUES (@Log)", rLog);
}