将桌面c#app中的行插入基于Web的MySQL数据库

时间:2016-03-08 02:15:40

标签: c# php mysql

我有一个c#desktop application尝试以编程方式将行插入到基于Web的MySQL数据库中。我需要帮助来解决这个问题。

在网络端,我使用的网页包含要插入的列的表单。表单的提交转到PHP文件,该文件从发布的数据中插入行。页面/表单如下所示,当我从网站手动提交时,插入工作正常。

<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>test</title>
</head>
<body>
<form action="insertrow.php" method="POST">
<input type="text" name="Id"><br>
<input type="text" name="Name"><br>
<input type="submit" name="submit">
</form>
</body>
</html>

在桌面上,我使用WebRequest将数据发布到表单。这是我从MSDN复制的c#代码:

private static void UploadRow(int myId, string myName)
{
    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create("http://www.example.com/myform.html");
    // Set the Method property of the request to POST.
    request.Method = "POST";
    // Create POST data and convert it to a byte array.
    string postData = "Id=" + myId
                    + "&Name=" + myName;
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream();
    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);
    // Close the Stream object.
    dataStream.Close();
    // Get the response.
    WebResponse response = request.GetResponse();
    // Display the status.
    Console.WriteLine("Post Status=" + ((HttpWebResponse)response).StatusDescription);
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Display the content.
    Console.WriteLine(responseFromServer);
    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();
}

当我运行时,帖子状态是&#34; OK&#34;并且responseFromServer似乎是页面的HTML源。但是,没有任何内容插入数据库。如果我错过了如何使用WebRequest的问题,那我正在思考。我无法在线找到这个问题的任何答案。我很感激任何帮助。感谢。

0 个答案:

没有答案