Solved

How to design C# class and access to SqlServer.

Posted on 2003-11-10
3
706 Views
Last Modified: 2010-04-17
Hi,

I am designing and developing a small new proejct in C# and SqlServer.
Since I am very new to C# and SqlServer (I used to work with Java),
I have the following questions:
1.Is it a good practice to create a class for each table in sqlserver?
2.If not, how to do it in a good way? Any good sample code?
3.Also, any sample code on C# to access SqlServer DB?

Thanks a lot.

Jennifer
0
Comment
Question by:jfz139
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
john_dobson earned 125 total points
ID: 9716226
Here is some sample code for connecting to a SQL DB.  You really don't need a class for each table, just set up one that will accept the table name.  If you are getting into more specifics and not wanting all the columns from a table try some store procedures and then pass your stored procedure as the query below and you get the same results.

The SQLDataReader works faster if you want to display results on the fly and not wait until everything is loaded in the Dataset.

Hope this helps.  

Peace.


            private void LoadUsers()
            {
                  DataSet ds = new DataSet();
                  ds = getData("data source=ServerName;initial catalog=DBName;Integrated Security=SSPI;packet size=4096"","Select * From Login;");

                  UserList.DataSource = ds;
                  UserList.DataBind();
            }

            public DataSet getData(string connection,string query)
            {
                  DataSet dataset = new DataSet();
                  SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
                  SqlDataAdapter adapter = new SqlDataAdapter();
                  adapter.SelectCommand = new SqlCommand(query, conn);
                  adapter.Fill(dataset);
                  return dataset;
            }
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9716907
in general, the basic concept behind building classes in your application, it to have a Class for each functional OBJECT the your app will manipulate.  That MAY mean one class per table, but if you have objects that trule have data that span several tables (this depends on how you database is structure, and on the particulars of the application), then you might have one class using data from SEVERAL tables, or even several classes using non-overlapping data from a SINGLE table.

There is nbo single, hard-and-fast rule.  For a VERY simple application, it would TYPICALLY be the case that you would have one class per table, but this would be a fairly simple structure.

AW
0
 

Author Comment

by:jfz139
ID: 9717391
John and Arthur,

Thank you both for helping me. I think both of you answered my question.
And I would like to give both some pts. Since I can accept only one answer,
I am going to accept John's answer. Sorry about it.

Thanks a lot.

Jennifer
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question