Creating and accessing a database in C #

R

Roy Gourgi

Hi,

I am new to C#.

I am trying to create a database and then to add records to it but to no
avail.
I have tried a lot of online examples but they all do not seem to work!!!!
It is very frustrating because I am used to Visual Foxpro where it is very
easy.

Please do not assume that I know anything so you really have to do all the
coding. :)

TIA
 
A

Arne Vajhøj

Roy said:
I am new to C#.

I am trying to create a database and then to add records to it but to no
avail.
I have tried a lot of online examples but they all do not seem to work!!!!

What database software are you using ?
It is very frustrating because I am used to Visual Foxpro where it is very
easy.

Please do not assume that I know anything so you really have to do all the
coding. :)

No point.

The examples we can create are not better than the online
examples you have already found.

So post some code and the error message you get and will
see if we can get it working for you.

Arne
 
R

Roy Gourgi

--
RG


Arne Vajhøj said:
What database software are you using ?


No point.

The examples we can create are not better than the online
examples you have already found.

So post some code and the error message you get and will
see if we can get it working for you.

Arne



Hi,

Here is one that I tried:

public class TestDB
{


public static void TryCreateTable()
{

using (SqlConnection con = new
SqlConnection(ConsoleApplication354.Properties.Settings.Default.masterConnectionString))
{
con.Open();
try
{
using (SqlCommand command = new SqlCommand("CREATE TABLE
Dogs1 (Weight INT, Name TEXT, Breed TEXT)", con))
{
command.ExecuteNonQuery();
}
}
catch
{
Console.WriteLine("Table couldn't be created.");
}
}
}


}
 
R

Roy Gourgi

--
RG


Arne Vajhøj said:
What database software are you using ?


No point.

The examples we can create are not better than the online
examples you have already found.

So post some code and the error message you get and will
see if we can get it working for you.

Arne

Hi,


public class TestDB
{


public static void TryCreateTable()
{

using (SqlConnection con = new
SqlConnection(ConsoleApplication354.Properties.Settings.Default.masterConnectionString))
{
con.Open();
try
{
using (SqlCommand command = new SqlCommand("CREATE TABLE
Dogs1 (Weight INT, Name TEXT, Breed TEXT)", con))
{
command.ExecuteNonQuery();
}
}
catch
{
Console.WriteLine("Table couldn't be created.");
}
}
}


}
 
R

Roy Gourgi

Hi Mark,

Yes that is the problem in that I am totally confused. :)
1) You say that you want to create a database but the SQL you are using is
to create a table

Well I was trying to create a database first and then a table as I assumed
that first you have to create a database and then the tables that make up
that database.
2) You are using the SQL Server native data provider objects - do you
already have an instance of SQL Server running?

I do not know what you mean by an instance of SQL Server running
unfortunately.
3) Are you actually trying to create a database or a table?

As I said I would like to create a database and then the tables. Is that not
the way it should be?
I thought that I already created the database when I "Add a New Source" and
I chose "Database". Is that not how it is done?
Then I tried to create the table to go into the database I think.

Can you tell me what I should be doing?

Thanks
 
J

Joe Fawcett

Roy Gourgi said:
Hi Mark,

Yes that is the problem in that I am totally confused. :)


Well I was trying to create a database first and then a table as I assumed
that first you have to create a database and then the tables that make up
that database.


I do not know what you mean by an instance of SQL Server running
unfortunately.


As I said I would like to create a database and then the tables. Is that
not
the way it should be?
I thought that I already created the database when I "Add a New Source"
and
I chose "Database". Is that not how it is done?
Then I tried to create the table to go into the database I think.

Can you tell me what I should be doing?

Thanks
We need to clarify a few things:
* Do you have SQL Server (or SQL Server Express) installed on the relevant
machine?
* Is it running? (Check services for MSSQL and similar)
* Can you find the database you need to create the table in (using either
SQL Server Management Studio or Visual Studio Server Explorer)?

If these are all true then the code you showed looks okay, it maybe that the
user running the code doesn't have permission to create new tables. Instead
of trapping the error and then writing "Table could not be created" show the
output from:
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.ToString());
}
 
R

Roy Gourgi

Hi Mark,

I just thought of something. I do not have to create the tables
programatically either so maybe I should create the tables after I create the
database?
Is that how I should do it maybe?

Thanks
 
R

Roy Gourgi

SQL Server, unlike FoxPro, is a server-based relational database management
system. It needs to be installed like any other application and then
started - most people accept the default option of having SQL Server start
automatically when the server boots up. So, as Joe says...

How do I get SQL Server to start automatically when the server boots up?

I think that I have installed it because I see it when I click Start and
then All Programs I see Microsoft SQL Server 2008.
Though I am not sure that it starts automatically when I open C#.

Ok I see, I just opened Database Explorer I see the Data Connections and I
just created a database named TestDB and I just created a table called
customer and added the fields Name, Address, TelNum.

I guess if I am able to do that it means that I am running SQL Server right?

Ok now that I have created the database TestDB and the table called customer
how do I add records and select them when I want to?

Thanks
 
R

Roy Gourgi

--
RG


Mark Rae said:
Start, Programs, Microsoft SQL Server 2008, Configuration Tools, SQL Server
Configuration Manager
Select SQL Server Services in the left pane and then make sure that SQL
Server (.........) is set to start automatically

Yes it is as I see it is set to Automatic.

Thanks Mark I appreciate it. It has been a very frustrating experience.
It looks like there are many ways to skin the cat. :)

Thanks again
Roy
 
S

sloan

A cheap trick is to provide a "virgin" database (an mdb file). Just the
tables/queries and such, no data.
Then filecopy it to make a "new" database.

A suggestion:
Move away from JET database(s) and move toward Sql Server Express or even
Sql Server CE.

...........

You can see a basic SqlServerCE database here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!176.entry
Follow the read me's as well about CE, especially the "johnnycantcode" ones.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top