শনিবার, ৯ অক্টোবর, ২০১০

Database Connection Open and execute simple Update Query

/// Connection Open
/// here 1 for OLEDB Connection and 2 for SQLConnection
/// i am just using a condition to execute two type connection in a one function
/// strQuery is the SQL Query

1. Assign update query
string member = memberID + "/IAC";
string strSQL = " update bob_contacts ";
strSQL+= " set bob_contactexternalid = replace(bob_contactexternalid,'IAC','IND'), ";
strSQL+= " bob_modify_date_time = getdate(), ";
strSQL+= " bob_contactcategoryid = 8, ";
strSQL+= " bob_contact_name = left(bob_contact_name,8) + '/ACT' ";
strSQL+= " where bob_contacttypeid = 'customer' and bob_contactcategoryid = 11 ";
strSQL += " and bob_contact_name = '"+member+"' ";

2. execute this query

executeSQL(strSQL);

public void executeSQL(string strQuery)
{
if (command == 1)
{
OleDbConnection dbCon = GetOledbConnection();
if (dbCon.State != ConnectionState.Open)
{
dbCon.Open();
}
OleDbCommand oledbComm = new OleDbCommand(strQuery, dbCon);
oledbComm.ExecuteNonQuery();
dbCon.Close();
}
if (command == 2)//TODO : SqlServer Database
{
SqlConnection dbCon = GetSqlConnection();
if (dbCon.State != ConnectionState.Open)
{
dbCon.Open();
}

SqlCommand oledbComm = new SqlCommand(strQuery, dbCon);
oledbComm.ExecuteNonQuery();
dbCon.Close();


}
}


3. OPen Connection String

public OleDbConnection GetOledbConnection()
{
OleDbConnection dbCon = new OleDbConnection(m_ConnectionString);
try
{
if (dbCon.State != ConnectionState.Open)
{
dbCon.Open();
}
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
dbCon.Close();
}
return dbCon;
}
/////// here m_ConnectionString is the connection string

MS Access connection
m_ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Payroll.mdb"

MSSQL Server
m_ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

and
Oracle Connection
m_ConnectionString="Provider=msdaora; user id=xxx;password=xxx; data source=xxx"
public SqlConnection GetSqlConnection()
{
SqlConnection dbCon = new SqlConnection(m_ConnectionString);
try
{
if (dbCon.State != ConnectionState.Open)
{
dbCon.Open();
}
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
dbCon.Close();
}
return dbCon;
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন