Solved

How to design C# class and access to SqlServer.

Posted on 2003-11-10
3
699 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
3 Comments
 

Accepted Solution

by:
john_dobson earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now