gpt4 book ai didi

C# ASP.NET 从同一个 mysql 命令中选择多列并在代码隐藏中使用它们

转载 作者:行者123 更新时间:2023-11-29 01:40:32 26 4
gpt4 key购买 nike

我会做这个东西:

"select nome,cognome from Utenti where CodiceCliente = @codice"

然后在后面的代码中使用“nome”中的值和“cognome”中的值。在 PHP 中有关联数组,在 C# ASP.NET 中?
'直到现在我总是使用两个不同的命令,但现在很烦人。
示例:

MySqlCommand getNome = new MySqlCommand("select Nome from Utenti where CodiceCliente = @codice", userConnection);
MySqlCommand getCognome = new MySqlCommand("select Cognome from Utenti where CodiceCliente = @codice", userConnection);
MySqlCommand getEmail = new MySqlCommand("select IndirizzoEmail from Utenti where CodiceCliente = @codice", userConnection);
getNome.Parameters.AddWithValue("@codice", codice);
getCognome.Parameters.AddWithValue("@codice", codice);
getEmail.Parameters.AddWithValue("@codice", codice);
userConnection.Open();
string nome = Convert.ToString(getNome.ExecuteScalar());
string cognome = Convert.ToString(getCognome.ExecuteScalar());
string email = Convert.ToString(getEmail.ExecuteScalar());

你能帮帮我吗?
之前谢谢,
丹妮尔

最佳答案

简单用法:

using(var connection = new MySqlConnection(myConnString))
{
var cmd = connection.CreateCommand();
cmd.CommandText = "select nome,cognome from Utenti where CodiceCliente = @codice";
cmd.Parameters.AddWithValue("@codice", value);
var reader = cmd.ExecuteReader();
while(reader.Read())
{
// here could be problems if database value is null
var nome = reader["nome"];
var cognome = reader["cognome"];
}
}

关于C# ASP.NET 从同一个 mysql 命令中选择多列并在代码隐藏中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032523/

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