سؤال

I recently starting creating my own classes and I decided to create a simpel one, but i ran into 1 problem. here's whats going on:

In my Form1 I have a

SqlCeConnection sqlCEconn = new SqlCeConnection(@"Data Source = C:\Dropbox\Visual\Database test2 -treeview\Database test2 -treeview\Database2.sdf");

and in my new class called test, I have this:

namespace Database_test2__treeview
{
class test
{
    public string testCHECK()
    {

        SqlCeConnection sqlCEconn = new SqlCeConnection();

        if (sqlCEconn.State == ConnectionState.Open)
        {
            return "Database connection: Open";
        }
        if (sqlCEconn.State == ConnectionState.Closed)
        {
            return "Database connection: Closed";
        }
        else
        {
            return "";
        }
    }
}
}

Later in Form1 im using my new class to check if the connection is open or not, by doing:

private void Form1_Load(object sender, EventArgs e)
    {
        sqlCEconn.Open();
        test testconnection = new test();

        Toolstrip1.Text = testconnection.testCHECK();

It's obvious that the return string will be "Closed", since my SqlCeConnection in test.cs is not linked to the SqlCeConnection in my Form1

how do I do that in a simple manner ?

thanks.

هل كانت مفيدة؟

المحلول

if you want the reference, then just send it as a parameter:

public string testCHECK(SqlCeConnection thisConnection)
{
    if (thisConnection.State == ConnectionState.Open)
    {
        return "Database connection: Open";
    }
    if (thisConnection.State == ConnectionState.Closed)
    {
        return "Database connection: Closed";
    }
    else
    {
        return "";
    }
}

نصائح أخرى

You are creating a new connection object in your test class - if you want to check the state of the existing connection, you should pass it in to the test class or even the testCheck method:

class test
{
    public string testCHECK(SqlCeConnection sqlCEconn)
    {
        if (sqlCEconn.State == ConnectionState.Open)
        {
            return "Database connection: Open";
        }
        if (sqlCEconn.State == ConnectionState.Closed)
        {
            return "Database connection: Closed";
        }
        else
        {
            return "";
        }
    }
}

And call it like this:

sqlCEconn.Open();
test testconnection = new test();

Toolstrip1.Text = testconnection.testCHECK(sqlCEconn);

You should create a class called SqlManager or something and manage connections and operations against the database from there.

That will allow multiple unrelated classes to work with exactly the same connection object.

In your check you create new connection, meanwhile it should follow in your testCHECK as param

public string testCHECK(SqlCeConnection sqlCEconn)

and your call

Toolstrip1.Text = testconnection.testCHECK(sqlCEconn)

I think you want to open your connection on the test constructor. Another alternative could be passing the sqlCEconn that you create on Form1_Load as an argument to your test object

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top