· 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
ü File | New Project | SharePoint | 2010 | Chọn Business Data
Connectivity Model và đặt tên BCSCustomer
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadItem | Expand id và chọn Identifier
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadItem | Expand returnParameter và chọn Entity1
§ Đổi thành CustomerList
ü Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand
ReadList | Expand returnParameter | Expand CustomerList |
Chọn Entity1
{
//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
ü 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
{
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:
{
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
{
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
· Chọn Create
· 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ự