使用C#.net中的数据表将数据插入数据集

时间:2023-01-29 14:05:22

I'm using a dataset to show data on crystal report without using an SQL server.

我正在使用数据集在不使用SQL服务器的情况下显示Crystal报表上的数据。

My problem is that data is not inserted into the table.

我的问题是数据没有插入表中。

Here is my code:

这是我的代码:

try
{
    DataSet2 ds1 = new DataSet2();             
    DataTable t1 = ds1.Tables.Add("report1");
    t1.Columns.Add("mtrno", Type.GetType("System.String"));
    t1.Columns.Add("pf1", Type.GetType("System.String"));
    t1.Columns.Add("pf2", Type.GetType("System.String"));
    t1.Columns.Add("pf3", Type.GetType("System.String"));
    t1.Columns.Add("pf4", Type.GetType("System.String"));
    t1.Columns.Add("pf5", Type.GetType("System.String"));
    t1.Columns.Add("pf6", Type.GetType("System.String"));
    t1.Columns.Add("pf7", Type.GetType("System.String"));
    t1.Columns.Add("rel", Type.GetType("System.String"));

    //t3.Columns.Add("r_no",Type.GetType("System.String"));
    //t3.Columns.Add("date", Type.GetType("System.String"));
    //t3.Columns.Add("type", Type.GetType("System.String"));
    //t3.Columns.Add("const", Type.GetType("System.String"));
    //t3.Columns.Add("volt", Type.GetType("System.String"));
    //t3.Columns.Add("class", Type.GetType("System.String"));
    //t2.Columns.Add("rel", Type.GetType("System.String"));

    DataRow r1;
    // DataRow r3;

    textBox1.Clear();
    DirectoryInfo d_info = new DirectoryInfo("D:\\");
    FileInfo[] f_info = d_info.GetFiles("*.txt");
    foreach (FileInfo fi in f_info)
    {
        string fname = @"D:\\" + Path.GetFileNameWithoutExtension(fi.Name) + ".txt";
        if (fname.Contains(listBox1.SelectedItem.ToString()))
        {
            StreamReader sread1 = new StreamReader(fname);
            string line = null;
            while ((line = sread1.ReadLine()) != null)
            {

                if (line.Contains("[Report Header]|" + comboBox3.SelectedItem.ToString()))
                {
                    line = line.Replace("[Report Header]|", "");
                    string[] r_words = line.Split('|');

                    line = sread1.ReadLine();
                    if (line.Contains("[Field Heading]|"))
                    {
                        string[] f_words = line.Split('|');
                        foreach (string s in f_words)
                        {
                            textBox1.Text = textBox1.Text + s;
                        }
                    }
                    textBox1.Text = textBox1.Text + Environment.NewLine;
                    for (int i = 0; i < 32; i++)
                    {
                        line = sread1.ReadLine();
                        if (line.Contains("[Meter_Record]|") || line.Contains(comboBox4.SelectedItem.ToString()))
                        {
                            line=line.Replace("[Meter_Record]|","");                                        
                            string[] m_words = line.Split('|');
                            r1 = t1.NewRow();                                        
                            r1["mtrno"] = m_words[0].ToString();
                            r1["pf1"] = m_words[1].ToString();
                            r1["pf2"] = m_words[2].ToString();
                            r1["pf3"] = m_words[3].ToString();
                            r1["pf4"] = m_words[4].ToString();
                            r1["pf5"] = m_words[5].ToString();
                            r1["pf6"] = m_words[6].ToString();
                            r1["pf7"] = m_words[7].ToString();
                            r1["rel"] = "yes";
                            //for (int j = 0; j < m_words.Length;j++ )
                            //{
                            //    textBox1.Text = textBox1.Text + m_words[j];
                            //}
                        }
                        //textBox1.Text = textBox1.Text + Environment.NewLine;
                    }
                }
            }
        }
    }

    CrystalReport2 objRpt1 = new CrystalReport2();
    //objRpt.SetDataSource(ds.Tables["h_report"]);
    objRpt1.SetDataSource(ds1.Tables["report1"]);  
    crystalReportViewer1.ReportSource = objRpt1;
    crystalReportViewer1.Zoom(1);
    crystalReportViewer1.Refresh(); 
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

3 个解决方案

#1


0  

Generally to add columns look at the solution here, and to add data:

一般来说,添加列请查看此处的解决方案,并添加数据:

DataRow anyRow = ds1.report1.NewRow();

anyRow.mtrno  = 123;
anyRow.pf1 = "abc";

report1.Rows.Add(anyRow);

EDIT: (in response to your comment)

编辑:(回应你的评论)

in your code you are not adding the new row, you define one but never add it i.e.

在你的代码中你没有添加新行,你定义一个但从不添加它,即

report1.Rows.Add(anyRow);

look inside your for loop i.e.

看看你的for循环,即

r1 = t1.NewRow();
r1["mtrno"] = m_words[0].ToString();

but you never add this new row to the table. Which is why i gave you an example of how to 'Add' a row.

但是你永远不会把这个新行添加到表中。这就是为什么我给你一个如何'添加'一行的例子。

#2


0  

The problem is that before this point you have to add rows to the data table and the data table to the data set.

问题是在此之前,您必须向数据表添加行,并将数据表添加到数据集。

objRpt1.SetDataSource(ds1.Tables["report1"]); 

You are making a dataset, datatable, datarows and putting values in those rows. What you forget is that the datatable does not know that you added data to a row, same as the dataset who dint know you did anthing with a datatable. To let the upper level actually contain the data you must add it there as well.

您正在创建数据集,数据表,数据行并在这些行中放置值。您忘记的是,数据表不知道您将数据添加到行,与数据集相同,因为数据集知道您使用数据表进行了对数。要让上层实际包含数据,您还必须将其添加到那里。

Use this code after you enter the values in the row.

在行中输入值后使用此代码。

// Add row to the table.
t1.Rows.Add(r1);
// Now the table contains a single row.

Use this code after you complete the datatable (since you have only one that can be just before you create the crystal report.

完成数据表后使用此代码(因为您只有一个可以在创建晶体报告之前)。

// Add table to the upper level data set.
ds2.Tables.Add(t1);
// Now the data set contains a single data table.

#3


0  

you are not adding t1 (DataTable) into dataset.

您没有将t1(DataTable)添加到数据集中。

Add this one line

添加这一行

ds1.Tables.Add(t1);

before

之前

CrystalReport2 objRpt1 = new CrystalReport2();

#1


0  

Generally to add columns look at the solution here, and to add data:

一般来说,添加列请查看此处的解决方案,并添加数据:

DataRow anyRow = ds1.report1.NewRow();

anyRow.mtrno  = 123;
anyRow.pf1 = "abc";

report1.Rows.Add(anyRow);

EDIT: (in response to your comment)

编辑:(回应你的评论)

in your code you are not adding the new row, you define one but never add it i.e.

在你的代码中你没有添加新行,你定义一个但从不添加它,即

report1.Rows.Add(anyRow);

look inside your for loop i.e.

看看你的for循环,即

r1 = t1.NewRow();
r1["mtrno"] = m_words[0].ToString();

but you never add this new row to the table. Which is why i gave you an example of how to 'Add' a row.

但是你永远不会把这个新行添加到表中。这就是为什么我给你一个如何'添加'一行的例子。

#2


0  

The problem is that before this point you have to add rows to the data table and the data table to the data set.

问题是在此之前,您必须向数据表添加行,并将数据表添加到数据集。

objRpt1.SetDataSource(ds1.Tables["report1"]); 

You are making a dataset, datatable, datarows and putting values in those rows. What you forget is that the datatable does not know that you added data to a row, same as the dataset who dint know you did anthing with a datatable. To let the upper level actually contain the data you must add it there as well.

您正在创建数据集,数据表,数据行并在这些行中放置值。您忘记的是,数据表不知道您将数据添加到行,与数据集相同,因为数据集知道您使用数据表进行了对数。要让上层实际包含数据,您还必须将其添加到那里。

Use this code after you enter the values in the row.

在行中输入值后使用此代码。

// Add row to the table.
t1.Rows.Add(r1);
// Now the table contains a single row.

Use this code after you complete the datatable (since you have only one that can be just before you create the crystal report.

完成数据表后使用此代码(因为您只有一个可以在创建晶体报告之前)。

// Add table to the upper level data set.
ds2.Tables.Add(t1);
// Now the data set contains a single data table.

#3


0  

you are not adding t1 (DataTable) into dataset.

您没有将t1(DataTable)添加到数据集中。

Add this one line

添加这一行

ds1.Tables.Add(t1);

before

之前

CrystalReport2 objRpt1 = new CrystalReport2();