Database quandaries...

S

saywhat

Okay, please forgive me but I need to ask this... I went to Google and
pressed the button etc. first, really, before I asked... lol

I'm familiar with database manipulation in C++/MFC. I noticed right away,
though, that in C# if you're not wanting to use a databound control, there
seems to be little info out there on using a database. IOW, if I want to
page through a record set, select certain fields, edit, add records, like I
can in MFC, how to do this?

Almost all the tutorials I come across tout the abiilty to do stuff "without
writing any code". Well darn it, I want to write some code and I'd like to
know how.

FWIW I find C# difficult to comprehend at times. Well, not comprehend, but
difficult to use. Most of the material I've found on the 'net deals with
NOT coding, and I'm still looking for materuial that would be useful for a
MFC/C++ programmer to translate their skills to C#.


Also, here's a real dumb question: How do I *create* a SQL database? I am
used to using MS Access 2000 to build my database, and then using it in my
MFC program. How do you do the initial building for SQL? I am a bit
confused as to how to access a SQL server in VS2005 as well, i.e how do I
connect to it? And again, where can I find the right instructional material
to guide me?

TIA


B
 
C

Cowboy \(Gregory A. Beamer\)

saywhat said:
Okay, please forgive me but I need to ask this... I went to Google and
pressed the button etc. first, really, before I asked... lol

I'm familiar with database manipulation in C++/MFC. I noticed right away,
though, that in C# if you're not wanting to use a databound control, there
seems to be little info out there on using a database. IOW, if I want to
page through a record set, select certain fields, edit, add records, like
I can in MFC, how to do this?

Almost all the tutorials I come across tout the abiilty to do stuff
"without writing any code". Well darn it, I want to write some code and
I'd like to know how.

FWIW I find C# difficult to comprehend at times. Well, not comprehend,
but difficult to use. Most of the material I've found on the 'net deals
with NOT coding, and I'm still looking for materuial that would be useful
for a MFC/C++ programmer to translate their skills to C#.


Also, here's a real dumb question: How do I *create* a SQL database?

No dumb questions, only dumb people afraid to ask them. :)
I am used to using MS Access 2000 to build my database, and then using it
in my MFC program. How do you do the initial building for SQL?

The best bet is to connect to a server and issue a DDL command to create the
database, along with the statements necessary to create the tables, etc. If
this is an undoable option, create the database file in SQL Server and
detatch it. You can then copy to the correct server and run sp_attachdb to
attach it. It really depends on how "on the fly" the creation is as to how
useful this option is. A good primer on T-SQL will give you the proper DDL
for object creation. BTW, the SQL books online are excellent.
I am a bit confused as to how to access a SQL server in VS2005 as well,
i.e how do I connect to it?

As you are less familiar with drag and drop (a good thing), I would examine
the actual objects in the SQL Client namespace. The help file has a great
deal of code to show you how to do things. My general pattern is something
like this:

//Look for connection strings on google
//I think the site is http://www.connectionstrings.org, but not sure
string connString = "connection string here";
string sql = "name of sproc or dynamic, parameterized sql";

/*
NOTE:
"SELECT * FROM CustomersWHERE Col1 = @Col1"
is a parameterized query.
*/

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);

//if stored proc, you need to have this
cmd.CommandType = CommandType.StoredProcedure;

//Either way, use something like this
SqlParameter param = new SqlParameter("@Col1", valueHere);
cmd.Parameters.Add(param);

//for a simple update
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Dispose();
}

For a DataSet fill you will want to create a DataAdapter and table map:

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.TableMappings.Add("Table", "Customers");

//And you need a DataSet
DataSet ds = new DataSet("OrderInfo"); //made up the name

//This goes inside the try {}
da.Fill(ds);
And again, where can I find the right instructional material to guide me?

Not sure if there is a "How to code C# fcor MFC Dummies" tutorial, so I
can't help you there. :)

MSDN has the best stuff, if you want volume. There are some other
communities, but the MSDN articles are often written by the architects of
the languages, etc.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top