question

JumboDumbo-8428 avatar image
0 Votes"
JumboDumbo-8428 asked karenpayneoregon answered

How to use LINQ to get Data from Oracle Database?

I am currently testing out a simple C# application to read from my Microsoft SQL Database. I want to use it to get Data from my Oracle Database, can someone help?
My Interface:

[ServiceContract]
public interface IService1
{

     [OperationContract]
     List<WeatherInfo> GetData(DateTime start, DateTime end);
 }


 // Use a data contract as illustrated in the sample below to add composite types to service operations.
 [DataContract]
 public class WeatherInfo
 {
     [DataMember]
     public int Id
     {
         get;
         set;
     }

     [DataMember]
     public DateTime Datum 
     {
         get;
         set;
     } 

My implementation of the Interface, here I am using LINQ to get the Data:

public class Service1 : IService1
{
WeatherDataEntities entity = new WeatherDataEntities(); //create an Entity to get Data from Database

     public List<WeatherInfo> GetData(DateTime start, DateTime end)
     {
         var list = (from siu in entity.WeatherData where siu.Datum > start && siu.Datum < end select new WeatherInfo {
             Datum = (DateTime)siu.Datum,
             Feuchtigkeit = (int)siu.Feuchtigkeit,
             Id = siu.Id,
             Luftdruck = (int)siu.Luftdruck,
             Regen = (int)siu.Regen,
             Richtung = (int)siu.Richtung,
             Temperatur = (double)siu.Temperatur,
             Wind = (double)siu.Wind,
             Zeit = (TimeSpan)siu.Zeit
         }).Take(100).ToList();

         return list;
     }
 }

I am also using a WPF Client Application to select the data, here is the Mainwindow.xaml file content:

<Window x:Class="WPF_Client.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Client"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DatePicker x:Name="startPicker" HorizontalAlignment="Left" Margin="33,29,0,0" VerticalAlignment="Top"/>
<DatePicker x:Name="endPicker" HorizontalAlignment="Left" Margin="33,72,0,0" VerticalAlignment="Top"/>
<Button x:Name="button" Content="Run" HorizontalAlignment="Left" Margin="33,115,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<DataGrid x:Name="grid" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="345" VerticalAlignment="Top" Width="538" Margin="213,29,0,0">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Id}" Width=""/>
<DataGridTextColumn Header="Date" Binding="{Binding Datum}" Width="
"/>
<DataGridTextColumn Header="Zeit" Binding="{Binding Zeit}" Width=""/>
<DataGridTextColumn Header="Tempertur" Binding="{Binding Temperatur}" Width="
"/>
<DataGridTextColumn Header="Luftdruck" Binding="{Binding Luftdruck}" Width=""/>
<DataGridTextColumn Header="Regen" Binding="{Binding Regen}" Width="
"/>
<DataGridTextColumn Header="Wind" Binding="{Binding Wind}" Width=""/>
<DataGridTextColumn Header="Richtung" Binding="{Binding Richtung}" Width="
"/>
<DataGridTextColumn Header="Feuchtigkeit" Binding="{Binding Feuchtigkeit}" Width="*"/>
</DataGrid.Columns>
</DataGrid>

 </Grid>

</Window>

Any help appreciated!

dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is using XML an option?

I also found this in the documentation:

https://docs.microsoft.com/en-us/dotnet/standard/linq/linq-xml-overview

0 Votes 0 ·

You have not indicated what the specific issue is.

0 Votes 0 ·

@JumboDumbo-8428, Welcome to Microsoft Q&A, as others mentioned, your problem is not clear, which data you want to get from the oracle database? Do you use ado.net to connect to the database or use Entityframework to connect to database?

0 Votes 0 ·

I am using Entityframework to connect to my Database, but i am unsure how to connect to my Oracle Database

0 Votes 0 ·

@JumboDumbo-8428, thanks for the feedback, Since Oracle is a 3rd party product, I recommend that you could ask for help in Oracle Forum.


0 Votes 0 ·
JoeMamma-5281 avatar image
0 Votes"
JoeMamma-5281 answered

Try to connect a WPF app, to test if your service works

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

     public MainWindow()
     {
         InitializeComponent();
     }

     private void button_Click(object sender, RoutedEventArgs e)
     {
         DateTime startTime;
         DateTime endTime;

         startTime = start.SelectedDate.Value;
         endTime = end.SelectedDate.Value;

         List<ServiceReference1.WeatherDataClass> dataSet = client.GetData(startTime, endTime).ToList();
         grid.ItemsSource = dataSet;
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

You first need to know your connection for the connection string, one way is to find one via connectionstrings site.

Then in your DbContext OnConfiguring set the connection string.

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
     if (!optionsBuilder.IsConfigured)
     {
         optionsBuilder.UseOracle("Your connection string goes here");
     }
 }

Example

 optionsBuilder.UseOracle("Data Source=xxxxxx;Persist Security Info=True;Enlist=false;Pooling=true;Statement Cache Size=10;User ID=xxxxx;Password=xxxx;");



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.