Classes and SQL Database in ASP.net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to first start off saying that this is pretty much my first week
of heavily getting into ASP.net. I previously have programmed in ASP Classic,
and now understanding the benefits of ASP.net I would like to cross over.

Anyways onto my question. I am curious how i can go about setting up a class
with the connection to my database in it, which any page can use to connect
to that database. I was never heavily in classes or functions during my ASP
years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the best
way i can learn..

~Devin
 
Hi Devin,

Good choice migrating to ASP.Net. Now, get ready to start thinking
differently!
Anyways onto my question. I am curious how i can go about setting up a class
with the connection to my database in it, which any page can use to connect
to that database. I was never heavily in classes or functions during my ASP
years, so please bare with me.

ADO.Net uses Connection Pooling. This is a marked contrast to ADO, where it
was often a good idea to keep a Connection opened, due to the overhead of
creating a new one. With ADO.Net, it is actually a best practice to open and
close Connections as quickly as possible. When they are closed, they are
released into a Pool, where they can be re-used inexpensively, and of course
the advantage is that you don't need to maintain opened (and unused)
Connections. In some cases, it is imperative that you close them. For
example, 2 DataReaders cannot use the same Connection at the same time.
Also if you know of any good example sites with source i can look at and
read that might further my education on this subject, since that is the best
way i can learn..

The .Net SDK is a free download, and is some of the best software
documentation I've ever read:

http://www.microsoft.com/downloads/...A6-3647-4070-9F41-A333C6B9181D&displaylang=en

And check out Microsoft's ASP.Net site: http://www.asp.net

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living
 
Well you could always do as follows. And since your a new to.Net I'll try and outline things out for you:

1. Create two project in a solution. The first project is aclass library the second is a asp.net web application.
2. In the asp.net web application go to references, click on theprojects tab under the add reference option, and select theclass library project.
3. time to write some code.
4. in the class library setup a subroutine that return a dataset.For example:

//Begin Class Example Here

using System;
using System.Data;
using System.Data.SqlClient;

namespace Sample1
{
public class Sample
{
public Sample() {}

public string ConnectionString;
public string Param1;

public DataSet ViewItems(string val)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();

SqlConnection Cn = new SqlConnection(ConnectionString);
SqlCommand Cmd = new SqlCommand([STORED PROCEDURE NAME HERE],Cn);
Cmd.CommandType = CommandType.StoredProcedure;

SqlParameter pValue1 = Cmd.Parameters.Add("@Param1",SqlDbType.VarChar);
pValue1.Value = Param1;

Cn.Open();
da.SelectCommand = Cmd;
da.Fill(ds, "VALUES");
}

}
}

//End Class Example

5. Now in the ASP.Net web application double click on the form togo to the code behind page. Now you can create a new Sampleobject and run the dataset. For example:

//Begin Webform1 Example Here

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using Sample1;

namespace SampleProject
{
public class Webform1 : System.Web.UI.Page
{

private void Page_Load(object sender, System.EventArgs e)
{
BindData();
}

public void BindData()
{

DataSet ds = new Dataset();

Sample s = new Sample();
s.ConnectionString =ConfigurationSettings.AppSettings["ConnectionString"];
s.Param1 = "Item1";
ds = s.View();

Repeater1.DataSource = ds;
Repeater1.DataBind();

}

}
}

// End Webform1 Example

Okay, so what did I do here. First I just create a class filethat would get information from a database and set it to adataset. Then on the webform I reference that class andsubroutine and execute it. The I bind the information to arepeater control on the web form.

Hopefully this example will give you so ideas as to how toproceed. Not sure if the example will work word for word -- I'mnot exactly testing it -- but it should be close :)

One thing to note incase it's not entirely evident, the"ConfigurationSettings.AppSettings["ConnectionString"]" is avalue from the web.config file. Just an exmaple of where tostore the connection string.

Alan Washington
http://www.aewnet.com
I would like to first start off saying that this is pretty muchmy first week
of heavily getting into ASP.net. I previously have programmedin ASP Classic,
and now understanding the benefits of ASP.net I would like tocross over.

Anyways onto my question. I am curious how i can go aboutsetting up a class
with the connection to my database in it, which any page canuse to connect
to that database. I was never heavily in classes or functionsduring my ASP
years, so please bare with me.

Thank you so much for all your help...
Also if you know of any good example sites with source i canlook at and
read that might further my education on this subject, sincethat is the best
way i can learn..

~Devin
User submitted from AEWNET (http://www.aewnet.com/)
 
Back
Top