Archiv der Kategorie: Datenbanken

Unter Datenbanken verstehe ich alle persistenten Speicherungssysteme die über ein relationales, objektorientiertes oder hierarchisches Datenmodell und über eine standardisierte Abfragesprache wie SQL, XQuery etc… verfügen.
Auch Datenbank-APIs wie Hibernate passen in diese Kategorie.

Atlassian JIRA+MS SQL Server: Get selected custom field value for Issue from JIRA Database in SQL

Problem

A selected custom field value, that is in a MS SQL Server JIRA Database should be retrieved.

Ansatz – Approach

The tables project_key, jiraissue, CustomFieldValue, customfield and customfieldoption have to be joined

Lösung – Solution

SELECT pk.PROJECT_KEY+'-'+CAST(a.issuenum AS varchar(max)) as issue, c.cfname as field, d.customvalue 
FROM project_key pk
INNER JOIN jiraissue a ON pk.PROJECT_ID=a.PROJECT 
INNER JOIN CustomFieldValue b ON a.ID=b.ISSUE
INNER JOIN customfield c ON b.CUSTOMFIELD=c.id 
INNER JOIN customfieldoption d ON c.id=d.CUSTOMFIELD 
WHERE c.CFName = 'Requirements/Specifications affected?' 
AND   b.STRINGVALUE=CAST(d.id as varchar(max))

Excel VBA: Create dynamic worksheet from SQL Query / Excel Arbeitsblatt durch SQL Query erstellen

Problem

A new Worksheet should be created, that contains the result of an SQL Query (ADODB) generic (in the way, that no column names have to be adressed in the recordset.

Approach – Ansatz

  • Iteration over the Resultsets field names to get Meta Information / Header names
  • Usage of CopyFromRecordSet method of the Range Object: worksheet.Range(„A2“).CopyFromRecordset Rs

Solution – Lösung

Sub CreateExcelSheetWithQueryResult(ws As Worksheet, sql As String)
    Dim Cn As New ADODB.Connection
    Dim Rs As New ADODB.Recordset
    Dim vaTmp() As String
    
    Cn.Provider = "SQLOLEDB.1"
    
    Cn.ConnectionString = "Password=pass;" & _
    "Persist Security Info=True;" & _
    "User ID=user;" & _
    "Initial Catalog=databaseName;" & _
    "Data Source=ServernameOrIP"
    
    Cn.Open
    
    Rs.CursorType = adOpenKeyset
    Rs.LockType = adLockPessimistic
    Rs.Open sql, Cn, adOpenStatic
   
    ws.Cells.Clear
    
    ' This section fills in the field names from the Orders table.
    ReDim vaTmp(Rs.Fields.Count)
    For x = 0 To Rs.Fields.Count - 1
        vaTmp(x) = Rs.Fields(x).Name
    Next
    ws.Cells(1, 1).Resize(1, Rs.Fields.Count) = vaTmp
   
    ws.Range("A2").CopyFromRecordset Rs
    
    Rs.Close
    Cn.Close
End Sub

And if you want to create a new Excel sheet you can do this with a button that assigned macro contains the following source:

Sub DetailQuery1_KlickenSieAuf()
    Dim ws As Worksheet
    Sheets.Add After:=Sheets(Sheets.Count)
    ' Referenz darauf
    Set ws = Sheets(Sheets.Count)
    CreateExcelSheetWithQueryResult ws, "SELECT TOP 10 * FROM irrsinn"
    
End Sub

MS SQL Server: Concat Strings in Group By Clausel / Group By, Strings gruppieren

Problem

Strings sollen in einer Group By Klausel anhand eines Attributs gruppiert und Komma separiert dargestellt werden.

Beispiel:

ID Name Value
1 A 4
1 B 8
2 C 9

Approach – Ansatz

Durch Verwendung der XPath Funktionen lässt sich etwas kreatives erreichen…

Solution – Lösung

CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1), [Value] INT)

INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'A',4)
INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'B',8)
INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (2,'C',9)

SELECT 
  [ID],
  STUFF((
    SELECT ', ' + [Name] + ':' + CAST([Value] AS VARCHAR(MAX)) 
    FROM #YourTable 
    WHERE (ID = Results.ID) 
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS NameValues
FROM #YourTable Results
GROUP BY ID

DROP TABLE #YourTable

IBM Doors DXL: How-to access Microsoft SQL Server & Microsoft Access from DXL (eine Bibliothek für SQL Statements / a library to perform sql statements)

Aufgabenstellung — Problem

Es wird eine einfache Bibliothek benötigt um SELECT Statements auf eine Microsoft SQL Server Datenbank abzusetzen und diese in einem DXL Programm zu verwenden

A library is needed for easily creating SQL Statements with result sets. This library should be embeddable in a DXL Skript.

Ansatz — Approach

Die Bibliothek wird von Capri-Soft (Björn Karpenstein) ständig weiterentwickelt und an dieser Stelle mit regelmäßigen Updates zur Verfügung gestellt.

The library will be provided on this site with frequently updates and can be downloaded at this site.

Lösung — Solution

Implemented Functions

Skip selectStatement(string sql, string dbServer, string dbName, string dbUsername, string dbPassword)
Skip selectStatementOnMDB(string sql, string mdbFile) [since Version 0.3]
Array selectStatement(string sql, string dbServer, string dbName, string dbUsername, string dbPassword)
Array selectStatementOnMDB(string sql, string mdbFile)  [since Version 0.3]
int executeQuery(string sql, string dbServer, string dbName, string dbUsername, string dbPassword)
int executeQueryOnMDB(string sql, string mdbFile)  [since Version 0.3]
OleAutoObj openDatabase(string dbServer, string dbName, string dbUsername, string dbPassword)
OleAutoObj openDatabaseOnMDB(string mdbFile)  [since Version 0.3]

Download

First download the DXL-Library here…

sqllibrary.dxl — Version 0.3 [match filename the includes of the examples!!!]
sqlserverlibrary.dxl — Version 0.2
sqlserverlibrary.dxl — Version 0.1

Create your DXL Skript in Doors

Recommendation: When you are writing your SQL Statements, use the CAST-Method every time you define a Field in the SELECT-Clause.

Eample for Version 0.3 with MS Access Support

In this example (only works since version 0.3) you can access .mdb Files like in the following example:

#include "C:\\dev\\dxl\\client\\sqllibrary.dxl";


Skip result = selectStatementOnMDB("SELECT [Connector_ID], [Connector_Type], [Btm_Mid_Label], [Stereotype]  FROM q_connector", "\\\\bbmag295\\ProcessModellingPrototypeAdvanced\\mdb\\modell.eap");
				
// Iteration through the SkipList
for myIterator in result do 
{
   string keyValue = (string key(result));
   DxlObject currentObject = null;

   if(find(result, keyValue, currentObject))
   {
       // Just put the column names here.. it will work
       print (string currentObject->"Connector_ID") "\t";
       print (string currentObject->"Connector_Type") "\t";
       print (string currentObject->"Btm_Mid_Label") "\t";
       print (string currentObject->"Stereotype") "\n";
   }
}

delete result;

Example:

string customFieldSQL= "" //-
" SELECT  CAST(pk.PROJECT_KEY+'-'+CAST(ji.issuenum as VARCHAR(7)) as varchar(10)) as pkey,  " //-
"	     CAST(convert(char, cg.created, 120) as varchar(30)) as on_date, " //-
"		 CAST(ci.field as varchar(50)), " //-
"		 CAST(cg.author as varchar(10)) as changed_by, " //-
"		 CAST(ISNULL(ci.oldstring,'') as varchar(4096)) as changed_from,  " //-
"		 CAST(ISNULL(ci.newstring,'') as varchar(4096)) as changed_to " //-
" FROM jiraissue ji, changegroup cg, changeitem ci, project_key pk " //-
" WHERE ci.groupid = cg.id " //-
" AND cg.issueid = ji.id  " //-
" AND pk.PROJECT_ID=ji.PROJECT " //-
" AND ci.field = '" customField "' " 

Examples for Version 0.2

#include "sqlserverlibrary.dxl"; // renamed to sqllibrary since version 0.3

// Here you can define your connection setting
string dbServer="IPorSERVERNAME";
string dbName="Databasename";
string dbUsername="databaseuser";
string dbPassword="databasepasswort";

// Reduce methods only to get SQL 
Skip selectStatement(string sql) 
{	return selectStatement(sql,dbServer,dbName,dbUsername,dbPassword); }
Array selectStatement(string sql) 
{	return selectStatement(sql,dbServer,dbName,dbUsername,dbPassword); }
int executeQuery(string sql) 
{ return executeQuery(sql,dbServer,dbName,dbUsername,dbPassword); }

// A manipulation like INSERT/UPDATE/DELETE
executeQuery("INSERT INTO table(col1) VALUES('TEST')");

// SKIPLIST: SELECT statement will return a SKIP-List with 
//           dynamic attributes (KEY is first column) 
Skip result = //-
selectStatement("SELECT [id], [col1], [col2], [col3], " //-
			    "[col4], [col5], [col6] " //-
				"FROM table " //-
				"WHERE col1<>'TEST' ORDER BY id ASC");
				
// Iteration through the SkipList
for myIterator in result do 
{
   string keyValue = (string key(result));
   DxlObject currentObject = null;

   if(find(result, keyValue, currentObject))
   {
       // Just put the column names here.. it will work
       print (string currentObject->"id") "\t";
       print (string currentObject->"col1") "\t";
       print (string currentObject->"col2") "\t";
       print (string currentObject->"col3") "\t";
       print (string currentObject->"col4") "\t";
       print (string currentObject->"col5") "\t";
       print (string currentObject->"col6") "\n";
   }
}

delete result;

// Array: SELECT statement will return an  Array 
Array result2 = selectStatement("SELECT [id], [col1], [col2], " //-
				"[col3], [col4], [col5], [col6] " //-
				"FROM table " //-
				"WHERE col1<>'TEST' ORDER BY id ASC");

int i;
for (i=0; i<resultCount; i++)
{
   DxlObject currentObject2 = (DxlObject get(result2,i,1));
   
   if(!null(currentObject2))
   {
       // Just put the column names here.. it will work
       print (string currentObject2->"id") "\t";
       print (string currentObject2->"col1") "\t";
       print (string currentObject2->"col2") "\t";
       print (string currentObject2->"col3") "\t";
       print (string currentObject2->"col4") "\t";
       print (string currentObject2->"col5") "\t";
       print (string currentObject2->"col6") "\n";
   }
}

delete result2;

Example for Version 0.1

#include "sqlserverlibrary.dxl"; // renamed to sqllibrary since version 0.3

// Here you can define your connection setting
Skip selectStatement(string sql)
{
	// Connection details for test
	string dbServer="DATABASE_SERVER_OR_IP";
	string dbName="DATABASE_NAME";
	string dbUsername="DATABASE_USER";
	string dbPassword="DATABASE_PASSWORD";

	return selectStatement(sql,dbServer,dbName,dbUsername,dbPassword);
}

// selectStatement will return a SKIP-List with dynamic attributes
Skip result = //-
selectStatement( //-
"SELECT col1,col2,col3,col4,col5,col6 " //-
			"FROM table " //-
			"WHERE col1 " //-
			" NOT IN ( " //-
			"	SELECT col1 " //-
			"	FROM table " //-
			" 	WHERE LTRIM(RTRIM(col1))<>'---' " //-
			"   GROUP BY alarm_id " //-
			"	HAVING COUNT(*)>1 " //-
			"	) ")
				
// Iteration durch SkipList
for myIterator in result do 
{
   string keyValue = (string key(result));
   DxlObject currentObject = null;

   if(find(result, keyValue, currentObject))
   {
       // Just put the column names here.. it will work
       print (string currentObject->"col1") " ";
       print (string currentObject->"col2") " ";
       print (string currentObject->"col3") " ";
       print (string currentObject->"col4") " ";
       print (string currentObject->"col5") " ";
       print (string currentObject->"col6") "\n";
   }
}

delete result;

Comment

Don’t forget to leave some improvement comments here!!!

IBM Doors DXL: Zugriff auf MS SQL Server

Aufgabenstellung

In der Programmiersprache DXL von IBM Doors soll auf Daten eines Microsoft SQL Servers zugegriffen werden.

Ansatz

Die Verwendung der COM-Schnittstelle erlaubt Zugriff auf die ADODB.Connection. Hierbei handelt es sich um eine Microsoft Bibliothek für den Zugriff auf SQL Server.

Lösung 1

Das folgende Skript erlaubt Zugriff auf den Microsoft SQL Server und führt eine SELECT-Anweisung durch:

// Connection details
string dbServer=“SERVER“;
string dbName=“DATABASE“;
string dbUsername=“USER“;
string dbPassword=“PASS“;

string connectionString = „Data Source='“ dbServer „‚;
Initial Catalog='“ dbName „‚;User Id='“ dbUsername „‚;Password='“ dbPassword „‚;“;

OleAutoArgs oleAutoArgs=create;
OleAutoObj adodbConnection, adodbRecordset, objFields, objField;

string fieldName, result, err;
int numFields, index;

// Instantiate a new ADODB Connection object
adodbConnection = oleCreateAutoObject „ADODB.Connection“;

if (null adodbConnection)
{
print „Unable to instantiate database connection\n“;
halt;
}

// Instantiate a new ADODB Recordset object
adodbRecordset = oleCreateAutoObject „ADODB.Recordset“;

if(null adodbRecordset)
{
print „Unable to create Recordset object\n“;
halt;
}

// Connection details

// Set the provider and data source of the connection
// based on information from connectionstrings.com
olePut(adodbConnection, „Provider“, „sqloledb“);
clear oleAutoArgs;
put(oleAutoArgs, connectionString );
// „Password=“ dataPass „;“)
// Open the connection to the database
err=oleMethod(adodbConnection, „Open“, oleAutoArgs);

if(!null err „“)
{
print „Error opening database: “ err „\n“;
halt;
}

// SQL Command: Open a cursor to return all columns and rows of ‚tableName‘
clear oleAutoArgs

put(oleAutoArgs, „select * from [alarms] order by 1“) // SQL Command
put(oleAutoArgs, adodbConnection) // ACTIVE CONNECTION
put(oleAutoArgs, 1) // CURSOR TYPE – ‚adOpenKeyset‘
put(oleAutoArgs, 1) // LOCK TYPE – ‚adLockReadOnly‘
put(oleAutoArgs, 1) // OPTIONS – ‚adCmdText‘

err=oleMethod(adodbRecordset, „Open“, oleAutoArgs);

if(!null err „“)
{
print „Error opening table: “ err „\n“;
halt;
}

// From the Recordset object, list each field name (defined in database)
oleGet(adodbRecordset, „Fields“, objFields);
oleGet(objFields, „Count“, numFields);

for(index=0; index0?“\“,“:““) „\““ fieldName;
}

print „\“\n“;

// From the Recordset object cursor, loop through and print each row
while(true)
{
result=““;
clear oleAutoArgs;
put(oleAutoArgs, 2); // StringFormat – ‚adClipString‘
put(oleAutoArgs, 1); // NumRows
put(oleAutoArgs, „\“,\““); // ColumnDelimiter
put(oleAutoArgs, „\““); // RowDelimiter
put(oleAutoArgs, „“); // NullExpr
oleMethod(adodbRecordset, „GetString“, oleAutoArgs, result);
if(length(result)<=0) break else print "\"" result "\n"; } [/javascript]

Lösung 2

Das untere Beispiel zeigt die Kapselung in eine Funktion und den Zugriff auf Einzelwerte, bzw. auf einzelne Zellen eines Recordsets. Dieses Beispiel lässt sich ausgezeichnet in andere Bibliotheken auslagern:

OleAutoArgs args = null;

void cleanup (OleAutoObj &obj)
{
if (!null obj)
{
oleCloseAutoObject obj;
obj = null;
}
}

// some syntax helpers for arguments (from DXL standard library) …
void checkNull (string s)
{
if (!null s)
{
print „Error: “ s „\n“ dxlHere();
halt;
}
}

OleAutoArgs createArgs ()
{
if (!null args) delete args;
args = create();
return args
}

OleAutoArgs ::<-(OleAutoArgs x, int a) { put(x, a); return x } OleAutoArgs ::<-(OleAutoArgs x, string a) { put(x, a); return x } OleAutoArgs ::<-(OleAutoArgs x, bool a) { put(x, a); return x } // Hier bekommt man eine Spalte des aktuellen Records als String string stringProperty (OleAutoObj obj, string s) { string result = null; checkNull oleGet (obj, s, result); return result } void selectStatement(string sql, string dbServer, string dbName, string dbUsername, string dbPassword) { string connectionString = "Data Source='" dbServer "';Initial Catalog='" dbName "';User Id='" dbUsername "';Password='" dbPassword "';"; OleAutoArgs oleAutoArgs=create; OleAutoObj adodbConnection, adodbRecordset, objFields, objField; string fieldName, result, err; int numFields, index; // Instantiate a new ADODB Connection object adodbConnection = oleCreateAutoObject "ADODB.Connection"; if (null adodbConnection) { print "Unable to instantiate database connection\n"; halt; } // Instantiate a new ADODB Recordset object adodbRecordset = oleCreateAutoObject "ADODB.Recordset"; if(null adodbRecordset) { print "Unable to create Recordset object\n"; halt; } // Connection details // Set the provider and data source of the connection // based on information from connectionstrings.com olePut(adodbConnection, "Provider", "sqloledb"); clear oleAutoArgs; put(oleAutoArgs, connectionString ); // "Password=" dataPass ";") // Open the connection to the database err=oleMethod(adodbConnection, "Open", oleAutoArgs); if(!null err "") { print "Error opening database: " err "\n"; halt; } // SQL Command: Open a cursor to return all columns and rows of 'tableName' clear oleAutoArgs put(oleAutoArgs, sql) // SQL Command put(oleAutoArgs, adodbConnection) // ACTIVE CONNECTION put(oleAutoArgs, 1) // CURSOR TYPE - 'adOpenKeyset' put(oleAutoArgs, 1) // LOCK TYPE - 'adLockReadOnly' put(oleAutoArgs, 1) // OPTIONS - 'adCmdText' err=oleMethod(adodbRecordset, "Open", oleAutoArgs); if(!null err "") { print "Error opening table: " err "\n"; halt; } // From the Recordset object, list each field name (defined in database) oleGet(adodbRecordset, "Fields", objFields); oleGet(objFields, "Count", numFields); for(index=0; index0?“\“,“:““) „\““ fieldName;
}

print „\“\n“;

// From the Recordset object cursor, loop through and print each row
while(true)
{
// Break when we are still at the end
bool bEOF = false;
checkNull oleGet(adodbRecordset, „EOF“, bEOF);
if (bEOF) break;

oleGet(adodbRecordset, „Fields“, objFields);

// Hier werden die Einzelfelder ausgelesen
oleGet(objFields, „Item“, createArgs <- 0 , objField); string feld1 = stringProperty(objField, "Value"); oleGet(objFields, "Item", createArgs <- 1 , objField); string feld2 = stringProperty(objField, "Value"); oleGet(objFields, "Item", createArgs <- 2 , objField); string feld3 = stringProperty(objField, "Value"); print feld3 " " feld1 " " feld2 "\n"; checkNull oleMethod (adodbRecordset, "MoveNext"); } cleanup adodbConnection; cleanup adodbRecordset; cleanup objFields; cleanup objField; } // Connection details string sql="select [alarm_id], [subsystem], [bitnumber] from [alarms] order by 1"; string dbServer="SERVER"; string dbName="DATENBANK"; string dbUsername="USER"; string dbPassword="PASS"; selectStatement(sql,dbServer,dbName,dbUsername,dbPassword); [/javascript]

Adobe Flex 3 / C#.NET FluorineFX: Balkendiagramme, Tortendiagramme und Liniendiagramme

Aufgabenstellung

Auf der Basis einer Datenbank-Abfrage werden Balkendiagramme, Tortendiagramme und Liniendiagramme mit Flex 3 und der Middleware FluorineFX, die auf einem Microsoft IIS Server läuft, erstellt.

Ansatz

FluorineFX lässt sich über die Webseite http://www.fluorinefx.com/download.html herunterladen und installieren.
Es handelt sich hierbei lediglich um eine Web-Anwendung, die auf dem Microsoft IIS Server mit dem Enterprise Manager eingebunden werden muss.

Vorraussetzungen

  • Installation von FluorineFX auf dem Microsoft IIS Server
  • Installation von Adobe Flex
  • Installation einer .NET Umgebung
  • Konfiguration aller Komponenten

Lösung

Beispiel

Code in FluorineFX

Der folgende Code wird in Visual Studion (Express) als Projekttyp Klassenbibliothek (oder FluorineFX Service Projekt) mit der Sprache C# genutzt. Die C# Klassenbibliothek wird im kompilierten Zustand (dll-Datei) in den Order „bin“ im FluorineFX-Verzeichnis (welches als Website in IIS eingebunden wurde) kopiert.

Die SQL-Anweisung sollte aus Performance-Gründen möglichst stark aggregiert werden. Das Beispiel benutzt eine zweispaltige Struktur (ReportVO) und kann beliebig angepasst werden.

C# Code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using FluorineFx; // Wichtig! FluorineFx.dll Verweis einbinden

namespace ITReportingServices
{
    [RemotingService("ReportingService")]
    public class ReportingService
    {
        public ArrayList getReport()
        {
            ArrayList reports = new ArrayList();

            SqlConnection conn = 
            new SqlConnection(MyConfigurationManager.msSqlServerString);

            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT MONTH(buchdatum) As Monat, "+
               "sum(wert) AS Summe FROM costcenteraccounting "+
               "WHERE (YEAR(buchdatum)) = 2011 "+
               "AND costcenter='101008960' "+
               "GROUP BY (MONTH(buchdatum)), (YEAR(buchdatum))	";
                SqlDataReader reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    ReportVO vo = new ReportVO();
                    vo.monat = reader.GetValue(0).ToString();
                    vo.summe = reader.GetValue(1).ToString().Replace(",",".");
                    reports.Add(vo);
                }
            }
            catch (Exception e)
            {
                reports = new ArrayList();
                ReportVO vo = new ReportVO();
                vo.monat = e.Message;
                vo.summe = e.Message;
                reports.Add(vo);
                return reports;
            }
            finally
            {
                conn.Close();
            }

            return reports;
        }
    }

    public class ReportVO
    {
        public string monat;
        public string summe;
    }
}

Code in Adobe Flex

Das Model wird auf den .NET-Namespace gemappt.

Model:

package models
{
   [RemoteClass(alias="ITReportingServices.ReportVO")]
   public class ReportVO
   {
       public var monat:String;
       public var summe:String;		
		
       public function ReportVO()
       {
       }
   }
}

Service in Flex-Code aufrufen:

<!-- unter den Script-Teil in MXML schreiben -->
<mx:RemoteObject id="reportingService" 
                             destination="GenericDestination"  
                             source="ITReportingServices.ReportingService" 
                             showBusyCursor="true" 
                             fault="faultHandler(event)" >
       <mx:method name="getReport" result="getReportHandler(event)"/>
</mx:RemoteObject>	
...

// Das hier in mx:Script Teil
[Bindable] public var reports:ArrayCollection=new ArrayCollection();

public function creationComplete():void
{
    reportingService.getReport();
}
			
public function getReportHandler(event:ResultEvent):void
{
     reports = event.result as ArrayCollection;
}
			
public function faultHandler(event:FaultEvent):void
{
   Alert.show(event.fault.toString());
}

Diagramme

Balkendiagramm:

<mx:ColumnChart dataProvider="{reports}" 
                     height="100%" width="100%" id="columnchart1">
    <mx:horizontalAxis>
      <mx:CategoryAxis id="ca" categoryField="monat" title="Market sizes" />
     </mx:horizontalAxis>
     <!-- horizontal axis renderer -->
     <mx:horizontalAxisRenderers>
    	<mx:AxisRenderer axis="{ca}" canDropLabels="false" />
      </mx:horizontalAxisRenderers>
      <mx:series>
            <mx:ColumnSeries displayName="Amount" 
        		xField="monat"
        		yField="summe"/>
       </mx:series>
</mx:ColumnChart>
<mx:Legend visible="false" dataProvider="{columnchart1}"/>		

Tortendiagramm:

<mx:Label text="Linke Seite" />
<mx:PieChart id="pie2" 
   width="100%" height="100%"
   dataaProvider="{reports}"  showDataTips="true">
      <mx:series> 
        <!--Apply the Array of radii to the PieSeries.-->
	<mx:PieSeries field="summe"
                	        nameField="monat"
                	       labelPosition="callout"/>
      </mx:series>
</mx:PieChart>
  <mx:Legend dataProvider="{pie2}"/>

Liniendiagramm:

<mx:LineChart width="100%" height="100%" 
	id="myChart" 
	dataProvider="{reports}" 
        	showDataTips="true">
   <mx:horizontalAxis>
     <mx:CategoryAxis dataProvider="{reports}" 
                	     categoryField="month"/>
    </mx:horizontalAxis>
    <mx:series>
       <mx:LineSeries yField="summe" 
                 	  displayName="Summe"/>       
    </mx:series>
</mx:LineChart>

MS SQL Server und C#.NET: Textdatei per SQL Bulk Insert

Aufgabenstellung

Es wird eine perfomante (ohne Cursor-Iterationen) Möglichkeit gesucht um Textdateien per C#.NET in den Microsoft SQL Server zu importieren.

Ansatz

C# bietet eine Klassenbibliothek mit den Klassen SqlBulkCopy und SqlBulkCopyOptions, die bereits im SAP Stammdaten Importer Tool vorgestellt wurde, und dessen Quellcode im unteren Abschnitt des Artikel als Visual Studio 2005 Projekt zum download angeboten wird. Dieses Tool ermöglicht das einlesen von SE16-Downloaddateien mit ein paar Millionen Datensätzen in weniger als 2 Sekunden. Bei der herkömmlichen Methode, einer Cursor-Iteration, wird je INSERT-Statement ein Datenbankaufruf/Netzwerkzugriff durchgeführt. Dies ist nur nicht mehr notwendig.

Lösung

DataTable füllen


...
DataTable dataTable = new DataTable();
...
// Spaltenüberschriften zum Table hinzufügen (in einer Schleife befüllbar)
for (/*Schleife um die Spaltennamen einzulesen*/)
{
   // Die Anzahl der Columns muss = objects array sein
   dataTable.Columns.Add("SPALTENNAME");
}
...
// Definition des Zeilenarrays
ArrayList objects = new ArrayList();
...
while(/*Schleife um zeilenweise über Datei zu iterieren*/)
{
  ...
  for (/*Schleife um über Zellen der Zeile zu iterieren*/)
  {
     ...
     objects.Add(aktuellesZellenElementString);
     ...
  }
   ...
   // Hinzufügen des Datenelements, das die Zeilendaten hat
   dataTable.Rows.Add(objects.ToArray());
}
...


DataTable in den MS SQL Server importieren


private void WriteToDatabase()
{
    // get your connection string
    string connString = msSqlServerString;
    // connect to SQL
    using (SqlConnection connection =  new SqlConnection(connString))
    {
        // make sure to enable triggers
        // more on triggers in next post

        SqlBulkCopy bulkCopy = new SqlBulkCopy
        (
            connection,
            SqlBulkCopyOptions.TableLock |
            SqlBulkCopyOptions.FireTriggers |
            SqlBulkCopyOptions.UseInternalTransaction,
            null
         );

        // set the destination table name
        bulkCopy.DestinationTableName = txtImportTable.Text;
        connection.Open();

        // write the data in the "dataTable"
        try
        {
            bulkCopy.WriteToServer(dataTable);
        }
        catch (Exception e)
        {
            MessageBox.Show("Es ist ein Fehler aufgetreten. "+
            "Stellen Sie sicher dass der Timeout im SQL Server "+
            "auf unendlich steht und AutoClose für die Verbindung "+
            "nicht aktiviert wurde. " + e.Message, "Fehler!");
        }
        connection.Close();
    }
    // reset
    this.dataTable.Clear();
}

Ein komplettes Beispiel lässt sich hier downloaden:
Quellcode SAP Stammdaten Importer (VS 2005 Projekt)

SQL Server: Anzahl Nachkommastellen ermitteln

Aufgabe

Es sollen die Anzahl der Nachkommastelle in einem SQL Query abgefragt werden

Lösung

SELECT * FROM completeyear2011ecoflac 
WHERE 
right(cast(completeactualyear as varchar(32)), 
  len(cast(completeactualyear as varchar(32))) 
 - charindex('.',cast(completeactualyear as varchar(32))))>0

Beurteilung

Wenn die SQL-Umgebung keine Funktion zur Verfügung stellt, kann man die obige SQL zwar nutzen, sollte den hinteren Teil allerdings in eine Funktion auslagern. Das Auslagern in eine Funktion dient der Übersichtlichkeit und verletzt das DRY-Prinzip nicht (Don’t repeat yourself!)

C# .NET + Access : Auf .mdb-Datei zugreifen

Aufgabenstellung

Von C# soll auf eine Access-Datenbank zugegrieffen werden.

Lösung

public static string getCustomerCPD(string customerno)
{
   string cpd = "Customer using getCustomerCPD not found";
   OleDbConnection conn = new OleDbConnection(
   "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;"+
    "Jet OLEDB:Database Locking Mode=1; "+
    "Data Source=\"" + AppDomain.CurrentDomain.BaseDirectory +
     "\\OCT.mdb\";Jet OLEDB:Engine Type=5;"+
    "Provider=\"Microsoft.Jet.OLEDB.4.0\";Jet OLEDB:System database=;"+
    "Jet OLEDB:SFP=False;"+
    "persist security info=False;Extended Properties=;"+
    "Jet OLEDB:Encrypt Database=False;"+
    "Jet OLEDB:Create System Database=False;"+
     "Jet OLEDB:Don't Copy Locale on Compact=False;"+
    "Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;"+
     "Jet OLEDB:Global Bulk Transactions=1";);

     try
     {
       conn.Open();
       OleDbCommand comm = new OleDbCommand();
       comm.Connection = conn;

        comm.CommandText = "SELECT cpdnumber FROM [tbl_Customer] "+
                                           "WHERE debitor=@customerno";
        comm.Parameters.AddWithValue("customerno", customerno);

        OleDbDataReader reader = comm.ExecuteReader();

        while (reader.Read())
        {
            cpd = reader.GetValue(0).ToString();
        }
  }
  catch (Exception e)
  {
     return e.Message;
  }
  finally
  {
      conn.Close();
  }
  return cpd;
}