Solved

How to design C# class and access to SqlServer.

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

803 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