Click here to Skip to main content
15,506,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey!
I'm actually planning to create a Phone Direction in C#. I know the basics of SQL.
Most of the books explain ADO.NET using Northwind database or any other database.
My question is how can I create my own database and create a connection with my code?
My Database is going to simple initially.
Thank you.
Posted
Comments
[no name] 14-Oct-14 8:51am    
It would be exactly the same way that the books you read showed you.

Assuming you are using SQL Server, you can do it in one of two ways:
1) Create a database in Visual Studio: http://msdn.microsoft.com/en-us/library/ms233763.aspx[^]
2) Create a database in SQL Server Management Studio: http://msdn.microsoft.com/en-us/library/ms186312.aspx[^]

There are other ways, but those are the easy ones.

But first, please sit down and think very carefully about your DB and what tables and data it should contain: an error at this stage can cause you a lot of grief later on!
 
Share this answer
 
Comments
Sipherz 14-Oct-14 9:04am    
Thanks a lot!
Just one last question. After the program is completed how do I run it on a different computer? Like will the Database and the Code be together once I publish it or what? Or is it entirely different? I hope you understood my question.
OriginalGriff 14-Oct-14 9:28am    
Oooh! Big questions! :laugh:

No, seriously, those are big questions, and it isn't trivial to answer: if you are using SQL Server, then the target PC must also have access to an SQL server instance holding the DB - and it's not a good idea to install any version of SQL Server automatically, as it's a big, complex beastie which can cause enormous problems if you start proliferating instances (not the least of which is that it tends to end up with everybody having their own copy of the DB instead of sharing data).

And if you are trying to use this in a web environment, you get a number of restrictions added by the web hosting company, who won't be happy about you adding software at all...

So exactly what are you trying to achieve?
Sipherz 14-Oct-14 11:46am    
Well, that was really helpful.
I just wanted to create a small software i.e a Phonebook.
Now, if my friend needs the program that I've created is it possible to give it to him? So that he can use? Yeah! You've answered my query. Thanks a ton :D
OriginalGriff 14-Oct-14 11:58am    
Right - yes it's very possible: but for that I wouldn't use SQL.
SQL is a big, powerful package, but it's major advantage is that multiple users can access the data at the same time without causing all sorts of nasty problems. It's a bit like sharing a bag of sweets between children when they all want one *right now*. If they all grab at the bag, it'll get ripped an the sweet will fall on the floor. If you hold the bag and pass out sweets to the kids instead it all works. SQL Server holds the bag - the database - and processes requests for sweets - commands - and hands them out - returns the data.

If you just want one user to access his own phone book, then you can use a single user database (Access, SQLCE, or SqLite for example) and the standard installation programs will make sure that everything you need is installed and registered with your application. All you have to do then is check if the DB exists, and create it if not as part of your application startup:
http://stackoverflow.com/questions/4778548/how-to-create-microsoft-access-database-in-c-sharp-programmatically
should show you several ways.
Sipherz 14-Oct-14 12:02pm    
I see... You've explained very well.
Thanks again!
Which version of visual studio you are using , there is SQLDataSource component in Toolbox under Data Tab, Which can connect to the desired database in just few clicks.

and you can use the this SQL data source further for your work.

The other ways is to create the connection to the required SQL DB as given below in your config file

<add name=""ReportConnection"" providername=""System.Data.SqlClient"" connectionstring=""Data" source="ServerName;Initial" catalog="DBName;User" id="username;Password=password;connection" timeout="100000000;"/><br" mode="hold">
You can also define it you code behind.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900