I'm getting an error saying:
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.
I'm getting JSON-data from an API-call. And when I'm trying to insert this in my SQL Server database I'm getting the error. I'm trying to insert 3000 rows. How can I solve this issue? The call result from the APi is stored in the variable "body", and then deserialized into the variable "json".
This is my code:
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
var json = JsonConvert.DeserializeObject<Root>(body);
var Api = json.api;
string SqlString = "INSERT INTO land.leagues(" +
"league_id" +
",name" +
",type" +
",country" +
",country_code" +
",season" +
",season_start" +
",season_end" +
",logo" +
",flag" +
",standings" +
",is_current" +
",coverage_standings" +
",coverage_players" +
",coverage_topScorers" +
",coverage_predictions" +
",coverage_odds" +
",coverage_fixtures_events" +
",coverage_fixtures_lineups" +
",coverage_fixtures_statistics" +
",coverage_fixtures_playersStatistics" +
",created" +
") VALUES";
foreach (var a in Api.leagues)
{
SqlString += "(" +
"'" + a.league_id +
"','" + a.name.Replace("'","`") +
"','" + a.type +
"','" + a.country +
"','" + a.country_code +
"','" + a.season +
"','" + a.season_start +
"','" + a.season_end +
"','" + a.logo +
"','" + a.flag +
"','" + a.standings +
"','" + a.is_current +
"','" + a.coverage.standings +
"','" + a.coverage.players +
"','" + a.coverage.topScorers +
"','" + a.coverage.predictions +
"','" + a.coverage.odds +
"','" + a.coverage.fixtures.events +
"','" + a.coverage.fixtures.lineups +
"','" + a.coverage.fixtures.statistics +
"','" + a.coverage.fixtures.players_statistics +
"','" + DateTime.Now +
"'),";
}
SqlConnection con = new SqlConnection(@"_ConnectionString_");
SqlCommand cmd;
con.Open();
cmd = new SqlCommand("TRUNCATE TABLE land.leagues " + SqlString.Remove(SqlString.Length - 1), con);
cmd.ExecuteNonQuery();
}
These are all my classes:
public class Fixtures
{
public bool events { get; set; }
public bool lineups { get; set; }
public bool statistics { get; set; }
public bool players_statistics { get; set; }
}
public class Coverage
{
public bool standings { get; set; }
public Fixtures fixtures { get; set; }
public bool players { get; set; }
public bool topScorers { get; set; }
public bool predictions { get; set; }
public bool odds { get; set; }
}
public class League
{
public int league_id { get; set; }
public string name { get; set; }
public string type { get; set; }
public string country { get; set; }
public string country_code { get; set; }
public int season { get; set; }
public string season_start { get; set; }
public string season_end { get; set; }
public string logo { get; set; }
public string flag { get; set; }
public int standings { get; set; }
public int is_current { get; set; }
public Coverage coverage { get; set; }
}
public class Api
{
public int results { get; set; }
public List<League> leagues { get; set; }
}
public class Root
{
public Api api { get; set; }
}