我有以下代碼,我想用 where 陳述句進行查詢,但它沒有給我任何結果,你能幫我嗎
public List<Persona> Listarwhere()
{
List<Persona> oLista = new List<Persona>();
using (SQLiteConnection conexion = new SQLiteConnection(cadena))
{
conexion.Open();
Persona obj = new Persona();
Form1 idcliente = new Form1();
//int cliente = int.Parse(idcliente.txtidpersona.Text);
string query = "select IdPersona, Nombre, Apellido, Telefono from Persona where idpersona = @idpersona";
SQLiteCommand cmd = new SQLiteCommand(query, conexion);
cmd.Parameters.Add(new SQLiteParameter("@idpersona", idcliente.txtidpersona.Text));
cmd.CommandType = System.Data.CommandType.Text;
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
oLista.Add(new Persona()
{
IdPersona = int.Parse(dr["IdPersona"].ToString()),
Nombre = dr["Nombre"].ToString(),
Apellido = dr["Apellido"].ToString(),
Telefono = dr["Telefono"].ToString()
});
}
}
}
return oLista;
}
我需要在幾個文本框中傳遞這個查詢的資料
uj5u.com熱心網友回復:
您應該嘗試將您的 'idpersona' 作為引數傳遞給您的方法。......也許是這樣的:
public List<Persona> Listarwhere(string id) // or (int id)
{
List<Persona> oLista = new List<Persona>();
using (SQLiteConnection conexion = new SQLiteConnection(cadena))
{
conexion.Open();
Persona obj = new Persona();
//Form1 idcliente = new Form1();
//int cliente = int.Parse(idcliente.txtidpersona.Text);
string query = "select IdPersona, Nombre, Apellido, Telefono from Persona where idpersona = @idpersona";
SQLiteCommand cmd = new SQLiteCommand(query, conexion);
cmd.Parameters.Add(new SQLiteParameter("@idpersona", id));
cmd.CommandType = System.Data.CommandType.Text;
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
oLista.Add(new Persona()
{
IdPersona = int.Parse(dr["IdPersona"].ToString()),
Nombre = dr["Nombre"].ToString(),
Apellido = dr["Apellido"].ToString(),
Telefono = dr["Telefono"].ToString()
});
}
}
}
return oLista;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/315635.html
上一篇:同步呼叫異步函式更靈敏
下一篇:檢查重復隨機輸出的更有效方法?
