c# - 如何使用 where 使这个 SQL 语句工作
问题描述
我有以下代码,我想用 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;
}
我需要在几个文本框中传递这个查询的数据
解决方案
您应该尝试将您的“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;
}
推荐阅读
- reactjs - React 将受控错误变为不受控制的错误让我发疯,我做错了什么?
- performance - 我可以改善 Rust zip crate 的糟糕性能吗?
- html - nodejs获取另一个页面
- gams-math - GAMS:获取向量的第 n 个元素
- android - 如何在布局中制作像谷歌支付这样的缺口
- xss - 有人可以给我一个反射 xss 的真实场景吗?
- javascript - Jasmine spyOn 不适用于窗口功能
- html - 如果超出可见屏幕,如何找到我的下拉 div?
- javascript - 如何将我的 csv 文件组织成 Javascript
- python - 如何在不执行“find_element”的情况下使用 selenium 定义按钮?(Python)