1

I am new to C# and OOP as well and am making a DB class to connect with SQL Server. Could you help me create a connection function and explain how to reuse it in many forms? I have seen a function from http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx but am unsure how to use it in another form.

Regards, Touseef

1
  • Is your question about reusing the creation function for a connection, or is it how to share one connection between different forms? Please clarify! Commented May 27, 2011 at 6:06

4 Answers 4

1
using System.Data.SqlClient;
//
    // First access the connection string, which may be autogenerated in Visual Studio for you.
    //
    string connectionString = "Write your sql connection string"
    //
    // In a using statement, acquire the SqlConnection as a resource.
    //
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        //
        // Open the SqlConnection.
        //
        con.Open();
        //
        // The following code shows how you can use an SqlCommand based on the SqlConnection.
        //
        using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
        using (SqlDataReader reader = command.ExecuteReader())
        {
        while (reader.Read())
        {
            // process it
        }
        }
    }

EDIT Use this link for detail tutorial http://www.codeproject.com/KB/database/sql_in_csharp.aspx

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

1 Comment

How about without the concrete classes? How to get the DbConnection instead to make code more database portable.
1

Here are few code samples to get you going :

Establish the connection with SQL

string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;";
SqlConnection con = new SqlConnection(connectionString);
con.Open();

//Database operations

con.Close();

Fetch the data from Database :

string queryString = "SELECT Column1, Column2 FROM TableName";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "myTable");

Hope this gets you going. All the best.

Comments

1

If you mean DbConnection class, which is in the System.Data.Common namespace, then one way to use it in a C# program is as follows:

string CnnStr = "Data Source=local;Initial Catalog=dbTest;User Id=sa;pwd=1";
DbConnection cnn = new SqlConnection(CnnStr);
cnn.Open();

Comments

0

Make use of : C#: Microsoft Enterprise Library: Data Access

To make connection on the second form you need to close the first connection and than create new one on the other from to get data.

4 Comments

private static void CreateCommand(string queryString, string connectionString) { using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); } }
how to use this function in another class?
@Touseef Khan - you can check the document using google where you can find the examples easily related to this
Note: MS Enterprise Library: Data Access is not compatible for VS 2008 Express edition. microsoft.com/en-us/download/details.aspx?id=6228

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.