我创建了一个返回JSON数据的存储过程:
ALTER PROCEDURE SpEmployeeSel
AS
BEGIN
SET NOCOUNT ON;
SELECT ve.*
FROM dbo.VwEmployee AS ve
FOR JSON PATH, INCLUDE_NULL_VALUES
END;
此查询输出:
[
{
"PersonId": 4,
"FirstName": "Anuj",
"MiddleName": "",
"LastName": "Tamrakar",
"DateofBirth": "2018-01-04T00:00:00",
"EmployeeId": 1,
"EmployeeCode": "Emp1",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 5,
"FirstName": "John",
"MiddleName": "",
"LastName": "Pradhan",
"DateofBirth": "2018-01-04T00:00:00",
"EmployeeId": 2,
"EmployeeCode": "Emp2",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 6,
"FirstName": "Priyanka",
"MiddleName": "",
"LastName": "Khadgi",
"DateofBirth": "2018-01-03T00:00:00",
"EmployeeId": 3,
"EmployeeCode": "Emp3",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 10,
"FirstName": "Sakar",
"MiddleName": "",
"LastName": "Thapa",
"DateofBirth": "2018-01-09T00:00:00",
"EmployeeId": 7,
"EmployeeCode": "Emp4",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 11,
"FirstName": "Aaa",
"MiddleName": "",
"LastName": "asdfasf",
"DateofBirth": "2018-01-03T00:00:00",
"EmployeeId": 8,
"EmployeeCode": "Emp5",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 12,
"FirstName": "Bibek",
"MiddleName": "",
"LastName": "Thapa",
"DateofBirth": "2018-01-11T00:00:00",
"EmployeeId": 9,
"EmployeeCode": "Emp6",
"DesignationId": 3,
"DesignationName": "Staff",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 13,
"FirstName": "dafgasdf",
"MiddleName": "",
"LastName": "asfsdf",
"DateofBirth": "2018-01-12T00:00:00",
"EmployeeId": 10,
"EmployeeCode": "Emp7",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 14,
"FirstName": "sdfsdf",
"MiddleName": "asdfsdaf",
"LastName": "asdfasdf",
"DateofBirth": "2018-01-03T00:00:00",
"EmployeeId": 11,
"EmployeeCode": "Emp8",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 15,
"FirstName": "Asdfasf",
"MiddleName": "asdf",
"LastName": "asdfasf",
"DateofBirth": "2018-01-05T00:00:00",
"EmployeeId": 12,
"EmployeeCode": "Emp9",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 16,
"FirstName": "asdfasf",
"MiddleName": "aasdfa",
"LastName": "asdfasf",
"DateofBirth": "2018-01-12T00:00:00",
"EmployeeId": 13,
"EmployeeCode": "Emp10",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 17,
"FirstName": "1111123123",
"MiddleName": "asdfasd",
"LastName": "asdfasdf",
"DateofBirth": "2018-01-05T00:00:00",
"EmployeeId": 14,
"EmployeeCode": "Emp11",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
}
]
您可以看到该过程在一列中输出预期的json数据。
现在在Visual Studio中,我试图像这样通过ADO.NET调用该过程
SqlConnection conn = new SqlConnection("//connection string path");
conn.Open();
SqlCommand command = new SqlCommand("SpEmployeeSEl", conn);
command.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
SqlDataAdapter sta = new SqlDataAdapter(command);
sta.Fill(dt);
conn.Close();
string result = dt.Rows[0][0].ToString();
现在当我查看输出结果时,我只得到一半的json数据,如下所示:
[
{
"PersonId": 4,
"FirstName": "Anuj",
"MiddleName": "",
"LastName": "Tamrakar",
"DateofBirth": "2018-01-04T00:00:00",
"EmployeeId": 1,
"EmployeeCode": "Emp1",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 5,
"FirstName": "John",
"MiddleName": "",
"LastName": "Pradhan",
"DateofBirth": "2018-01-04T00:00:00",
"EmployeeId": 2,
"EmployeeCode": "Emp2",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 6,
"FirstName": "Priyanka",
"MiddleName": "",
"LastName": "Khadgi",
"DateofBirth": "2018-01-03T00:00:00",
"EmployeeId": 3,
"EmployeeCode": "Emp3",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 10,
"FirstName": "Sakar",
"MiddleName": "",
"LastName": "Thapa",
"DateofBirth": "2018-01-09T00:00:00",
"EmployeeId": 7,
"EmployeeCode": "Emp4",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 11,
"FirstName": "Aaa",
"MiddleName": "",
"LastName": "asdfasf",
"DateofBirth": "2018-01-03T00:00:00",
"EmployeeId": 8,
"EmployeeCode": "Emp5",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 12,
"FirstName": "Bibek",
"MiddleName": "",
"LastName": "Thapa",
"DateofBirth": "2018-01-11T00:00:00",
"EmployeeId": 9,
"EmployeeCode": "Emp6",
"DesignationId": 3,
"DesignationName": "Staff",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 13,
"FirstName": "dafgasdf",
"MiddleName": "",
"LastName": "asfsdf",
"DateofBirth": "2018-01-12T00:00:00",
"EmployeeId": 10,
"EmployeeCode": "Emp7",
"DesignationId": 1,
"DesignationName": "Ceo",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:16:00"
},
{
"PersonId": 14,
"FirstName": "sdfsdf",
"MiddleName": "asdfsdaf",
"LastName": "asdfasdf",
"DateofBirth": "2018-01-03T00:00:00",
"EmployeeId": 11,
"EmployeeCode": "Emp8",
"DesignationId": 2,
"DesignationName": "Human Resource",
"InsertPersonId": 1,
"InsertDate": "2018-01-20T02:17:00"
},
{
"PersonId": 15,
"FirstName": "Asdfasf",
"MiddleName": "asdf
json字符串终止于中间。
我尝试查看dataTable的内容,并且字符串被分成两行,这导致代码dt.Rows[0][0].ToString()
仅返回第一行,这是问题的根本原因。
所以我不知道为什么字符串被分成两行。
然后我尝试用这样的实体框架调用该过程:
string result;
using (AccountingEntities db = new AccountingEntities())
{
result = db.SpEmployeeSel().FirstOrDefault();
}
但结果与Ado.Net相同,破坏了json字符串。
如果你们提供实体框架的解决方案而不是ADO.NET,我真的很感激。由于我的应用程序使用实体框架。
更新: 我为Entity Framework做的解决方法:
string jsonresult;
using (AccountingEntities db = new AccountingEntities())
{
List<string> jsonlist= db.SpTestSEl().ToList();
jsonresult = String.Join("", jsonlist.ToArray());
}
我为Ado.net做的解决方法:
注意:实体框架很糟糕。
答案 0 :(得分:0)
我没试过这个,不管它的工作是否是我发现的一个解决方案,请试试看。
var queryWithForJson = "SELECT ... FOR JSON";
var conn = new SqlConnection("<connection string>");
var cmd = new SqlCommand(queryWithForJson, conn);
conn.Open();
var jsonResult = new StringBuilder();
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
jsonResult.Append("[]");
}
else
{
while (reader.Read())
{
jsonResult.Append(reader.GetValue(0).ToString());
}
}