c# silverlight wcf mysql

本文关键字:mysql wcf silverlight | 更新日期: 2023-09-27 17:53:52

我有silverlight 4应用程序主机ASP。. NET web应用启用了WCF RIA服务。从服务器端apach2 php5 Mysql。使用MySql连接器,ADO。. NET实体数据模型,领域服务类我可以访问MySql数据库。工作好…(对我来说慢,但工作)。

表:

languages
        LANG_ID char(10)
        LANG_NAME char(40)
        LANG_NAME_SHORT char(10)
        LANG_IMAGE blob
        LANG_PATH char(100)
        DEFAULT char(1)
        STATUS_ID char(1)
xaml:

    <ComboBox x:Name="Language_Choice" Height="25" Width="110">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding LANG_PATH}" Width="16" Height="16"></Image>
                    <TextBlock Text="{Binding LANG_NAME_SHORT}" VerticalAlignment="Center" HorizontalAlignment="Left"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
代码:

        DataDomainContext _context = new DataDomainContext();
        EntityQuery<languages> _query = from _l in _context.GetLanguagesQuery()
                                        where _l.STATUS_ID == "Y"
                                        orderby _l.LANG_NAME_SHORT
                                        select _l;
        LoadOperation<languages> _loadOp = _context.Load(_query);
        Language_Choice.ItemsSource = _loadOp.Entities;

问题:我想把结果查询放到List中:

public class LangChoice
{
    public string LangID { get; set; }
    public string LangName { get; set; }
    public byte LangImg { get; set; }
    public string LangPath { get; set; }
    public string LangDef { get; set; }
}     List<LangChoice> LangList = new List<LangChoice>();

是否有办法将结果放在列表中并保存列表项索引,其中语言。默认值== " y "。没有列表,我不知道在xaml或代码中定义的Language_Choice.SelectedIndex=0;(error=out of range)。

我是新的"开发人员"。是我写一个代码通用 SELECT像:"SELECT fild1, fild2 FROM table WHERE field_900="HELP"…"我不会从表中得到所有字段,只有定义。环境(c# Silverlight, apache, PHP, MySql with WCF RIA Services)

转发谢谢

c# silverlight wcf mysql

我找到了一种"答案"…

添加到ASP。. NET Web应用项目:新的"支持silverlight的WCF服务"。写入以下代码:

    [OperationContract]
    public List<Languages> LanguageList(string query)
    {
        string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
        var langList = new List<Languages>();
        using (MySqlConnection conn = new MySqlConnection(nwConn))
        {
            conn.Open();
            using (MySqlCommand cmd = new MySqlCommand(query, conn))
            {
                MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                if (dr != null)
                    while (dr.Read())
                    {
                        var lang = new Languages
                        {
                            LangID = dr.GetString(0),
                            LangName = " " + dr.GetString(1),
                            LangPath = dr.GetString(2),
                            LangDef = dr.GetString(3)
                        };
                        langList.Add(lang);
                    }
                return langList;
            }
        }
    }
[DataContract]
public class Languages
{
    [DataMember]
    public string LangID;
    [DataMember]
    public string LangName;
    [DataMember]
    public string LangPath;
    [DataMember]
    public string LangDef;
}

web。配置创建连接:
<connectionStrings> <add name="MyConnection" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;User Id=root;password=***;database=****;Persist Security Info=True"/> </connectionStrings>

在Silverlight客户端项目中:添加"Service Reference"

现在的代码有些人认为:

        string query = @"SELECT `LANG_ID`, `LANG_NAME_SHORT`, `LANG_PATH`, 
                        `DEFAULT` FROM `languages` WHERE `STATUS_ID` = 'Y' ORDER BY `LANG_NAME_SHORT`";
        DataToListServiceClient proxy = new DataToListServiceClient();
        proxy.LanguageListCompleted += new EventHandler<LanguageListCompletedEventArgs>(proxy_LanguageListCompleted);
        proxy.LanguageListAsync(query);

    void proxy_LanguageListCompleted(object sender, LanguageListCompletedEventArgs e)
    {
        System.Collections.ObjectModel.ObservableCollection<Languages> _list = e.Result;
        Language_Choice.ItemsSource = _list;
        int i = 0; for (i = 0; i < _list.Count; i++) { if (_list[i].LangDef == "Y") break; }
        Language_Choice.SelectedIndex = i;
    }

更改XAML:

Source="{Binding LangPath}"  &  Text="{Binding LangName}"