简体   繁体   English

需要使用C#从SQL Server 2014到Xamarin检索图像(Varbinary MAX)

[英]Need to retrieve images (Varbinary MAX) from SQL Server 2014 to Xamarin by using C#

I am new to the Xamarin platform, I have a local hosted database in SQL Server 2014 using php (with aid of XAMP server) and the table name ItemProductsDB and saved images as varbinary(MAX) . 我是Xamarin平台的新手,我在SQL Server 2014中使用php(借助XAMP服务器)在本地托管数据库,表名称为ItemProductsDB ,并将图像另存为varbinary(MAX) I'm getting all the other details from database as a string (example Product Name, Product ID, etc...) but the images as byte[] the class as below. 我从数据库中获取所有其他详细信息作为字符串(例如产品名称,产品ID等),但图像作为byte[]类如下。

public class Contactone
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Price { get; set; }
    public string Date { get; set; }        
    public byte[] Image { get; set; }        
}

public class ContectList
{
    public List<Contactone> contacts { get; set; }
}

I have already created the cs page and xaml to retrieve the other data except image, the code below JsonParsingPage.cs ( this below code runs just fine but no retrieving of images ) 我已经创建了CS页和xaml来检索除图像以外的其他数据,JsonParsingPage.cs下面的代码(下面的代码运行得很好,但是没有检索到图像)

public partial class JsonParsingPage : ContentPage
{
    public JsonParsingPage()
    {
        InitializeComponent();
        this.BackgroundImage = "background.png";
        this.Title = "Meals";
        GetJSON();
    }

    public async void GetJSON()
    {
        // Check network status 
        if (NetworkCheck.IsInternet())
        {
            var client = new System.Net.Http.HttpClient();
            var response = await client.GetAsync("http://192.168.43.226/GetProducts.php");
            string contactsJson = response.Content.ReadAsStringAsync().Result;
            ContectList ObjContactList = new ContectList();
            if (contactsJson != "")
            {
                //Converting JSON Array Objects into generic list
                ObjContactList = JsonConvert.DeserializeObject<ContectList>(contactsJson);
            }
            //Binding listview with server response  
            listviewConacts.ItemsSource = ObjContactList.contacts;
        }
        else
        {
            await DisplayAlert("JSONParsing", "No network is available.", "Ok");
        }

        //Hide loader after server response  
       ProgressLoader.IsVisible = false;
    }       

The fronted Xaml code as below with binding respective fields 前面的Xaml代码与绑定各个字段

e<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"            
         x:Class="TestProject.Views.DetailViews.JsonParsingPage">    
<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>            
        <ListView x:Name="listviewConacts" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" ItemSelected="listviewContacts_ItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid HorizontalOptions="FillAndExpand" Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Label Text="{Binding Name}" HorizontalOptions="StartAndExpand" Grid.Row="0" TextColor="White"  FontAttributes="Bold"/>
                            <Label Text="{Binding Description}" HorizontalOptions="StartAndExpand" Grid.Row="1" TextColor="Orange"  FontAttributes="Bold"/>
                            <Label Text="{Binding Price}" HorizontalOptions="StartAndExpand" Grid.Row="2" TextColor="Gray"  FontAttributes="Bold"/>
                            <Label Text="{Binding Date}" HorizontalOptions="StartAndExpand" Grid.Row="3" TextColor="Gray"  FontAttributes="Bold"/>
                            <Label Text="{Binding Image}" HorizontalOptions="StartAndExpand" Grid.Row="4" TextColor="Gray"  FontAttributes="Bold"/>
                            <BoxView HeightRequest="2" Margin="0,10,10,0" BackgroundColor="Gray" Grid.Row="4" HorizontalOptions="FillAndExpand" />
                        </Grid>
                    </ViewCell>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
    <ActivityIndicator x:Name="ProgressLoader" IsRunning="True"/>
</Grid>
</ContentPage>

My question is: what do I have to do to retrieve varbinary images from SQL Server and display them in a Xamarin form (including cs and XAML code)? 我的问题是:我该怎么做才能从SQL Server检索varbinary图像并将其以Xamarin形式显示(包括cs和XAML代码)?

Any help would be appreciated. 任何帮助,将不胜感激。

Pan

从数据库中读取byte []后:

image.Source = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM