Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › set up Database to use with c#
New Posts  All Forums:Forum Nav:

set up Database to use with c#

post #1 of 7
Thread Starter 
Hi guys,

I want to set up a link between my database and a c# program.

This is what i currently have:

I have installed MySql, guitools and obdc. I loaded a database and things worked fine. I was able to see tables etc, different DB's. and even code within the DB.

So now, i want to use that same DB with a c# program. I want the program to read data in from the serial port and store to the database I choose.

Here is where I am stuck. How do i link the the c# to write to or even read the database?

I have VS 2010, and there are options to add database etc, and i have tried all of the options within the add database list, and none work.

I get errors about named pipes, errors 40 and 26. I went into Mysql admin and checked the box for named pipes etc, but still nothing has worked.

I have googled, and the stuff i saw online does not work for me lol. Any ideas on what could be going on OR what i am doing wrong??
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
post #2 of 7
If extremely high performance is not required, the easiest method would be to use Entity Framework to create an object model of your database schema. That way you can interact with strongly typed objects and leave all of the boilerplate SQL queries to the .NET framework. This article should give you a quick rundown on how to set it up: http://www.codeproject.com/Tips/426790/Using-MySQL-with-Entity-Framework. You can probably skip down to Step 5 as I think it adds all the references for you, but if not then also do Step 3 and 4.

You will need to have this driver installed too: http://dev.mysql.com/downloads/connector/net/
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
post #3 of 7
Thread Starter 
I will give this a look smile.gif
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
post #4 of 7
Thread Starter 
Thank you so much. This really helped. the main thing i was missing was the connector driver you mentioned. I can view the database. Now, need to comm with it effectively:thumb:
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
post #5 of 7
Quote:
Originally Posted by BioniC187 View Post

I can view the database. Now, need to comm with it effectively:thumb:

If you're new to development with a database, you'll want to read this as well: http://www.codeproject.com/Articles/11128/3-tier-architecture-in-C
It's a best practice to separate the UI layer from the logic and the database. It may seem like more work in the short term, but there are significant benefits, and in the long run will make the code much easier to work with and reduce effort in making future changes. Not to mention that it makes the app 'modular', meaning that you could create platform-specific interfaces that all access a single logic layer. Greatly simplifying the app, reducing code, and keeping bugs to a minimum.
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #6 of 7
Thread Starter 
That's a good idea, thanks smile.gif

I was able to add the database from within c#.

Tools>Connect to database

Then chose the mysql database etc etc.

So now the database comes up in left pane under data sources.
If i want i can view the table columns etc.

But how do i write to it from c#? the fact that i can see the columns etc, means i have connected to it - so i do not believe i need to have a connection string in place. I tried the google again, but cannot find any answers. What set of commands do i use?

Side note : I have tried to connect with the program itself by trying ti use a connection string, but i get error 40 about named pipes. Mysql is on 3306. I tried new instance at 1433, allowed through firewall etc, but no connection string worked
Edited by BioniC187 - 5/11/13 at 11:56pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
FX - 6300 M5A 78L-M USB3 MSI 460 GTX Cylcone Edition Kingston Hyper X Fury 
Hard DriveOSMonitorPower
1TB Win 7 Ultimate 64bit SP1 23" LED 450W 
  hide details  
Reply
post #7 of 7
Have you created a new Entity Framework model using that data source? Once you've created one, you will normally instantiate an instance of your database context, which will normally be a class called XyzEntities (the name depends on what you named your model so I can't say exactly what it is). You can then access the tables and relationships as properties. Below is a quick example. It assumes you know something about Lambda expressions and LINQ, but it should be reasonably easy to get the gist of it even if you don't have experience with these.
Code:
using (var db = new MyStoreEntities())
{
    var customer1 = db.Customers.Single(cust => cust.ID == 1);
    var products = db.Products.Where(prod => prod.Category == "Adults Only");
    var newOrder = new Order { OrderTime = DateTime.Now, Products = products };
    
    customer1.Orders.AddObject(newOrder);
    db.SaveChanges();
}

This example shows how tables and relationships are represented. It also shows that by generating an Entity Framework model you auto generate several new types, each of which represents an entity in the database (eg. a Customer, Order or Product).
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
i7 920 D0 MSI X58 Pro-E GTX 560 Ti 448 3x2GB G.Skill DDR3-1333 9-9-9-24 
Hard DriveHard DriveOptical DriveOS
840 Pro Caviar Black LG BD-ROM Windows 8.1 Pro x64 
MonitorMonitorKeyboardPower
Dell U2713HM Dell U2311H Turbo-Trak (Google it :D) Corsair HX-520 
CaseMouseMouse PadAudio
CM690 Mionix Avior 7000 Everglide Titan AKG K 242 HD 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › set up Database to use with c#