Solved

How to design C# class and access to SqlServer.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 74
mapAB Challlenge 35 123
wordmultiple challenge 12 116
Customizing meta title for 404 page template in WordPress' TwentySixteen theme 2 71
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This article will show, step by step, how to integrate R code into a R Sweave document
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

947 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

18 Experts available now in Live!

Get 1:1 Help Now