我需要使用C#和php將新記錄添加到我的SQL數據庫。我使用此代碼:向SQL數據庫插入新行
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
{
}
}
這是populatecode.php文件的內容:
<?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;
?>
但這個代碼需要太多的時間來更新SQL數據庫。我的問題是:
有沒有一種更有效的方法來在SQL數據庫中插入多個記錄?
我很確定一隻小貓每次有人要求進行性能優化,同時保持所涉及的RDMS是祕密的,都會被着火。 –
你可以直接從C#插入它們,而不是http發佈到PHP? –
哇 - 安全問題!首先,一定要使用這個:http://php.net/manual/en/function.mysql-real-escape-string.php – Faust