时间:2019-03-17 标签:c#silverlightwcfmysql

发布于 2024-11-04 06:30:19 字数 2205 浏览 1 评论 0原文

我有 silverlight 4 应用程序。托管 ASP.NET Web 应用程序。启用 WCF RIA 服务。从服务器端 apache2 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;

问题: 我希望将结果查询放入列表:

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>();

有什么方法可以将结果放入列表并保存列表项索引,其中 languages.DEFAULT ==“Y”。 没有列表我不知道 xaml 或代码中定义的 Language_Choice.SelectedIndex=0;(error=out of range) 。

我是新的“开发人员”。 我是否编写了通用代码 SELECT,例如:“SELECT fild1, fild2 FROM table WHERE field_900="HELP" ...” 我不会从表中生成所有字段,仅定义。 环境(c# Silverlight、Apach、PHP、MySql 和 WCF RIA 服务)

转发感谢

I have silverlight 4 app. host ASP.NET web app. enabled WCF RIA services. From sever side apach2 php5 Mysql. With MySql connector, ADO.NET Entity Data Model, Domain Service class I have access to MySql database. Work fine... (for me slowly, but WORK).

table:

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>

Code:

        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;

Question:
I want result query put into 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>();

Is there any way to put result to list and save list item index where languages.DEFAULT == "Y".
Without list I don't know defined Language_Choice.SelectedIndex=0;(error=out of range) in xaml or code.

I'm new "developer". Is I write a code generic SELECT like: "SELECT fild1, fild2 FROM table WHERE field_900="HELP" ..." I don't wont result all field from table, only defined.
Environment (c# Silverlight, Apach, PHP, MySql with WCF RIA Services)

Forwards Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2024-11-11 06:30:19

我找到一种方法“答案”...

添加到 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.config 中创建连接:
; <添加名称=“MyConnection”providerName=“MySql.Data.MySqlClient”
connectionString="server=localhost;用户 ID=root;密码=***;数据库=****;持久安全信息=True"/>

在 Silverlight 客户端项目中:添加“服务引用”。

现在代码如下:

        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}"  

I find one way "answer"...

Add to ASP.NET Web Application Project: new "Silverlight-enabled WCF Service". Write to following code:

    [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;
}

In web.config create conecction:
<connectionStrings> <add name="MyConnection" providerName="MySql.Data.MySqlClient"
connectionString="server=localhost;User Id=root;password=***;database=****;Persist Security Info=True"/>
</connectionStrings>

In Silverlight client project: Add "Service Reference".

Now Code some think like:

        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;
    }

Change XAML:

Source="{Binding LangPath}"  &  Text="{Binding LangName}"  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文