Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
C#
SqlConnection con = new SqlConnection("Server=server_name;Database=database_name;uid=userid;password=password");
con.Open();


I want to make a seperate class.cs file and can call this class whenever needed it will help me not to change userid and password in every place just change it in class file and its done how to do this.actually i have created the file but the problm is this how to use con object at the place of sqlcommand..the class must return con.or sumthing like that.
Posted
Updated 8-Feb-18 17:37pm
v2
Comments
Tota Dill 11-Sep-13 7:45am    
how to use connection string class function that contain connection in other forms code

see my article http://www.codeproject.com/KB/tips/SqlParameters.aspx[^]
which holds classes for generic use in connecting to a database.
 
Share this answer
 
v2
for example you can make a new class : sqlParms.cs

then in that class you can create any function you want depending on the level you want to work on it : in simple cases you can create a function called getConn which return the connection string and the call it like that :

sqlParms prms=new sqlParms();
SqlConnection con=prms.getConn();


this is the simple way. other you can do more complicated work by using digimanus link ...
regards
 
Share this answer
 
Try
C#
using System.Data.SqlClient;

class myConnection
    {
        public static SqlConnection GetConnection()
        {
            string str = "Data Source=.;Initial Catalog = YourDatabaseName;uid =sa;pwd = YourPassword";

            SqlConnection con = new SqlConnection(str);
            con.Open();
            return con;
        }
    }
 
Share this answer
 
Comments
NISHAN SANDEEPA 22-Nov-12 23:16pm    
SqlConnection con = new SqlConnection("data source=.;initial catalog=dbname;integrated security=true");
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("insert into tblReg values(@id,@name)", con);
da.InsertCommand.Parameters.Add("@id", SqlDbType.Int).Value = txtId.Text;
da.InsertCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = txtName.Text;
con.Open();
da.InsertCommand.ExecuteNonQuery();
con.Close();
this is my normal method.
-------------------------------------------------------------------
I need to know, by creating a separate class like myConnection(given up) ....how I call this class in my main class.
help me by giving example.
because I am a beginner for c#.
Hi,

Hope this help..

C#
class SqlCommandWrapper
{
   private string _connectionString;

   public string connectionString
   {
     get{ return _connectionString; }
     set{ _connectionString = value; }
   }

   public SqlCommandWrapper(string tcConnectionString)
   {
     this.connectionString = tcConnectionString;
   }

   public SqlCommand CreateCommand()
   {
     SqlCommand loReturnValue = new SqlCommand()
     {
        Connection = new SqlConnection(this.connectionString)
     }

     return loReturnValue;
   }
}
 
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