#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    3
    Rep Power
    0

    help with classes


    I have a class database which his as follows.

    public class DataBase
    {

    public void Database()
    {
    // initiate the object and set up connection

    Connection = new SqlConnection("Server=" + g_sServer + ";uid=" + g_sUserid + ";pwd=" + g_sPassword + ";Database=" + g_sDatabase + ";");

    //open the connection
    Connection.Open();

    }



    public void close()
    {
    //close the connection
    Connection.Close();
    Connection = null;

    }
    }


    NOw I want to write another class called getInfo , which reads the records from the Db.
    However in this new class I need to have Connection as an already open connection as a property ..and I was wondering
    how i could get that done...
    I have no C# experience and am learning pls help!!!!
    Any help would be greatly appreciated.THANKS!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Romania
    Posts
    9
    Rep Power
    0
    First of all a connection string should look like this:

    //all this is happening in the Load event of the form (double clik
    //on the text bar)

    this.sqlConnection.ConnectionString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=autoSRL;Data Source=" + this.txtServerName.Text;

    //where Initial Catalog is your database and this.txtServerName
    // is a text box.

    //then you do
    this.sqlconnection.Open();

    this.sqlDataAdapter.SelectCommand.ExecuteNonQuery();
    this.sqlDataAdapter.Fill(this.myDataSet,"the name of the table")

    this.sqConnection.Close();

    -------------------------------------------------------------------
    When you create another Form on wich you desire to make the work you create a property like this

    public SqlConnection ObtainConnection
    {
    set
    {
    this.tempSqlConnetion = value;
    }
    get
    {
    return this.tempSqlConnection;
    }
    }
    //where tempConnection is a variable
    // private SqlConnection tempSqlConnection


    When you create an instance of this new form you will do somethig like this:

    Form2 frm = new Form2();
    frm.ObtainConnection = this.sqlConnection;
    frm.Show();


    --------------------------------------------------------------------------

    Then in the new form (in Form2 that is) you open the connection with a statement like this:
    this.tempSqlConnection.Open();

    NOTE that with properties in C# you could send anithing like DataSet, SqlDataAdapters even the form itself so you can do updates in the main fomr from another one.
    ---------------------------------------------------------------------------

    If you need more info mail me bonciutoma@yahoo.com and ask me more (I'm not an expert but I'm not a beginner so I think that I can Help you) :)

    Bye

IMN logo majestic logo threadwatch logo seochat tools logo