Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Creating databases in c# (http://www.programmingforums.org/showthread.php?t=14294)

Sil3ncer7 Oct 29th, 2007 10:09 AM

Creating databases in c#
 
Wondering if I could get some pointers and or direction on how to create a database to be able to read/write to. I want to make a "Movie database" and not to sure on where to start.. Thanks

xavier Oct 29th, 2007 11:13 AM

Re: Creating databases in c#
 
How about Creating databases in c#

Arla Oct 29th, 2007 5:47 PM

Re: Creating databases in c#
 
Xavier... wow that's genius, however did you come up with that solution ;)

Seriously... yeah, google search, decide on the database you want to use (MySql, SQLite, SQLServer etc etc) and then search more from there.

Sil3ncer7 Oct 29th, 2007 7:18 PM

Re: Creating databases in c#
 
I tried that, but I was looking for like a basic layout. I know theres google and I have done that....

DaWei Oct 29th, 2007 7:39 PM

Re: Creating databases in c#
 
What do you currently know about databases and the construction thereof? I think it's a germane question, despite the plethora of libraries that purport to handle it all for you.

Sil3ncer7 Oct 29th, 2007 7:41 PM

Re: Creating databases in c#
 
I know a little bit about SQL server 05

Arla Oct 29th, 2007 9:08 PM

Re: Creating databases in c#
 
Okay, you confused me utterly...

Do you know anything about databases? Have you created one outside of C#? Basic layout for a program would be just SQL statements wrapped within either ExecuteQuery or ExecuteScaler or ExecuteNonQuery functions (if I recall my function names right).

Sil3ncer7 Oct 29th, 2007 9:16 PM

Re: Creating databases in c#
 
Quote:

Originally Posted by Arla (Post 136023)
Okay, you confused me utterly...

Do you know anything about databases? Have you created one outside of C#? Basic layout for a program would be just SQL statements wrapped within either ExecuteQuery or ExecuteScaler or ExecuteNonQuery functions (if I recall my function names right).


OK, I know a little about c# and I know the basic SQL commands, but I am not sure how to be able to put a movie title into a txt box, hit save and have it save to a database or file so I can open it up at a later time and add, remove, or edit a movie title.

I guess I am trying to figure out how to tie a c# application to a SQL database..

Hopefully that helps.

hollystyles Oct 30th, 2007 5:21 AM

Re: Creating databases in c#
 
You need to look at an ADO tutorial (Active Data Objects) you need a connection string, a connection object, a command object and either a SQL string or (preferable) a stored procedure.

The connection string should be in your App.config file. The stored procedure with the Insert Sql statement and a @movietitle parameter should be in your SQL Server 2005 database that you create.

When save is clicked you need to:

Read in the connection string from app.config
Create a connection object passing the connectionstring to the constructor
Create a command object using the connection object's CreateCommand() method
Set the Command objects CommandType to CommandType.StoredProcedure
Set the Command object's CommandText to the name of the insert stored procedure you create in the database
Use the COmmand object's parameters collection to add the movie title from the textbox
declare an int variable
open the connection using the connection object's Open() method
Set the int variable just declared to = CommandObject.ExecuteNonQuery()
Close the connection
Return the int variable (it should equal 1 for one record inserted)

There are many ways to tackle this problem with ADO the above is my preferred way. DONT bung all this in the click handler of the save button! (Well you can while you're getting to grips with ADO) but as soon as possible look to de-couple your data handling code from the UI. For example at it's simplest:

Create a folder called DAL (For Data Access Layer) in solution explorer immediately below the project.

Create a class file called Movie.cs, give the Movie class a string member variable called movieTitle.
Create a public int method called InsertMovie()
Put the above discussed ADO code in this method.

When save is clicked on the form, Instantiate an instance of your new movie class:

Movie movie = new Movie()
movie.movieTitle = Textbox.Text;
movie.InsertMovie();

Once you've grokked all that! you can think about: What if that movie title already exists in the database' movie table? Well it's easy cos we took the best practice approach. You simple put an If not exists sql statement in the InsertMovie stored procedure so it wont execute the insert if the title exists, this means ExecuteNonQuery will return 0 (no records inserted) you can check that return value in the Save button click handler and inform the user with a polite message.

So you see? you haven't asked a small question. Database driven applications are a big area. Good luck.

Sil3ncer7 Oct 30th, 2007 9:16 AM

Re: Creating databases in c#
 
Quote:

Originally Posted by hollystyles (Post 136041)
You need to look at an ADO tutorial (Active Data Objects) you need a connection string, a connection object, a command object and either a SQL string or (preferable) a stored procedure.

The connection string should be in your App.config file. The stored procedure with the Insert Sql statement and a @movietitle parameter should be in your SQL Server 2005 database that you create.

When save is clicked you need to:

Read in the connection string from app.config
Create a connection object passing the connectionstring to the constructor
Create a command object using the connection object's CreateCommand() method
Set the Command objects CommandType to CommandType.StoredProcedure
Set the Command object's CommandText to the name of the insert stored procedure you create in the database
Use the COmmand object's parameters collection to add the movie title from the textbox
declare an int variable
open the connection using the connection object's Open() method
Set the int variable just declared to = CommandObject.ExecuteNonQuery()
Close the connection
Return the int variable (it should equal 1 for one record inserted)

There are many ways to tackle this problem with ADO the above is my preferred way. DONT bung all this in the click handler of the save button! (Well you can while you're getting to grips with ADO) but as soon as possible look to de-couple your data handling code from the UI. For example at it's simplest:

Create a folder called DAL (For Data Access Layer) in solution explorer immediately below the project.

Create a class file called Movie.cs, give the Movie class a string member variable called movieTitle.
Create a public int method called InsertMovie()
Put the above discussed ADO code in this method.

When save is clicked on the form, Instantiate an instance of your new movie class:

Movie movie = new Movie()
movie.movieTitle = Textbox.Text;
movie.InsertMovie();

Once you've grokked all that! you can think about: What if that movie title already exists in the database' movie table? Well it's easy cos we took the best practice approach. You simple put an If not exists sql statement in the InsertMovie stored procedure so it wont execute the insert if the title exists, this means ExecuteNonQuery will return 0 (no records inserted) you can check that return value in the Save button click handler and inform the user with a polite message.

So you see? you haven't asked a small question. Database driven applications are a big area. Good luck.



HOLY COW!!! Thats what I was looking for. Thanks so much. I guess I wasn't clear in my original post, but you nailed it, I know their alot bigger than it seems, but thats why I needed some pointers and you definatly helped with the psuedocode essentially, Thanks


All times are GMT -5. The time now is 3:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC