Я хочу вставить строку JSON в таблицу, десериализовать строку json в список объектов, а затем вставить в таблицу, но не работает. Мой код:
Мои данные JSON
{"data":[{"ID":"1","Personnel_Number":"1001","Employee_Name":"Employee 1","Password":"12345","Gender":"M","Grade":"W5","Designation":"4","SBU_ID":"1","Department":"1","Category":"1","Email_ID_Official":"","Email_ID_Personal":"","Mobile_Number":"","Current_Address":"","Permanent_Address":"","Aadhaar_No":"","DOB":"","DOJ":"","DOM":"","Marital_Status":"","Profile_Pic_Path":"http://iprofileapi.dev.app6.in/UploadedFiles/6365794593150041732018329.jpg","Remarks":"","Status":"Active","Created_By":"dbo","Created_On":"20-09-2017 19:35:13","Updated_By":"3872","Updated_Date":"09-01-2019 12:48:00"},{"ID":"2","Personnel_Number":"1002","Employee_Name":"Employee 2","Password":"12345","Gender":"M","Grade":"W5","Designation":"153","SBU_ID":"6","Department":"28","Category":"1","Email_ID_Official":"","Email_ID_Personal":"","Mobile_Number":"","Current_Address":"","Permanent_Address":"","Aadhaar_No":"","DOB":"","DOJ":"","DOM":"","Marital_Status":"","Profile_Pic_Path":"","Remarks":"","Status":"Active","Created_By":"dbo","Created_On":"20-09-2017 19:35:13","Updated_By":"4957","Updated_Date":"10-02-2018 19:06:58"},{"ID":"3","Personnel_Number":"1003","Employee_Name":"Employee 3","Password":"12345","Gender":"M","Grade":"W5","Designation":"41","SBU_ID":"1","Department":"3","Category":"1","Email_ID_Official":"","Email_ID_Personal":"","Mobile_Number":"","Current_Address":"","Permanent_Address":"","Aadhaar_No":"","DOB":"","DOJ":"","DOM":"","Marital_Status":"","Profile_Pic_Path":"","Remarks":"","Status":"Active","Created_By":"dbo","Created_On":"20-09-2017 19:35:13","Updated_By":"5482","Updated_Date":"05-10-2017 15:36:16"},{"ID":"4","Personnel_Number":"1004","Employee_Name":"Employee 4","Password":"12345","Gender":"M","Grade":"W2","Designation":"120","SBU_ID":"26","Department":"88","Category":"1","Email_ID_Official":"","Email_ID_Personal":"","Mobile_Number":"","Current_Address":"","Permanent_Address":"","Aadhaar_No":"","DOB":"","DOJ":"","DOM":"","Marital_Status":"","Profile_Pic_Path":"","Remarks":"","Status":"Active","Created_By":"dbo","Created_On":"20-09-2017 19:35:13","Updated_By":"6298","Updated_Date":"06-10-2017 13:30:20"},{"ID":"5","Personnel_Number":"1005","Employee_Name":"Employee
И т. Д. ......
сначала я создаю класс для всех элементов на одной и той же основе. Я не публикую все данные, потому что они будут слишком длинными, их примерные данные:
public class tblEmployee
{
public string ID { get; set; }
public string Personnel_Number { get; set; }
public string Employee_Name { get; set; }
public string Password { get; set; }
}
Затем ясоздать еще один класс для списка элементов:
public class SalesTransactions
{
public List<tblEmployee> transactions { get; set; }
public int count { get; set; }
}
И затем я должен создать данные вставки в таблицу сервера SQL при нажатии кнопки
protected void btninsertjsondata_Click(object sender, EventArgs e)
{
var conString = ConfigurationManager.ConnectionStrings["SQLDBConnection1"];
string strConnString = conString.ConnectionString;
SqlConnection conn = new SqlConnection(strConnString);
SqlCommand com;
string data = HttpContent("https://localhost:45333/Retrive_Jsondata.aspx");
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
SalesTransactions table = JsonConvert.DeserializeObject<SalesTransactions>(data);
conn.Open();
com = new SqlCommand("INSERT INTO dbo.tbl_Iprofile_Employee select * from " + table, conn);
com.CommandType = CommandType.Text;
int refId = com.ExecuteNonQuery();
conn.Close();
if (refId > 0)
{
Response.Write("{\"response\":{\"status\":\"success\",\"msg\":\"Details Saved Successfully..\"}}");
}
else
{
Response.Write("{\"response\":{\"status\":\"fail\",\"msg\":\"oops!! something went wrong\"}}");
}
}
При отладке моего кода в table
яполучаю Count 0
и transactions null
, поэтому мой sql-запрос не работает, и я получаю 'Неверный синтаксис рядом с' + '.'В Data
я получаю всю строку JSON, но не могу преобразовать ее в форму таблицы, где я делаю что-то не так, пожалуйста, помогите мне, если я добавлю цикл for для отображения всех столбцов и строк ???