gpt4 book ai didi

c# - 从连接到远程 MySQL 数据库的 PHP 服务检索/获取数据以显示到我的 ASP.Net Web 应用程序

转载 作者:行者123 更新时间:2023-11-29 05:07:01 25 4
gpt4 key购买 nike

所以我有一个远程(托管)MySQL 数据库,我通过 PHP 服务连接到该数据库。我需要我的 ASP.Net c# web 应用程序和我的 android 来与之通信。但是,我正在努力使用从服务中检索到的所有信息来填充我的 Web 应用程序模板。例如,我想填充用户的个人资料页面。

下面是我的 PHP 连接和与数据库的通信:

    `// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM Vendor Where VendorId = 2"; //this is just a test
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo . $row["id"]. . $row["BusinessName"]. . $row["Location"]. . $row["Email"]. .$row["Website"]. .$row["ProductType"]. .$row["Contact"]. .$row["PaymentOption"]. .$row["ProfileImg"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
`

然后(不共享我的所有设置)这将是 asp.net c# 与我的 PHP 文件/服务通信的代码示例。

public void getUserInfo(int id)
{


string BusinessName = lblBusiness.Text.Trim();
string email = lblEmail.Text.Trim();
string Contact = lblPhone.Text.Trim();

string location = lblLocation.Text.Trim();
string Website = lblWebsite.Text.Trim();

string payment = lblPayment.Text.Trim();

//Variables to get information from the service
Stream dataStream = null;
WebResponse response = null;
StreamReader reader = null;
//Stores the result from the server
string responseFromServer = null;
try
{
string requestMethod = "GET";
//Sending this data across the stream
string postData = "&Email=" + email + "&BusinessName=" + BusinessName + "&Website=" + Website + "&PaymentOption=" + payment + "&Location=" + location + "&ProductType=" + ProductType + "&Contact=" + Contact + "";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
string URL = "";// url of php service location
string contenttype = "application/x-www-form-urlencoded";
//Create link to web service
WebRequest request = WebRequest.Create(URL);
//Pass the request method
request.Method = requestMethod;
request.ContentType = contenttype;
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
//Get response from the server
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
catch (WebException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (dataStream != null && reader != null && response != null)
{
dataStream.Close();
reader.Close();
response.Close();
}
//Getting the response from the service
//string result = responseFromServer.ToString();

}

}

另外,我不确定从该函数返回什么。请帮忙。

最佳答案

您的 php 文件是我按的“API”。
你基本上需要从你的 php 文件中返回它。

$vendorArray = [
"Id" => $row["id"],
"BusinessName" => $row["BusinessName"],
// ... this is just pseudo code, convert the array or take the array or something like that
];

header('Content-type: application/json');
echo json_encode($vendorArray);

然后在 asp.net 中你做:

var deserializedVendor = JsonConvert.DeserializeObject<Vendor>(responseFromServer);

您的供应商类必须与您的 jsonObject 匹配才能反序列化

public class Vendor {

public string Id {get;set;}

public string BusinessName {get;set;}

...
}

这取决于你的 jsonResponse...

您还可以直接反序列化为具有供应商列表的复杂项目,如下所示:

var allTheVendorsDeserialized = JsonConvert.DeserializeObject<AllTheVendors>(responseFromServer);


public class AllTheVendors {

public bool Success {get;set}

public List<Vendor> {get;set}

}

哪里 php:

$arr = ["Success" => true, $myArrayOfVendors];

header('Content-type: application/json');
echo json_encode($arr);

关于c# - 从连接到远程 MySQL 数据库的 PHP 服务检索/获取数据以显示到我的 ASP.Net Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46020779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com