Skip to content

Instantly share code, notes, and snippets.

@sin2akshay
Created April 24, 2018 11:11
Show Gist options
  • Save sin2akshay/35ebfec6d64b7739f3d5169a3a3cb325 to your computer and use it in GitHub Desktop.
Save sin2akshay/35ebfec6d64b7739f3d5169a3a3cb325 to your computer and use it in GitHub Desktop.
C# - Executing a SQL Query in DB2 Database. Using a datatable to get the data.
/// <summary>
/// Common method for Getting data as Datatable with connection string
/// </summary>
/// <param name="ConnectionString"></param>
/// <param name="CmdText"></param>
/// <param name="TableName">Table Name</param>
/// <returns></returns>
public DataTable GetDataTable(string ConnectionString, string CmdText,string TableName)
{
DB2Connection Con;
Con = new DB2Connection(ConnectionString);
DB2Command cmd = new DB2Command();
cmd.CommandText = CmdText;
cmd.Connection = Con;
Con.Open();
DataTable DTT = new DataTable();
DB2DataAdapter da = new DB2DataAdapter();
da.SelectCommand = cmd;
da.Fill(DTT);
DTT.TableName = TableName;
Con.Close();
return DTT;
}
//Example Call:
//DtUser = GetDataTable(ConnectionString, CmdText, "USER");
//Where DtUser is an empty Datatable
//ConnectionString = server=SERVERNAME:60018;database=DBNAME;user Id=USERNAME;password=PASSWORD;currentSchema=SCHEMANAME;persist security info=true;Connection Lifetime=600;Pooling=true;Connect Timeout=30;
//CmdText = SELECT USER_ID FROM USER WHERE UPPER(USER_LAN_CODE) = 'LANID_1' WITH UR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment