Archive

Archive for November, 2014

SqlDataAdapter Example

November 16, 2014 Leave a comment

SqlDataAdapter menyediakan cara untuk komunikasi antara DataSet dan SQL Database. Berikut ini example dari SqlDataAdapter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace ConsoleApp
{
class Program
{
private static SqlConnection connection;
private static SqlDataReader dataReader = null;
static void Main(string[] args)
{
ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["AdoTraining"];
string connectString = setting.ConnectionString;
connection = new SqlConnection(connectString);

insertAdapter();
updateAdapter();
deleteAdapter();
selectAdapter();
}
private static void TestConnection()
{
connection.Open();
Console.WriteLine("Test Connection Success\n");
connection.Close();
}
private static void selectAdapter()
{
SqlCommand selectCommand = new SqlCommand("select * from Employee", connection);
selectCommand.CommandType = System.Data.CommandType.Text;

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = selectCommand;

DataSet employeeDataSet = new DataSet();
adapter.Fill(employeeDataSet, "Employee");

foreach (DataRow employee in employeeDataSet.Tables["Employee"].Rows)
{
Console.WriteLine("{0}: {1}: {2} : {3}", employee["Id"].ToString(), employee["FirstName"].ToString(), employee["LastName"].ToString(), employee["Email"].ToString());
}

}

private static void insertAdapter()
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from Employee", connection);

DataSet employeeDataSet = new DataSet();
adapter.Fill(employeeDataSet, "Employees");

DataRow employee = employeeDataSet.Tables["Employees"].NewRow();
employee["ID"] = 4;
employee["FirstName"] = "Ismail";
employee["Email"] = "IsmailH@rocket.mail.com";
employee["LastName"] = "Hanif";

employeeDataSet.Tables["Employees"].Rows.Add(employee);
SqlCommandBuilder cmdb = new SqlCommandBuilder(adapter);

adapter.Update(employeeDataSet, "Employees");

}

private static void updateAdapter()
{
SqlCommand command = new SqlCommand();
command.CommandText = "update Employee set Email= @Email where Id = @EmployeeId";
command.CommandType = System.Data.CommandType.Text;
command.Connection = connection;

command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = "HanifaIsmail@gmail.com";
command.Parameters.Add("@EmployeeId", SqlDbType.Int).Value = 4;

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.UpdateCommand = command;

connection.Open();
adapter.UpdateCommand.ExecuteNonQuery();
connection.Close();

}

private static void deleteAdapter()
{
SqlCommand command = new SqlCommand();
command.CommandText = "delete from Employee where Id> @deleteId";
command.CommandType = System.Data.CommandType.Text;
command.Connection = connection;

command.Parameters.Add("@deleteId", SqlDbType.Int).Value = 3;

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.DeleteCommand = command;

connection.Open();
adapter.DeleteCommand.ExecuteNonQuery();
connection.Close();

}

}
}

Print Screen

Print Screen


Semoga bermanfaat.

Advertisements

SqlCommand Object

November 9, 2014 Leave a comment

SqlCommand digunakan untuk interaksi dengan database . Sebagai contoh, kita dapat melakukan operasi Select, Insert, Update, dan Delete  data dalam tabel database.

Connection string detail :

<connectionStrings>
<add name="AdoTraining" connectionString="Persist Security Info=False;User ID=*****;Password=*****;Initial     Catalog=AdoTraining;Server=localhost"/>
</connectionStrings>

Example:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace ConsoleApp
{
class Program
{
private static SqlConnection connection;
private static SqlDataReader dataReader = null;
static void Main(string[] args)
{
ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["AdoTraining"];
string connectString = setting.ConnectionString;
connection = new SqlConnection(connectString);

CommandDeleteData();

Console.WriteLine("-------------");
Console.WriteLine("Insert Data");
CommandInsertData();

Console.WriteLine("-------------");
Console.WriteLine("Select Data");
CommandSelectData();

Console.WriteLine("-------------");
Console.WriteLine("Update Data");
CommandUpdateData();
CommandSelectData();

Console.WriteLine("-------------");
Console.WriteLine("Delete Data");
CommandDeleteData();

}
private static void TestConnection()
{
connection.Open();
Console.WriteLine("Test Connection Success\n");
connection.Close();
}

private static void CommandInsertData()
{
try
{
connection.Open();

string sqlInsert = "insert into Employee(Id, FirstName, LastName, Email) values(@Id, @FirstName, @LastName, @Email)";
SqlCommand insertCommand = new SqlCommand();
insertCommand.Connection = connection;
insertCommand.CommandText = sqlInsert;

insertCommand.Parameters.Add("@Id",SqlDbType.Int).Value = 1;
insertCommand.Parameters.Add("@FirstName",SqlDbType.NVarChar).Value="Kukuh";
insertCommand.Parameters.Add("@LastName",SqlDbType.NVarChar).Value="Utama";
insertCommand.Parameters.Add("@Email",SqlDbType.NVarChar).Value="kukuh2utama@gmail.com";
insertCommand.ExecuteNonQuery();
Console.WriteLine("Insert Success");

}
finally
{
if (connection != null)
{
connection.Close();
}
}

}

private static void CommandSelectData()
{
try
{
connection.Open();
SqlCommand selectCommand = new SqlCommand("select Id, FirstName, LastName, Email from Employee");
selectCommand.Connection = connection;
dataReader = selectCommand.ExecuteReader();

while (dataReader.Read())
{
Console.WriteLine("Record: " + dataReader[0] + " " + dataReader[1] + " " + dataReader[2]);
}
}
finally
{
if (dataReader != null)
{
dataReader.Close();
}

if (connection != null)
{
connection.Close();
}
}

}

private static void CommandUpdateData()
{
try
{
connection.Open();

string updateSql = "update Employee Set FirstName=@FirstName, LastName=@LastName, Email=@Email where Id=@Id";
SqlCommand updateCommand = new SqlCommand(updateSql);
updateCommand.Connection = connection;

updateCommand.Parameters.Add("@Id", SqlDbType.Int).Value = 1;
updateCommand.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = "Fitria";
updateCommand.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = "Utama";
updateCommand.Parameters.Add("@Email", SqlDbType.NVarChar).Value = "fitria2utama@gmail.com";
updateCommand.ExecuteNonQuery();
Console.WriteLine("Update Success");
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}

private static void CommandDeleteData()
{
try
{
connection.Open();

string deleteSql = "delete from Employee where Id=@Id";
SqlCommand deleteCommand = new SqlCommand(deleteSql);
deleteCommand.Connection = connection;

deleteCommand.Parameters.Add("@Id", SqlDbType.Int).Value = 1;
deleteCommand.ExecuteNonQuery();
Console.WriteLine("Delete Success");
}
finally
{
if (connection != null)
{
connection.Close();
}
}
}

}
}

Screen Capture

SqlCommand

SqlCommand


 

Referensi : dari berbagai sumber.

Semoga bermanfaat. 🙂

Array di Javascript

November 8, 2014 Leave a comment

Dalam javascript ada 2 cara untuk membuat array :

1. cara 1

var arrayName = [arrayItem1, arrayItem2, arrayItem3 ];

2. cara 2

var arrayName = new Array(arrayItem1, arrayItem);

javascript menyediakan beberapa method untuk melakukan operasi dasar pada array. Dan yang perlu diperhatikan array di javascript element disimpan sebagai Object. Sehingga dalam satu array dapat menyimpan berbagai type data.

Example :

var people = [{ Id:1, Name:"Kukuh", Address:"Klaten"},
"Hore", 100];

Variable array diatas terdiri dari 3 element. Untuk mengakses element 1 dapat dilakukan dengan 2 cara yaitu :

1. people[0].Id,  people[0].Address,  people[0].Name

2. people[0]["Id"],  people[0]["Address"],  people[0]["Name"]

Untuk element 2 dan 3 dapat dilakukan dengan mengakases langsung melalui indexnya.

1.people[1]

2.people[2]

Semoga bermanfaat. 🙂