简体   繁体   中英

Access a list in a class then insert into database

I'm having a problem on accessing the list in the class.

Below is the classes I've created. In class Datum, there's a list called location.

public class Location
{
    public string location { get; set; }
    public string remarks { get; set; }
    public string deliverydate { get; set; }
    public string pickupdate { get; set; }
}

public class Datum
{
    public string tracking_number { get; set; }
    public string pickup_place { get; set; }
    public string delivery_place { get; set; }
    public string tel_no { get; set; }
    public string status { get; set; }
    public DateTime lastupdate_time { get; set; }
    public List<Location> Location { get; set; }
}

public class Root
{
    public List<Datum> data { get; set; }
}

I want to use the List to insert the values into database. But below code gives an error:

'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?). at line

foreach ( var p in data.Root)
{
  string tracking_number = p.tracking_number;
  string pickup_place = p.pickup_place;
  string delivery_place = p.delivery_place;
  string tel_no = p.tel_no ;
  string status = p.status ;
  datetime lastupdate_time = p.lastupdate_time ;

  // start from this line, gives CS1061 error
  string location = p.Location.location ;
  string remarks = p.Location.remarks ;
  string deliverydate  = p.Location.deliverydate;
  string pickupdate = p.Location.pickupdate;

  query = "INSERT INTO Tracking (tracking_number, pickup_place, delivery_place, tel_no, status,
  lastupdate_time, location, remarks, deliverydate , pickupdate) VALUES (tracking_number,
  pickup_place, delivery_place, tel_no, status, lastupdate_time, location, remarks, deliverydate ,
  pickupdate);    
}

How can I solve this problem? I've tried to look for solutions, but nothing works.

Please try following code, you need another loop of location

foreach (var p in data.Root)
{
  string tracking_number = p.tracking_number;
  string pickup_place = p.pickup_place;
  string delivery_place = p.delivery_place;
  string tel_no = p.tel_no;
  string status = p.status;
  DateTime lastupdate_time = p.lastupdate_time;
    
  // Other information will be same for the all location
   foreach (var item in p.Location)
   {
    string location = item.location;
    string remarks = item.remarks;
    string deliverydate = item.deliverydate;
    string pickupdate = item.pickupdate;
         query = "INSERT INTO Tracking (tracking_number, pickup_place, delivery_place, tel_no, status,
      lastupdate_time, location, remarks, deliverydate , pickupdate) VALUES(tracking_number,
      pickup_place, delivery_place, tel_no, status, lastupdate_time, location, remarks, deliverydate,
      pickupdate);
   }
}

Suggestion

  1. Prepare your Root class.
  2. Pass your entire Root class as JSON string in SQL Server using newtonsoft json. Var json = Json.Serialize(rootObject)
  3. Grab this JSON in SQL Server and extract the Locations and other respective data from that JSON.
  4. Instead of using loops simply insert your locations as a set/subset using Insert / Select combination.

Your proc header should be like this

Create proc Usp_InsertDatum (@json nvarchar(max))

In C#

command.Parameters.AddWithValue("@json", json)

Here is a SQL shack link for JSON and SQL Server. https://www.sqlshack.com/how-to-parse-json-in-sql-server/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM