Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • August 31, 2015, 12:47:05 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Help with databases in Visual Studio C#  (Read 3814 times)

mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 245
    • View Profile
    • Mediaguy
    • Donate to Member
Help with databases in Visual Studio C#
« on: March 27, 2008, 07:20:09 AM »
Hi everyone,
You might know that I'm trying to learn C# and have started on the small application that I'd write for work.

I've created a form that creates a int Staffnumber, string[] itemreference, datetime ReturnDate.

I also have a database that has key ID, number Staffnumber, text itemreference, datetime ReturnDate, amongst other things.

Visual studio seems to make it very easy to throw my dataset onto the form, but I need to do take my variables and place them into the dataset.

I don't really want someone to write my code for me, but I'm also finding it very hard to find examples and explanations on the internet. I'm hoping that someone who has learnt this before could let me know where they went.

MSDN seems to show me how to do addrow's and things but I can't seem to make inteletext give the options. I think I'm missing something obvious.
Learning C# - Graham Robinson

giddy

  • Member
  • Joined in 2008
  • **
  • Posts: 20
    • View Profile
    • Giddy - We make websites and software
    • Donate to Member
Re: Help with databases in Visual Studio C#
« Reply #1 on: April 02, 2008, 08:23:25 AM »
hi

I know exactly what you're talking about, I had a large project to do over a year ago and I had to learn ADO.NET and it was so painful because there is hardly much out there about ado.net. You might want to think about getting a book btw, David Sceppa's programming ADO.NET core reference is pretty good.

ok, a DataSet an in memory representation of your tables or schema,  a dataset contains Tables, so when you have the table you want, you do exactly what you mentioned.
(ds = dataset)
DataTable dt = ds.Tables[0];
DataRow row = dt.NewRow()
Now you can do what you want with 'row'

Remember to persist the changes with
ds.AcceptChanges() //if i'm not mistaken!?

Yes, i didn't forget, You probably don't see the NewRow() on intellisense because 1. You don't have using System.Data on the top, and/or 2. You don't have System.Data.dll referenced.

I Hope that was helpful :)
Gideon

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 974
    • View Profile
    • Revolution Calling
    • Donate to Member
Re: Help with databases in Visual Studio C#
« Reply #2 on: April 02, 2008, 10:17:32 AM »
I've created a form that creates a int Staffnumber, string[] itemreference, datetime ReturnDate.
...
I also have a database that has key ID, number Staffnumber, text itemreference, datetime ReturnDate

You seem to have a mismatch between the two representations of itemreference. On the form you're showing it as an array of strings, while the table contains a single text column. Was the array a mis-typing, or do you need to rethink the model a bit?

Also, a word to the wise: the TEXT datatype is deprecated in SQL Server. You should be using VARCHAR(MAX) or NVARCHAR(MAX) instead, as TEXT will be removed in a future version.



mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 245
    • View Profile
    • Mediaguy
    • Donate to Member
Re: Help with databases in Visual Studio C#
« Reply #3 on: April 02, 2008, 12:39:51 PM »
Hi. After I didn't get any reply here I popped along to the MSDN forum where I got some help

http://forums.micros...3086413&SiteID=1

At the moment I'm not using MSSQL. I could use it but I'm bouncing between computer and am finding access easier. Hopefully the dataset will allow me to move between them more easily.

Quote
Was the array a mis-typing, or do you need to rethink the model a bit?
It's a booking system. Student ID, Date to return and bookS. So there is a for loop to write each into the database.
Learning C# - Graham Robinson

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 7,295
  • "In my dreams, I always do it right."
    • View Profile
    • wraith808
    • Donate to Member
Re: Help with databases in Visual Studio C#
« Reply #4 on: April 02, 2008, 12:54:17 PM »
Sorry :(  I didn't even notice your post or I would have responded.  Glad you got the help you needed, though!

CWuestefeld

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 974
    • View Profile
    • Revolution Calling
    • Donate to Member
Re: Help with databases in Visual Studio C#
« Reply #5 on: April 02, 2008, 02:00:11 PM »
Quote
Was the array a mis-typing, or do you need to rethink the model a bit?
It's a booking system. Student ID, Date to return and bookS. So there is a for loop to write each into the database.
If I understand, then, I think you've got your data model wrong. It sound to me like the itemreference should be a child table with a foreign key pointing back to this table. Then your UI would change from being a big entry field to possibly a listbox.



mediaguycouk

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 245
    • View Profile
    • Mediaguy
    • Donate to Member
Re: Help with databases in Visual Studio C#
« Reply #6 on: April 02, 2008, 02:20:51 PM »
If I understand, then, I think you've got your data model wrong. It sound to me like the itemreference should be a child table with a foreign key pointing back to this table. Then your UI would change from being a big entry field to possibly a listbox.
I have absolutely no idea what you just said.
Learning C# - Graham Robinson