hi all,
I am trying to make the jump from VB6 to C#. While I do have a couple
of
books, the best way I can learn something is to write something I
know. So
please forgive me in advance if this question sounds a bit off in the
C#
terminology.
I am taking a simple thing like opening a MS Access database and
filling a
listbox with names from 1 of the tables in the database.
I am using Visual Studio 2005 and I created a new project with 1 mdi
form.
In VB I would create the connection to the database and connect to it
in the
startup module (sub main). Then when needed in the program I would
pass the
sql statement to a public subroutine to open the recordset, retrieve
the
records and close the recordset.
While I believe I have the correct coding to connect to the database,
open a
table, retrieve the needed field/column and then close the table for
C#, I
cannot figure out where the best places are for the routines to go.
Would I
put the connection routine in the program.cs module? I see that this
has
been created in the program.cs module:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDIMain());
}
so I was guessing that I could do a:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDIMain());
ConnectToDatabase():
}
for my initial connection and have the routine ConnectToDatabase() in
the
program.cs module.
The next question would be where do I put a "ExtractRecords" routine
so that
all of the forms in the program could access the routine and extract
from the
database? Would that go in the program.cs module as well?
Thank you for your support,
George
Ignacio Machin ( .NET/ C# MVP ) Visa profil
Fler alternativ 11 Feb, 17:52
Nyhetsgrupper: microsoft.public.dotnet.languages.csharp
Från: "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com>
Hi,
--
Ignacio Machinhttp://
www.laceupsolutions.com
Mobile & warehouse Solutions.
In VB I would create the connection to the database and connect to it in
the
startup module (sub main). Then when needed in the program I would pass
the
sql statement to a public subroutine to open the recordset, retrieve the
records and close the recordset.
That is wrong, (both in VB6 and in .NET) you should only connect to
the DB
when you need the data AND close it ASAP.
In .net you have a class DataSet that hold a number of tables,
relationship,
etc kind of an in memory DB.
Take a look in MSDN to these classes DataSet , DataTable, DataView.
Also take a look (and see the examples) of how to use
OleDbDataAdapter.
- Dölj citerad text -
- Visa citerad text -
While I believe I have the correct coding to connect to the database, open
a
table, retrieve the needed field/column and then close the table for C#,I
cannot figure out where the best places are for the routines to go. Would
I
put the connection routine in the program.cs module? I see that this has
been created in the program.cs module:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDIMain());
}
so I was guessing that I could do a:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDIMain());
ConnectToDatabase():
}
for my initial connection and have the routine ConnectToDatabase() in the
program.cs module.
The next question would be where do I put a "ExtractRecords" routine so
that
all of the forms in the program could access the routine and extract from
the
database? Would that go in the program.cs module as well?
Thank you for your support,
George
Scott Roberts Visa profil
Fler alternativ 11 Feb, 18:17
for my initial connection and have the routine ConnectToDatabase() in the
program.cs module.
As Ignacio said, your ConnectToDatabase() call should really go inside
your
ExtractRecords() routine.
The next question would be where do I put a "ExtractRecords" routine so
that
all of the forms in the program could access the routine and extract from
the
database? Would that go in the program.cs module as well?
It may seem overkill, but it wouldn't hurt to go ahead and start a
Data
Access Layer, even for just getting started. For starters, just
create
another class file and start putting static routines in there. Then,
from
your forms, call the static routines on that class.
When you start a new "real" project, you can just copy that class into
your
new project. Or, if you intend to use the class across multiple
projects,
put it into its own assembly.
namespace MyDAL
{
public class DataRoutines
{
public static DataSet ExtractRecords(object SomeCriteria)
{
// Open DB connection.
// Create and populate DataSet.
// Close & dispose DB connection.
return DataSetResult;
}
}
}
From your form:
DataSet mydata = MyDAL.DataRoutines.ExtractRecords("some criteria");
I took your advice and tried to figure this out. I create a new class
file
as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace Routines
{
public class DataRoutines
{
public static DataSet ExtractRecords(string strAccessConn)
{
// DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
myAccessConn = new OleDbConnection(strAccessConn);
OleDbCommand myAccessCommand = new
OleDbCommand(strAccessSelect,
myAccessConn);
OleDbDataAdapter myDataAdapter = new
OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet, "series");
myAccessConn.Close();
DataTableCollection dta = myDataSet.Tables;
return myDataSet;
}
}
}
I immediately get an error when I try to run this:
The type or namespace name 'DataSet' could not be found (are you
missing a
using directive or an assembly reference?)
Any ideas on what I did wrong?
Thanks,
George
[...]
I immediately get an error when I try to run this:
The type or namespace name 'DataSet' could not be found (are you missing
a
using directive or an assembly reference?)
Any ideas on what I did wrong?
From the MSDN documentation for the DataSet class: "Namespace:
System.Data"
http://msdn2.microsoft.com/en-us/library/system.data.dataset(vs.80).aspx
The error specifically says "are you missing a using directive or
assembly
reference?"
So, are you? Do you have a "using" directive that specifies
"System.Data"?
While every now and then I run across a complier error that seems
relatively vague, I've found that the C# compiler most often provides
very
useful, specific advice. If you simply read the error message, it
often
describes very clearly what might be wrong and provides a strong
suggestion as to how you might fix it.
IMHO, that's the case here.
Pete
I took your advice and tried to figure this out. I create a new class file
as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
namespace Routines
{
public class DataRoutines
{
public static DataSet ExtractRecords(string strAccessConn)
{
// DataSet myDataSet = new DataSet();
OleDbConnection myAccessConn = null;
myAccessConn = new OleDbConnection(strAccessConn);
OleDbCommand myAccessCommand = new
OleDbCommand(strAccessSelect,
myAccessConn);
OleDbDataAdapter myDataAdapter = new
OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(myDataSet, "series");
myAccessConn.Close();
DataTableCollection dta = myDataSet.Tables;
return myDataSet;
}
}
}
I immediately get an error when I try to run this:
The type or namespace name 'DataSet' could not be found (are you missinga
using directive or an assembly reference?)
Any ideas on what I did wrong?
Thanks,
George
The DataSet class is in the System.Data namespace, so add this at the
top:
using System.Data;
Or, you can use the fully-qualified name everywhere like this:
System.Data.DataSet myDataSet = new System.Data.DataSet();
Adding the "using" statement at the top is probably easier.
From the MSDN documentation for the DataSet class: "Namespace:
System.Data"
http://msdn2.microsoft.com/en-us/library/system.data.dataset(vs.80).aspx
The error specifically says "are you missing a using directive or
assembly reference?"
So, are you? Do you have a "using" directive that specifies
"System.Data"?
While every now and then I run across a complier error that seems
relatively vague, I've found that the C# compiler most often provides
very useful, specific advice. If you simply read the error message,
it often describes very clearly what might be wrong and provides a
strong suggestion as to how you might fix it.
IMHO, that's the case here.
Welcome to "George". He's been posting on the C++ groups for a while
now,
with some sort of automated system creating multiposts all over the
place.
There's been some speculation that he's an automated agent, although I
don't
agree.
Pete
Welcome to "George". He's been posting on the C++ groups for a while now,
with some sort of automated system creating multiposts all over the place.
There's been some speculation that he's an automated agent, although I don't
agree.
That wasnt really nice Ben, nice Ben, nice Ben, nice Ben
error 8000231321
I'd use ADO for db access... those toys coming with c# IMHO is a bit
numbing (numming?) on the creative mind...
//CY
That wasnt really nice Ben, nice Ben, nice Ben, nice Ben
Hey, not cool! I intentionally didn't comment (ok, deleted before
posting)
on George's programming ability. What I wrote are purely facts, as
you can
verify by looking at:
http://groups.google.com/group/microsoft.public.win32.programmer.kern...
error 8000231321

I'd use ADO for db access... those toys coming with c# IMHO is a bit
numbing (numming?) on the creative mind...
//CY
- Visa citerad text -
Dont be offended, was trying to make a joke, still adding something
to
the OP:s question
being a mvp you might help?
not native to english so it might come over a tad wrong... no pun
intended, just writing what fell into my head.
Will look at the link, but if we start looking for spammers with that
scope then ill be banned too, cant separate a public from a static
and
where to use one or the other
//CY
Looked at the link, here is an Idea: go hunt why recently 4-5
(internet) cables got damaged to egypt/iran/iraq instead... echelon
installations? or why some spooks writing maleware for my personal
puter? he seems harmless. He seems to respond and might be using you
as a ballplank or a thinktank - well you decide if you want to
answer.
or is it an AI, will he take the turing test?
//CY
Ignacio Machin ( .NET/ C# MVP ) wrote:
>
First a little note: top posting *and* using a -- signature
marker mess up peoples reply.
>That is wrong, (both in VB6 and in .NET) you should only connect to
the DB
>when you need the data AND close it ASAP.
Does ADO have a connection pool ??
Arne
Ignacio Machin ( .NET/ C# MVP ) wrote:
>
First a little note: top posting *and* using a -- signature
marker mess up peoples reply.
>That is wrong, (both in VB6 and in .NET) you should only connect to
the DB
>when you need the data AND close it ASAP.
Does ADO have a connection pool ??
Connection pools in ado.net as to micro:
http://msdn2.microsoft.com/en-us/library/8xx3tyca(VS.85).aspx
but I guess if u close it u loose it...
using (SqlConnection connection = new SqlConnection(
"Integrated Security=SSPI;Initial Catalog=Northwind"))
{
connection.Open();
// Pool A is created.
}
using (SqlConnection connection = new SqlConnection(
"Integrated Security=SSPI;Initial Catalog=pubs"))
{
connection.Open();
// Pool B is created because the connection strings differ..
}
using (SqlConnection connection = new SqlConnection(
"Integrated Security=SSPI;Initial Catalog=Northwind"))
{
connection.Open();
// The connection string matches pool A.
}
!top posting? that means I must reply with all left of prev mess to
comply? Ill think about that... did it your way, did it get better?