Hi,
Welcome to Microsoft Q&A!
If you want to use SqlDbType.Image, you could convert the image to a byte array and save it to the database like the following.
Besides, for large file, it is recommended to save the image path to the database.
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Storage;
......
IStorageFile file;
IBuffer buffer = await FileIO.ReadBufferAsync(file);
byte[] picbytes= buffer.ToArray();
……
cmd.Parameters.Add("@picture", SqlDbType.Image).Value = picbytes;
Update:
You could query the image from the database and show it in Image control, which will verify whether you store it correctly.
try{
string sql = "select picture from imagetable";
SqlConnection sc = new SqlConnection(connstring);
sc.Open();
SqlCommand scmd = new SqlCommand(sql, sc);
SqlDataReader red = scmd.ExecuteReader();
if (red.Read())
{
BitmapImage bitmap = new BitmapImage();
byte[] bytes = (byte[])red[0];
var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
await bitmap.SetSourceAsync(stream);
myImage.Source = bitmap; //myImage is Image control
}
red.Close();
}
catch (Exception ex)
{
MytextBlock.Text = ex.Message;
}
Update2:
The error message prompts ”System.NullReferenceException”, so you could check if the file is null before you operate IBuffer buffer = await FileIO.ReadBufferAsync(file)
.
In addition, if you want to convert the SqlDataReader
to List<Class>
, you need to create a BitmapImage type property for the class. Then you could read the byte array from the database and convert it to a stream, and set it as the source of this property.
For example:
PetTable.cs:
public class PetTable
{
public BitmapImage Bitmap{get;set;}
public string PetName{get;set;}
public string PetColor{get;set;}
}
MainPage.cs:
public ObservableCollection<PetTable> pets;
public MainPage()
{
this.InitializeComponent();
pets= newObservableCollection<PetTable>();
......
while (reader.Read())
{
PetTable pet= new PetTable ();
BitmapImage map=new BitmapImage();
byte[] bytes = (byte[])reader["Picture"]; // Picture is the column name
var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
await map.SetSourceAsync(stream);
pet.Bitmap=map;
pet.PetName = (string)reader["Name"];
pet.PetColor = (string)reader["Color"];
pets.Add(pet);
}
}
Finally, you could put an Image control on your XAML page and test it like the following, if the image shows in the control, which means your code works well.
MyImageControl.Source=pets[0].Bitmap;
I have updated my Update2 according to your new question, please try the above code. If you follow it, you only need to specify the x:DataType
for the Datatemplate. As follows:
<control:DataGrid AutoGenerateColumns="False" ItemsSource="{x:Bind pets, Mode=OneWay}">
<control:DataGrid.Columns>
<control:DataGridTemplateColumn Header="Picture">
<control:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="local:PetTable " >
<Image x:Name="MyImage" Width="30" Height="30" Source="{x:Bind BitMap}"/>
</DataTemplate>
</control:DataGridTemplateColumn.CellTemplate>
</control:DataGridTemplateColumn>
</control:DataGrid.Columns>
</control:DataGrid>
Update3:
public ObservableCollection<PetTable> pets;
public MainPage()
{
this.InitializeComponent();
bind();
}
public async void bind()
{
pets= newObservableCollection<PetTable>();
......
while (reader.Read())
{......}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.