以Windows形式显示SqlDataReader信息. [英] Display SqlDataReader Info in windows form.

查看:63
本文介绍了以Windows形式显示SqlDataReader信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在 http://www.csharp-station.com/中进行示例Tutorials/AdoDotNet/Lesson07.aspx [ ^ ].在控制台中这一切都很好,但是我正在尝试将其应用于Windows窗体.下面是单击按钮后的内容:

So i''m doing the example from http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx[^] on stored procedures. Which is all well and good in console but I''m trying to apply it to a windows form. Below is what I have on a button click:

private void btnQ4_Click(object sender, EventArgs e)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
                        
            try
            {
                ArrayList list = new ArrayList();
                // create and open a connection object
                conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
                conn.Open();

                // 1. create a command object identifying
                // the stored procedure
                SqlCommand cmd = new SqlCommand("Ten Most Expensive Products", conn);

                // 2. set the command object so it knows
                // to execute a stored procedure
                cmd.CommandType = CommandType.StoredProcedure;

                // execute the command
                rdr = cmd.ExecuteReader();

                // iterate through results, printing each to console
                while (rdr.Read())
                {
                    dataGridView1.ColumnCount = 1;
                    dataGridView1.Columns[0].Name = "Top10";
                    dataGridView1.Columns[1].Name = "Units";
                    dataGridView1.Rows.Add(rdr["TenMostExpensiveProducts"].ToString() + "" + rdr["UnitPrice"].ToString());
                }
            }            
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            
        }




粗体的while语句是我遇到的问题.我不知道应该将数据读入哪个对象.在示例中,它直接进入控制台,但是我应该将其放入datagridveiw吗?如果是这样,我该怎么做呢?我试过将其放入Richtextbox中,但是字符串仅包含一行数据,而不是列表.任何帮助将不胜感激,谢谢.




The while statement in bold is the part I''m having a problem with. I have no idea what object I should have the data read into. In the example it goes straight to console however should I be putting it into a datagridveiw? And if so how do I go about doing that? I''ve tried getting it into a richtextbox however a string will only hold one lines worth of data not a list. Any help will be much appreciated thanks.

推荐答案

在上面的示例中,您必须使用sqldatadapter 代替sqldatareader,因此sqldataadapter 可以容纳任意数量的记录.

一旦将数据输入到sqldataadapter,就必须分配给datatable

因此可以将数据表分配为任何网格的数据源.

例如:

In the above example, you have to use sqldatadapter in the place of sqldatareader, So the sqldataadapter can hold any number of records.

once you get data in to sqldataadapter, you have to assign to datatable

so data table can be assigned as a data source to any grid.

example:

System.data.SqlAdapter da;
datatable dt;
da=new system.data.SqlAdapter ("select * from emp",connection)
dt.fill(da)



现在将dt绑定到您的网格



now bind dt to your grid





您能更改一下程序名称吗?
我认为您的过程名称中有一个关键字名称"most".
可以通过更改过程名称来解决您的问题.

谢谢
Hi,


Can you please change your procedure name.
I think in your procedure name there ia a keyword name "most".
may your problem solve by change your procedure name.

thanks


这篇关于以Windows形式显示SqlDataReader信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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