· Tạo mới Database tên BCSForSharePoint2010
ü Tạo mới table tên: Customer với các columns sau:
ü Nhập mới dữ liệu cho Customer
· Mở Visual Studio 2010 tạo mới BCS
ü File | New Project | SharePoint | 2010 | Chọn Business Data
Connectivity Model và đặt tên BCSCustomer
ü Chọn Site để Dubugging và nhấn Finish
· Giao diện tạo mới nhu sau
ü Đổi tên Class Entity1 thành CustomerEntity
ü Đổi tên khoá chính từ Identifier1 thành CustomerID
ü Tại cửa sổ Properties của CustomerID, Type Name: chọn System.Int32
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadItem | Expand id và chọn Identifier
§ Đổi tên khoá chính từ Identifier1 thành CustomerID
§ Tại cửa sổ Properties của CustomerID, Type Name: chọn System.Int32
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadItem | Expand returnParameter và chọn Entity1
§ Đổi từ Entity1 thành Customer
§ Trong customer chọn Identifier1 thành CustomerID và chọn Type
Name: chọn System.Int32
§ Đổi tên thành CustomerName, Type Name: System.String
§ Click phải Customer | Add Type Descriptor
§ Đặt tên là City, Type Name: System.String
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadList | Expand returnParameter và chọn Entity1List
§ Đổi thành CustomerList
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadList | Expand returnParameter | Expand CustomerList |
Chọn Entity1
§ Expand Customer |đổi Identifier1 thành CustomerID
§ Tại cửa sổ Properties của CustomerID, Type Name: chọn System.Int32
§ Chọn Message và đổi tên thành CustomerName, Type Name:
System.String
§ Click phải Customer | Add Type Descriptor
§ Đặt tên là City, Type Name: System.String
· Cửa sổ Solution Explorer | Chọn Class Entity1
public partial class Customer
{
//TODO: Implement additional properties here.
//The property Message is just a sample how a
property could look like.
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string City { get; set; }
}
· Cửa sổ BDC Method Details - CustomerEntity | collap method
ReadItem, ReadList
ü Click Add Method ở combobox và chọn Method Create Delete Member
ü Double click vào method ReadItem và thấy tập tin
CustomerEntityService.cs xuất hiện
ü Khai báo code Using như sau
using System.Data.SqlClient;
using System.Data.SqlTypes;
ü Khai báo phương thức trả về SqlConnection với tên GetSqlConnection
static SqlConnection GetSqlConnection()
{
string connectionString = "server=(local);database=BCSForSharePoint2010;Integrated
Security=true";
SqlConnection sqlConn = new SqlConnection(connectionString);
return sqlConn;
}
ü Trong phương thức ReadItem thay thế code bị lỗi bằng code sau:
public static Customer ReadItem(int id)
{
Customer customer = new Customer();
SqlConnection sqlConn = GetSqlConnection();
sqlConn.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText
= "select CustomerID,
CustomerName, City"
+ " from Customer"
+ " where CustomerID=" + id.ToString();
sqlCommand.Connection
= sqlConn;
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (sqlDataReader.Read())
{
customer.CustomerID
= int.Parse(sqlDataReader[0].ToString());
customer.CustomerName
= sqlDataReader[1].ToString();
customer.City
= sqlDataReader[2].ToString();
}
else
{
customer.CustomerID
= -1;
customer.CustomerName
= "Customer Not
Found";
customer.City
= "";
}
sqlConn.Dispose();
return customer;
}
ü Trong phương thức ReadList khai báo code
/// This is a sample finder method for Entity1.
/// If you want to delete or rename the method think about
changing the xml in the BDC model file as well.
/// </summary>
/// <returns>IEnumerable of Entities</returns>
public static IEnumerable<Customer> ReadList()
{
SqlConnection sqlConnection = GetSqlConnection();
try
{
List<Customer> allCustomers = new List<Customer>();
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection
= sqlConnection;
sqlCommand.CommandText
= "select CustomerID,
CustomerName, City from Customer";
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (sqlDataReader.Read())
{
Customer customer = new Customer();
customer.CustomerID
= int.Parse(sqlDataReader[0].ToString());
customer.CustomerName
= sqlDataReader[1].ToString();
customer.City
= sqlDataReader[2].ToString();
allCustomers.Add(customer);
}
Customer[] customerList = new Customer[allCustomers.Count];
for (int customerCounter = 0; customerCounter <=
allCustomers.Count - 1; customerCounter++)
{
customerList[customerCounter]
= allCustomers[customerCounter];
}
return (customerList);
}
catch (Exception ex)
{
string tellMe = ex.Message;
Customer[] customerlist = new Customer[0];
Customer customer = new Customer();
customer.CustomerID
= -1;
customer.CustomerName
= "Unable to retrieve
data";
customer.City
= "";
customerlist[0]
= customer;
return (customerlist);
}
finally
{
sqlConnection.Dispose();
}
}
ü Trong phương thức Delete khai báo code
public static void Delete(int customerID)
{
SqlConnection sqlConnection = GetSqlConnection();
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection
= sqlConnection;
sqlCommand.CommandText
= "delete customer where
CustomerID=" +
customerID.ToString();
sqlCommand.ExecuteNonQuery();
sqlConnection.Dispose();
}
· Sau khi code xong build project và Deploy
· Mở Central Administration | Application Management | chọn Manage
service applications
· Bạn chọn tiếp Business Data Connectivity Service
· Bạn check CustomerEntity với namespace
BCSCustomer.BdcModel1 và chọn ICon Set Object Permissions
· Bạn Add user và chọn quyền cho user đó
· Mở site với port như lúc đầu đã Debungging (http://acer:8888) và chọn All Site Content
· Chọn Create
· Tại category Data | chọn External List | chọn Create
· Đặt tên BCSCustomer và chọn vào Icon dưới để thêm External Content
Type
· Chọn BdcModel1 với External Content Type
BCSCustomer.BdcModel1.CustomerEntity và chọn OK
· Như vậy BCSCustomer List đã được hiển thị với Method ReadList
· Có thể Delete Item | Click vào Item có Combobox | Chọn Delete
· Quay lại SQL Server thấy đữ liệu cũ như sau và chọn Icon Execute
SQL
· Kết quả Row Item đã bị xoá
· Như vậy đã hoàn thành việc tạo BCS với VS 2010 với phương thức
ReadItem, ReadList, Delete, còn các phương thức khác tương tự