如何使用SQL DataReader在Silverlight 4中填充DataGrid [英] How to Fill a DataGrid in Silverlight 4 with SQL DataReader

查看:94
本文介绍了如何使用SQL DataReader在Silverlight 4中填充DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在不使用CRUD的情况下使用Ria服务的最佳方法.我正在使用SQlReader来添加从存储过程中接收到的数据,并将其填充到一个可观察的集合中.然后使用DataGrid.ItemsSource = Loaded.Entities.由于某种原因,我得到了正确的行数,但是所有行都具有相同的数据.如果我在WCF中做同样的事情,它就可以正常工作...任何想法吗?


这是一些代码

I was looking for the best way to use Ria Services without CRUD. I was using SQlReader to add data recieved from the Stored procedure and stuff it into an observable collection. Then using DataGrid.ItemsSource = Loaded.Entities. For some reason when doing this, I get the correct number of Rows, but all the rows have the same data. if I do the same thing in WCF it works fine... Any ideas??


Heres some code

DimainClass

ObservableCollection<SqlPropertyClass> Getter = new ObservableCollection<SqlPropertyClass>();
 
        public ObservableCollection<SqlPropertyClass> GetData()
        
        {
 
         // Create a Connection to SQL Server
                        SqlConnection MyConnection = new SqlConnection("Data Source= 10.0.5.113; Initial Catalog=CINET3.0;Integrated Security=True");
                        MyConnection.Open();
                        SqlCommand myCommand = new SqlCommand("GetManagerFilterCAP", MyConnection);
                        
                        #region Set up SP and pass in variables
                        myCommand.CommandType = CommandType.StoredProcedure;
                        //Parameters for Stored procedure
                        myCommand.Parameters.Add(new SqlParameter("@EmployeeName", "Gregg Coleman"));
                        try
                        {
                            myCommand.ExecuteNonQuery();
                            SqlDataReader DataReader = myCommand.ExecuteReader();
                            if (DataReader.HasRows == true)
                            {
                                while(DataReader.Read())
                                {
                                   
                                    Getter.Add(new SqlPropertyClass()
                                    {
                                        NominationsGUID = DataReader.GetGuid(0),
                                        NominatedEmployee = DataReader.GetString(1),
                                        ReasonForNomination = DataReader.GetString(2),
                                        Notes = DataReader.GetString(3),
                                        Submittor = DataReader.GetString(4),
                                        SubmittorManager = DataReader.GetString(5)
                                    });
                                }

                            }
                            else { }
                        }
                        catch (SqlException e)
                        {
                            string Error;
                            Error = e.Message;
                            
                        }
                        return Getter;
        }
        

MainPage.xaml.cs

  SQLContext _Context = new SQLContext();
        LoadOperation<SqlPropertyClass> Loader;
        CollectionViewSource ViewSource = new CollectionViewSource();
        public MainPage()
        {
            InitializeComponent();
            Loader = _Context.Load(_Context.GetDataQuery());
            Loader.Completed += new EventHandler(Loader_Completed);
        }
        void Loader_Completed(object sender, EventArgs e)
        {
            ViewSource.Source = Loader.Entities;
            DataGrabber.ItemsSource = ViewSource.View;
        }
    }

推荐答案

我相信您在通过数据读取器读取数据时错过了 MoveNext .因此,您总是会得到第一条记录.
I believe you''ve missed MoveNext while reading through the data reader. Thus, you always get the first record.


有趣...我应该把MoveNext放在哪里?以及为什么返回的Getter拥有所有功能,但问题是当它到达Silverlight一侧时:(
Interesting... where do I put MoveNext? and why does the return Getter have everything, but the issue is when it gets to the Silverlight Side :(


实际上,它看起来像是DataReader捕获数据,然后执行它自己的MoveNext. ..所以认为这是另外一回事
Actually it looks like DataReader grabs data and then does it''s own MoveNext... so think it is something else


这篇关于如何使用SQL DataReader在Silverlight 4中填充DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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