Rohit Singh 

 Welcome to the online tutorial of Dotnet fundamentals. This page is dedicated to the programmers, who want to get some quick tips and tricks of using .Net. Everyone is welcome to put in their modules, so that it can be used by others. Also, any queries are most welcome and will be answered ASAP.

 

 Database Connections 

 ConnectionStrings:  

 MySql Connection String:

SERVER=ServerNameOrIP; DATABASE=DatabaseName; UID=userId; PASSWORD=password;


SqlServer 2005 Connection String:

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Office 2007 - Excel Connection String

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ExcelFilePath;Extended Properties='Excel 12.0 Xml;HDR=YES'

 

 
 Code:

 

Connection to Mysql and SQL Server:

public class DBConnection


 

{

//Return MySqlConnection object.

public MySqlConnection ConnectionMySql()

{

MySqlConnection sqlConn = null;

try

 


{

sqlConn = new MySqlConnection(<ConnectionString>);

sqlConn.Open();

}

catch (Exception ex)

{

throw ex;

}


return sqlConn;

}

//Take MySqlConnection object, then close the connection and dispose the connection object.

 

 

 

 

 

public void CloseConnectionAndNullify(MySqlConnection mySqlConnection_)

{


if (mySqlConnection_ == null || mySqlConnection_.State == ConnectionState.Closed)

return;

mySqlConnection_.Close();

mySqlConnection_.Dispose();

mySqlConnection_ = null;

}

}

 


In the above code just change the MySql Objects to SQL Server Objects.