显示多个查询结果C# [英] Display multiple Query results C#

查看:101
本文介绍了显示多个查询结果C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从单个查询输出多个结果的方法,并能够对其进行迭代,以便可以将单个结果与表单字段"进行匹配.这是一个更好的示例,而这正是我正在尝试做的事情.

I am looking for a way to output multiple results from a single query, and be able to iterate through them so that I can Match single results to Form Fields. Here is a better example, and exactly what I am trying to do.

string sql = "SELECT site_name, meter_no, address FROM meter "
 + "WHERE property_id = 39 "
 + "ORDER BY meter_no ASC LIMIT 2;";



我正在使用itextsharp将当前查询结果输出到PDF表单.站点名称,我只需要返回一次,但是有时,每个站点名称都附有meter_no的多个结果.该站点名称进入一个表单字段,然后不再被触摸. meter_no被放置在另一个字段中,但是我希望能够浏览"结果,并根据与相同站点名称相关联的后续meter_no来填写更多字段.使用C#可以吗?我正在使用Visual Studio,而我的数据库是Postgresql.

如果您需要任何说明,请不要犹豫,我不确定我是否做了适当的工作来确切说明我需要完成的工作.



I am outputting the result of this query currently to a PDF Form using itextsharp. The Site name, I only have to return once, but sometimes, there are multiple results of meter_no attached to each site name. The site name goes into a form field, and then isn''t touched again. The meter_no is put into another field, but I want to be able to "Tab" through the results and fill in more fields based off of subsequent meter_no''s tied to the same site name. Is this possible using C#? I am using Visual Studio, and My Database is a Postgresql.

If you need any clarification, do not hesitate to ask, I''m not sure if I did a suitable job explaining exactly what I need to accomplish.

推荐答案

try
{
    conn.Open();
    NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
    NpgsqlDataReader dr = cmd.ExecuteReader();
    //NpgsqlTransaction tran = conn.BeginTransaction();

    while (dr.Read())
        //Retrieve Site Name and the first Meter Number/Address attached to the property id
    {
        String site_nameString = dr.GetString(0);
        String meterString = dr.GetString(1);
        String addressString = dr.GetString(2);

        while (dr.Read())
            //Retrieve the second Meter Number/Address.
        {
            NpgsqlCommand subCmd1 = new NpgsqlCommand(subSql1, subConn1);
            String meter2String = dr.GetString(1);
            String address2String = dr.GetString(2);

            while (dr.Read())
                //Retrieve the third Meter Number/Address.
            {
                NpgsqlCommand subCmd2 = new NpgsqlCommand(subSql2, subConn2);
                String meter3String = dr.GetString(1);
                String address3String = dr.GetString(2);


除了Mike的解决方案外,您还应该紧急使用Parameter属性来避免查询中出现SQL注入.


In Addition to Mike''s solution you should urgently use Parameter Property to avoid SQL-Injection in your query.


string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
        + "WHERE CustomerID = @ID;";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(commandText, connection);
        command.Parameters.Add("@ID", SqlDbType.Int);
        command.Parameters["@ID"].Value = customerID;

        // Use AddWithValue to assign Demographics.
        // SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml);

        try
        {
            connection.Open();
            Int32 rowsAffected = command.ExecuteNonQuery();
            Console.WriteLine("RowsAffected: {0}", rowsAffected);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }



问候



Regards


这篇关于显示多个查询结果C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆