Solved

How to design C# class and access to SqlServer.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Recommendation vb6 to vb.net or others 14 202
Definitions and default visual studio colors 5 74
iSeries email authority 6 87
Mutiple Folder view (5 Folder view) 3 48
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

735 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