【发布时间】:2011-11-13 10:12:40
【问题描述】:
我需要使用 C# 和 php 将新记录添加到我的 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?
-
哇——安全问题!首先,一定要使用这个:php.net/manual/en/function.mysql-real-escape-string.php
-
@Erwin:RDMS 是 MySQL,因此是 mysql_connect、mysql_select_db 和 mysql_query
标签: c# php mysql sql performance