【问题标题】:Printing multiple attributes打印多个属性
【发布时间】:2012-12-13 13:35:52
【问题描述】:

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

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
    }

}

    }
}

这是打印输出(已编辑(删除 finally 子句后)):

1234 1200,0000 5 飞行员 1111 莱班
1234 1200,0000 5 飞行员 1111 莱班
1234 1200,0000 5 飞行员 1111 莱班
4321 110,0000 6 女 1111 Hindberg
4321 110,0000 6 女 1111 Hindberg
4321 110,0000 6 女 1111 Hindberg
3241 500,0000 2 Pilot-etui 3333 莱班
3241 500,0000 2 Pilot-etui 3333 莱班
3241 500,0000 2 Pilot-etui 3333 莱班
1423 250,0000 30 飞溅 4444 Lensway
第1423章

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

表中的数据是

价格 = 价格。贮藏 = 储存。 Antal = 数量。 Gruppe = 集团。 Mærke = 品牌

+---------+----------+-----------------+------------+---------------+-----------+
| 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  |
+---------+----------+-----------------+------------+---------------+-----------+

【问题讨论】:

    标签: c# sql arrays file io


    【解决方案1】:

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

    每次迭代读取相同的字段四次将每次返回相同的内容。只有当您调用Reader.Read()时,记录集才会继续移动

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

    using (SqlConnection conn = new SqlConnection(connString)) {
        using (StreamWriter s = new StreamWriter(UdFil)) {
            while (Reader.Read()) {
               ....
            }
        }
    }
    

    这将适当地关闭Connection 和StreamWriter。

    【讨论】:

    • 谢谢!但是你会在哪里调用 "Reader.Read() ?关于 StreamWriter,我认为它在迭代之外,因为它是在 for 循环之外调用的。
    • 您已经有一个使用While(Reader.Read()) 的外循环。这将在结果集中每行运行一次。您的流是在此循环内创建的。
    • 抱歉,没看到。所以你想让我在 while 循环之外调用 Streamwriter?
    • 我认为“想要”是错误的词。我想让你明白为什么你会得到这些结果。如果您现在认为将 StreamWriter 放在循环之外会有所帮助,您应该尝试一下。
    • 我试过了,现在我得到了和以前一样的结果,但是我得到了三次,而不是一次。我不明白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多