On this page
Introduction
This tutorial describes how to use the SugarCommand,
SugarDataReader,
and SugarDataAdapter components.
Requirements
This tutorial assumes that you know how to connect to
SugarCRM.
Note that if you do not use design time (specifically, if you do not
place a SugarConnection
component from the toolbox on a designer), you have to
embed license information manually.
This is described in the Licensing topic.
General Information
The main function of any database application is establishing a connection
to a data source and working with the data it contains. ADO.NET data providers
serve as a bridge between an application and a data source and allow you to
execute commands as well as retrieve data using a DataReader or a DataAdapter.
Updating data involves using the Command and DataAdapter objects.
Retrieving Data Using SugarCommand and SugarDataReader
In this sample, we use SugarCommand
and SugarDataReader to retrieve data.
SugarDataReader retrieves data in pages: while you read from it, it automatically requests the next pages from SugarCRM.
SugarDataReader offers higher performance than SugarDataAdapter, especially when you query a lot of data.
For more information, refer to the description of these classes in our documentation.
|
|---|
using Devart.Data.Sugar;
...
class Program
{
static void Main(string[] args) {
const string connectionString = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;";
const string sql = "SELECT name, end_date FROM Campaigns";
using (SugarConnection connection = new SugarConnection(connectionString)) {
connection.Open();
using (SugarCommand command = connection.CreateCommand()) {
command.CommandText = sql;
using (SugarDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
Console.WriteLine("{0}\t{1}", reader.GetValue(0), reader.GetValue(1));
}
}
}
}
}
}
|
|
|---|
Imports Devart.Data.Sugar
...
Module Module1
Sub Main()
Const connectionString As String = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;"
Const sql As String = "SELECT name, end_date FROM Campaigns"
Using connection As New SugarConnection(connectionString)
connection.Open()
Using command As SugarCommand = connection.CreateCommand()
command.CommandText = sql
Using reader As SugarDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine(reader.GetValue(0) & vbTab & reader.GetValue(1))
End While
End Using
End Using
End Using
End Sub
End Module
|
Retrieving Data Using SugarDataAdapter
Applicable only for projects targeting the full .NET Framework.
In this sample, we use the SugarDataAdapter component. It retrieves data when you call its Fill method. Note that this method retrieves all the queried data from SugarCRM, which can take some time.
Here is a small sample that demonstrates how to use SugarDataAdapter to retrieve data.
|
|---|
static void Main(string[] args) {
const string connectionString = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;";
const string sql = "SELECT name, end_date FROM Campaigns";
using (SugarConnection connection = new SugarConnection(connectionString)) {
connection.Open();
DataTable table = new DataTable("Campaigns");
using (SugarCommand command = connection.CreateCommand()) {
command.CommandText = sql;
using (SugarDataAdapter adapter = new SugarDataAdapter(command)) {
adapter.Fill(table);
}
}
foreach (DataRow row in table.Rows) {
Console.WriteLine("{0}\t{1}", row[0], row[1]);
}
}
}
|
|
|---|
Sub Main()
Const connectionString As String = "server=https://your_company.sugaropencloud.eu;user id=admin;password=AmFFA4ZryM;"
Const sql As String = "SELECT name, end_date FROM Campaigns"
Using connection As New SugarConnection(connectionString)
connection.Open()
Dim table As New DataTable("Campaigns")
Using command As SugarCommand = connection.CreateCommand()
command.CommandText = sql
Using adapter As New SugarDataAdapter(command)
adapter.Fill(table)
End Using
End Using
For Each row As DataRow In table.Rows
Console.WriteLine(row(0) & vbTab & row(1))
Next
End Using
End Sub
|
Custom Object Support
dotConnect for SugarCRM fully supports SugarCRM custom objects and fields and can retrieve their data in the same way as the data of predefined objects.
Additional Information
This tutorial describes only the basic ways of working with data. You can also use
the Entity Framework ORM technology,
which is intended for converting data between incompatible type systems in different data sources and object-oriented programming languages. This technology allows you to reduce the
amount of code and maintenance required for data-oriented applications.
For more information, see the Entity Framework section.
See Also
Entity Framework
| Updating Data