Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello all
I Want class in asp.net using c# to connect my web application with sql server and that class has some function to retrieve,update and insert data into sql server
any help please.
Posted
Comments
OriginalGriff 2-Jun-13 6:27am    
What have you tried?
Where are you stuck?
ahmed hussein khazal 2-Jun-13 6:50am    
how can create class in asp.net to connect with sqlserver and how can use it
[no name] 2-Jun-13 6:52am    
You start by doing some research. Start with http://www.codeproject.com/Articles/361579/A-Beginners-Tutorial-for-Understanding-ADO-NET

1 solution

Generally we don't use class to write any sql commands or statements but here's how u can create a class with a connection string:

public class connection
   {
       public static  SqlConnection getconnection()
       {
           SqlConnection con = new SqlConnection(@"server=servername; initial catalog=databasename; User Id=username; Password=password;");
           return con;
       }
   }


here's how to use that connection for retrieving data
 public partial class _default : System.Web.UI.Page
    {
protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = connection.getconnection();
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tablename", conn);

            DataSet ds = new DataSet();

            da.Fill(ds,"tablename");
            GridView1.DataSource = ds;
            GridView1.DataBind();
            conn.Close();

        }
}



happy coding :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900