首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打印多个属性

打印多个属性
EN

Stack Overflow用户
提问于 2012-12-13 21:35:52
回答 1查看 151关注 0票数 0

我有一个程序,我需要打印出多个商品(vare)的属性。我遇到的问题是,它只多次打印出单个项目的属性。我尝试将属性放入一个二维数组中,但它仍然打印出相同的属性。我的输出文件是一个.txt文件。我想要的是让程序打印出一个商品列表,每个商品都有各自的属性。(有4个不同的项目和6个不同的属性)。

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Opgave_4._1
{
  class Program
{

        static void Main()
 {
    string connString;
    connString = "Data Source=(local);Initial Catalog=Occhi che guardano;Integrated Security=SSPI";

    string sqlstring = "SELECT * FROM Vare ORDER BY Vare.Varenavn";

    Console.WriteLine(sqlstring);

    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand(sqlstring, conn);
    conn.Open();
    SqlDataReader Reader = cmd.ExecuteReader();

    string att1;
    decimal att2;
    int att3;
    string att4;
    int att5;
    string att6;
    string[,] vareliste = new string[10, 10];
    StreamWriter Stream = null;
    String UdFil = "C:\\Users\\Thomas\\Desktop\\text.txt";
    Stream = new StreamWriter(UdFil);
    try
    { 
        while (Reader.Read())
        {
            if (Reader.IsDBNull(0))
                att1 = "hej";
            else
                att1 = Reader.GetString(0);
            if (Reader.IsDBNull(1))
                att2 = 0.0006000M;
            else
                att2 = Reader.GetDecimal(1);
            if (Reader.IsDBNull(2))
                att3 = 12;
            else
                att3 = Reader.GetInt32(2);
            if (Reader.IsDBNull(3))
                att4 = "hej";
            else
                att4 = Reader.GetString(3);
            if (Reader.IsDBNull(4))
                att5 = 14;
            else
                att5 = Reader.GetInt32(4);
            if (Reader.IsDBNull(5))
                att6 = "hejsa";
            else
                att6 = Reader.GetString(5);




            Stream.WriteLine(att1 + " " + att2 + " " + att3 + " " + att4 + " " + att5 + " " + att6);

            try
            {

            }
            catch (Exception e)
            {
                Console.WriteLine("Fejl i sti  {0}", e.ToString());
                Console.ReadLine();
            }
            finally
            {
                if (Stream != null)
                    Stream.Close();
            }


        }

    }

    catch (System.Data.SqlClient.SqlException e)
    {
        Console.WriteLine(e.Message);
    }
    finally
    {
        Reader.Close(); // luk ResultSet
        conn.Close(); // luk connection igen
    }

}

代码语言:javascript
运行
复制
    }
}

下面是打印输出(经过编辑(删除finally子句之后)):

1234 1200,0000 5飞行员1111 Raiban

1234 1200,0000 5飞行员1111 Raiban

1234 1200,0000 5飞行员1111 Raiban

4321 110,0000 6 Femme 1111 Hindberg

4321 110,0000 6 Femme 1111 Hindberg

4321 110,0000 6 Femme 1111 Hindberg

3241 500,0000 2 Pilot-etui 3333 Raiban

3241 500,0000 2 Pilot-etui 3333 Raiban

3241 500,0000 2 Pilot-etui 3333 Raiban

1423 250,0000 30 Splash 4444 Lensway

1423

(如果我在for循环中使用了一个小于3的整数,由于某种原因,它不会打印任何东西)

表中的数据为

Pris =价格。Lager =存储。Antal =数量。Gruppe =组。Mérke=品牌

代码语言:javascript
运行
复制
+---------+----------+-----------------+------------+---------------+-----------+
| Vare_id | Varepris | VareLager_antal |  Varenavn  | Varegruppe_nr | Varemærke |
+---------+----------+-----------------+------------+---------------+-----------+
|    1234 | 1200     |               5 | Aviator    |          1111 | Raiban    |
|    1423 | 250      |              30 | Splash     |          4444 | Lensway   |
|    3241 | 500      |               2 | Pilot-etui |          3333 | Raiban    |
|    4321 | 110      |               6 | Femme      |          1111 | Hindberg  |
+---------+----------+-----------------+------------+---------------+-----------+
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-13 21:53:36

为每次迭代创建新的StreamWriter意味着您每次都会覆盖您的文件。你最终只会得到这里的最后一条记录。

在每次迭代中读取同一字段四次,每次都会返回相同的内容。只有在调用Reader.Read()时,记录集才会移动

您应该尝试使用using语句来控制资源的清理。例如

代码语言:javascript
运行
复制
using (SqlConnection conn = new SqlConnection(connString)) {
    using (StreamWriter s = new StreamWriter(UdFil)) {
        while (Reader.Read()) {
           ....
        }
    }
}

这将相应地关闭ConnectionStreamWriter

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13860985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档