简体   繁体   English

使用阅读器从表中查询数据

[英]Query data from table using reader

Currently I try retrieve data of a register of a table but it doesn't retrieve anything, Any idea?. 目前,我尝试检索表的寄存器的数据,但它不检索任何内容,知道吗? I don't know if my script is wrong or i'm missing something. 我不知道我的脚本是否错误或缺少什么。 don't Capture nothing. 不要捕获任何东西。 Also i try with this query but i don't know if the Where is the correct form? 我也尝试与此查询,但我不知道哪里是正确的形式? SqlCommand comm = new SqlCommand("select clase, operacion from Clase_Documento where codigoafip = '"+ codafip +"' ", conn); SqlCommand comm =新的SqlCommand(“从clase_Documento中选择clase,操作,其中codigoafip ='” + codafip +“'”,conn);

Field codafip = (Field)doc.Fields["ZIMCodigoAFIP"];
Field clasedoc = (Field)doc.Fields["ZIMBlart"];
Field operacion = (Field)doc.Fields["ZIMOperacion"];

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost\\DOKUSTAR;Database=RdaDB10;User ID=bcc;Password=******;Trusted_Connection=Yes";
conn.Open();

SqlDataReader dr;
SqlCommand comm = new SqlCommand("select * from Clase_Documento", conn);
dr = comm.ExecuteReader();
while(dr.Read())
{
    if (codafip.Value == dr.GetString(4))
    {   
        string clasedoc2 = dr.GetString(1);
        string operacion2 = dr.GetString(5);
        clasedoc.Value = clasedoc2; //here put the data of the table in its respective field
        operacion.Value = operacion2; //here put the data of the table in its respective field
    }
}

Only to clarify, mi table is this, and the field that I want retrieve is "clase" and "operacion" depending of parameter codigoafip 为了澄清,mi表是这个,我想检索的字段取决于参数codigoafip是“ clase”和“ operacion” 在此处输入图片说明

Assuming your field names were claseDocField, operacionField and codafipField (and all string): 假设您的字段名称是claseDocField,operacionField和codafipField(以及所有字符串):

using (SqlConnection conn = new SqlConnection(@"server=.\DOKUSTAR;Database=RdaDB10;Trusted_Connection=Yes"))
using (SqlCommand comm = new SqlCommand(@"select top(1) clasedocField, operacionField
from Clase_Documento
where codafipField = @codafip", conn))
{
 comm.Parameters.Add("@codafip", SqlDbType.VarChar).Value = codafip.Value;
 conn.Open();

  SqlDataReader dr = comm.ExecuteReader();
  if(dr.Read())
  {
        clasedoc.Value = (string)dr["claseDocField"];
        operacion.Value = (string)dr["operacionField"];
  }
}

EDIT: 编辑:

    using (SqlConnection conn = new SqlConnection(@"server=.\DOKUSTAR;Database=RdaDB10;Trusted_Connection=Yes"))
    using (SqlCommand comm = new SqlCommand(@"select top(1) clase, operacion
    from Clase_Documento
    where codigoafip = @codafip", conn))
    {
     comm.Parameters.Add("@codafip", SqlDbType.VarChar).Value = codafip
;
     conn.Open();

      SqlDataReader dr = comm.ExecuteReader();
      if(dr.Read())
      {
            clasedoc.Value = (string)dr["clase"];
            operacion.Value = (string)dr["operacion"];
      }
      else 
      {
           clasedoc.Value = "Not found";
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM