我会做出这件事:
"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());
你能帮助我吗?
在此之前,谢谢
丹尼尔
答案 0 :(得分:2)
简单用法:
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"];
}
}
答案 1 :(得分:2)
你可以试试这个:
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();
if(reader.HasRows) {
while(reader.Read())
{
var nome = reader[0];
var cognome = reader[1];
}
reader.Close();
}
}
您必须检查DataReader
是否有行以避免异常,最后不要忘记关闭