1

im facing a problem , when i creat my own class i want to create Mysql connection function and i want to use it inside all my forms i do this

program.cs

using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.Xml;
using System.IO;
public class testing
    {
        public string fahadt="Hello Class";




        public  void conncting()
        {


            MySqlConnection connection;
            string cs = @"server=localhost;userid=root;password=;database=taxi";
            connection = new MySqlConnection(cs);
            try
            {

                connection.Open();

            }
            catch (MySqlException ex)
            {
               // MessageBox.Show(ex.ToString());
            }

        }

    }

and in my form

private void button7_Click(object sender, EventArgs e)
        {
            testing fahad = new testing();
            try
            {
                dataGridView1.Show();
                fahad.conncting();

                // here is error under fahad.conncting.createcommand();
                MySqlCommand cmd = fahad.conncting.CreateCommand();
                //cmd.CommandText = "SELECT * FROM  ocms_visitors WHERE `id`='"+textBox4.Text+"'";
               // MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
                //textBox4.Text = adap.id;

               // DataSet ds = new DataSet();
               // adap.Fill(ds);

               // dataGridView1.DataSource = ds.Tables[0].DefaultView;
                //MessageBox.Show("Yes Mysql Connection is Working Now  !");
            }
            catch (Exception)
            {
                throw;
            }

        }

i dont know how i can do it im very new = C#

please help me and also i have another q should i use using .... in class and form or Enough in class ?

thanks

1
  • 2
    take a look at this stackoverflow.com/questions/10372713/… also try doing a google search > type this in to google C# stackoverflow make sqlconnection object that's used in all forms Commented Dec 28, 2012 at 19:46

3 Answers 3

2

You need to return MySqlConnection

Modify your function to:

    public  MySqlConnection conncting()
    {


        MySqlConnection connection;
        string cs = @"server=localhost;userid=root;password=;database=taxi";
        connection = new MySqlConnection(cs);
        try
        {

            connection.Open();
            return connection;

        }
        catch (MySqlException ex)
        {
            return null;
           // MessageBox.Show(ex.ToString());
        }

    }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but how i can used in the form MySqlCommand cmd = fahad.conncting().CreateCommand(); cmd.CommandText = "SELECT * FROM ocms_visitors WHERE id='3'"; MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
What error are you getting. For me the code in your comment should work fine.
0

To answer your initial question, it looks to me like you have already created the connection you were looking for. Next steps for you would be to learn about the SqlCommand Class.

For reference/edification, try this link, and happy coding!

1 Comment

how can program for me Mysql Class ? cos i tryed many time all not working
0

I suspect you want to return a connection from your conncting() method?

    public MySqlConnection conncting()
    {
        string cs = @"server=localhost;userid=root;password=;database=taxi";
        MySqlConnection connection = new MySqlConnection(cs);
        connection.Open();
        return connection;
    }

To answer your second question, yes, using using {..} blocks is a good idea for IDisposable instances whenever possible. This includes connections, commands, data adapters, etc. The following might be a reasonable pattern:

    using (MySqlConnection conn = fahad.conncting())
    using (MySqlCommand cmd = new MySqlCommand("select * from table", conn))
    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
    using (DataTable dt = new DataTable())
    {
        da.Fill(dt);
        // do something with datatable
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.