I need to add new records to my SQL database using C# and php. I’m using this code:
string server = "http://www.naseelco.com/scorm/populatecode.php";
foreach (string code in codeList)//codelist has 200 items
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(server);
request.Method = "POST";
string postData = "Code=" + code + "&Active=false";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
try
{
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
catch
{
}
}
This is the content of the populatecode.php file:
<?php
$Code = $_POST['Code'];
$Active = $_POST['Active'];
mysql_connect("localhost","myUserName","myPassword") or die ('Error updating database');
mysql_select_db("naseelc1_AC");
mysql_query("INSERT INTO codes (code,Active)VALUES ('$Code','$Active')");
echo $Code;
?>
But this code takes too much time to update the SQL database. My question is:
Is there a more efficient way to insert multiple records in a SQL database?
The first thing when analyzing performance problems is to test and verify where you lose the time, not to assume that it’s one special part of your system.
You say “takes too much time to update the SQL database“, how do you know it’s the INSERTs that are slowing you down?
First you have to measure the runtime of each part of your system. Then optimize that part.
Do not start optimization if you don’t know which part to optimize.
Use a profiler or simple time measuring code using
microtime()in PHP and the Stopwatch class in C# to calculate the runtime of a piece in your program to pinpoint the hotspots.Assuming you have done all that and still find that `INSERTìng a single row is taking too long, then there are several areas to explore. Too many indexes is probably the first thing to check.
Seeing your program, I’d bet that Anders Abel is right and you are losing your time in the HTTP overhead, not during INSERTs
An alternative approache would be to send the whole list to the PHP code and then using a multi-row
INSERTto insert everything with just a single statement:But I still believe it’s your concept of sending each code as one POST to the backend that is taking all the time…