我对将azure函数用于新的api Preview 2 .net核心感兴趣,因此我设置了一个小函数来查询数据库并向我返回结果。
public static class Function1
{
private readonly KPContext _context;
private static SqlConnection connection = new SqlConnection();
[FunctionName("Function1")]
public static Object Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
{
List<Employees> datastore = new List<Employees>();
Employees employees = new Employees();
string connstring = Environment.GetEnvironmentVariable("ConnectionStrings:SQlConnectionString");
var conn = new SqlConnection(connstring);
log.Info("C# HTTP trigger function processed a request.");
conn.Open();
var query = "select Employee_ID, Employee_LName, Employee_FName, CASE WHEN Username IS NULL THEN Employee_FName + '.' + Employee_LName ELSE Username end as Username from Employees";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
employees.Employee_ID = (int)reader[0];
employees.Employee_LName = (string)reader[1] != null ? (string)reader[1] : "";
employees.Employee_FName = (string)reader[2] != null ? (string)reader[2] : "";
employees.UserName = (string)reader[3] != null ? (string)reader[3] : "";
datastore.Add(employees);
}
return datastore;
}
}
}
所以这按预期工作,但最后只给我一个人作为所有结果,而不是给我每个单独的结果
因此,当我首先调试代码时,代码正在执行我希望遍历每一行的操作,然后将它们添加到我的数据存储中,然后突然间由于没有很好的解释,一条记录最终将其自身复制到了内部的其他所有记录上列表,即使它一直增加直到最后的数据存储计数,而且我有多次相同的记录,而不是我的所有员工。我从azure函数中提取了相同的确切代码,并放入了Web api .net core 2,它按预期运行。
答案 0 :(得分:2)
您一直在<!DOCTYPE html>
<html>
<head>
<title>button</title>
<style type="text/css">
.ButtonLeft, .ButtonRight{
display: inline-block;
position: relative;
margin: 0.5%;
padding: 0.652em 1.5em;
border: none;
border-radius: 1.5em;
color: #FFFFFF;
background-color: rgba(0, 10, 26, 0.75);
background: -webkit-gradient(
linear, left top, left bottom,
from(rgba(0, 10, 26, 0.0)),
to(rgba(0, 10, 26, 0.75)));
text-align: center;
font-family: Arial;
font-size: 1em;
font-weight: bold;
text-decoration: none;
outline: none;
transition: all 0.5s;
}
.ButtonLeft span{
display: inline-block;
position: relative;
left: -0.05em;
transition: 0.5s;
}
.ButtonLeft span:before{
content: '\00ab';
position: relative;
opacity: 0;
left: -0.05em;
transition: 0.5s;
}
.ButtonLeft:hover span{
padding-left: 1.7em;
left: -1.5em;
}
.ButtonLeft:hover span:before{
left: -1.5em;
opacity: 1;
color: inherit;
}
.ButtonRight span{
display: inline-block;
position: relative;
right: -0.05em;
transition: 0.5s;
}
.ButtonRight span:after{
content: '\00bb';
position: relative;
opacity: 0;
right: -0.05em;
transition: 0.5s;
}
.ButtonRight:hover span{
padding-right: 1.7em;
right: -1.5em;
}
.ButtonRight:hover span:after{
right: -1.5em;
opacity: 1;
color: inherit;
}
</style>
</head>
<body>
<button class="ButtonLeft"><span>i am lefty</span></button>
<br>
<br>
<button class="ButtonRight"><span>i am right</span></button>
</body>
循环中不断添加相同的Employees
实例。
在循环中创建一个新实例并将其插入。
while