Click here to Skip to main content
15,916,846 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how can I access a class that saves data to sql server? how to instantiate it since it is not static? thanks


[UPDATE]

C#
SqlConnection conn = null;
       
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString);

conn.Open();

SqlCommand cmd = new SqlCommand("uspSaveFiscal", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FiscalStart", year_start.Text);
cmd.Parameters.AddWithValue("@FiscalEnd", year_end.Text);
cmd.Parameters.AddWithValue("@SA_Start", Convert.ToDateTime(sa_start.Text));
cmd.Parameters.AddWithValue("@SA_End", Convert.ToDateTime(sa_end.Text));
cmd.Parameters.AddWithValue("@FA_Start", Convert.ToDateTime(fa_start.Text));
cmd.Parameters.AddWithValue("@FA_End", Convert.ToDateTime(fa_end.Text));
cmd.Parameters.AddWithValue("@FiscalDesc", fiscal_desc.Text);

SqlDataReader ha = cmd.ExecuteReader();

* this function needs to be transferred to a class: scheduler.cs
Posted
v4
Comments
Pheonyx 6-Aug-13 3:42am    
Based on what you are asking:

MyClass myClass = new MyClass();

But can you elaborate more as to what you actually mean because it feels like an incomplete question.
OriginalGriff 6-Aug-13 3:50am    
There is no one answer to this: the class does not have to be static, it could be a "normal" class, it could be a singleton.
We need better information - remember we can't see your screen or access your HDD, and that there are umpteen different way to access a database!
Use the "Improve question" widget to provide better info!
Bernhard Hiller 6-Aug-13 4:08am    
Do not forget to add a "reference" to the dll which contains that class (and perhaps to some further dlls the former dll depends on).

Hi,

You can create your class like,

C#
public class scheduler
{
   public void TestFunction(parameters)
   {}
}

You can access this function as follows:

 scheduler s = new scheduler();   // initialize your class object
 s.TestFunction(parameters list); // call your function


Hope it helps you.
 
Share this answer
 
I Used SQLHelper, DataService and DataRepository Classes. I call them via Instance. Thanks
 
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