Creating Business Data Connectivity Service using Visual Studio 2010

You will learn in this article how to Create Business Data Connectivity Service using Visual Studio 2010.


New Create Database name BCSForSharePoint2010

  • Create a new table name: Customer with the following columns:

    Module6_video04_BCS_VS2010_01.png

  • Enter new customer data

    Module6_video04_BCS_VS2010_02.png

Open Visual Studio 2010 to create a new BCS

  • File | New Project | SharePoint | 2010 | Select Data Connectivity Business Model and name BCSCustomer

    Module6_video04_BCS_VS2010_03.png

  • Select Site and click Finish to Dubugging

    Module6_video04_BCS_VS2010_04.png

Interface creates the following new

        Module6_video04_BCS_VS2010_05.png

  • Rename the Class Entity1 CustomerEntity

    Module6_video04_BCS_VS2010_06.png

  • Rename the primary key of CustomerID from Identifier1

    Module6_video04_BCS_VS2010_07.png

  • In the Properties window of the CustomerID, Name Type: select System.Int32

    Module6_video04_BCS_VS2010_08.png

BDC Explorer window

  • Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand ReadItem | Expand id and choose Identifier

    Module6_video04_BCS_VS2010_09.png

    -Rename the primary key of CustomerID from Identifier1

    Module6_video04_BCS_VS2010_10.png

    -In the Properties window of the CustomerID, Name Type: select System.Int32

    Module6_video04_BCS_VS2010_10 (1).png

    -Selected the following image

    Module6_video04_BCS_VS2010_11.png

  • Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand ReadItem | Expand returnParameter and select Entity1

    Module6_video04_BCS_VS2010_12.png

    -Change from the Customer Entity1

    Module6_video04_BCS_VS2010_13.png

    -In the customer select the CustomerID and choose Identifier1 Name Type: select System.Int32

    Module6_video04_BCS_VS2010_14.png

    -Select Message

    Module6_video04_BCS_VS2010_15.png

    -Renamed CustomerName, Type Name: System.String

    Module6_video04_BCS_VS2010_16.png

    -Click to Customer | Add Type Descriptor

    Module6_video04_BCS_VS2010_17.png

    -Named the City, the Type Name: System.String

    Module6_video04_BCS_VS2010_18.png

    -Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand ReadList | Expand returnParameter and select  Entity1List

    Module6_video04_BCS_VS2010_20.png

    -CustomerList changed

    Module6_video04_BCS_VS2010_21.png

    -Expand Model | Expand BdcModel1 | Expand CustomerEntity | Expand ReadList | Expand returnParameter | Expand  CustomerList | Choose Entity1

    Module6_video04_BCS_VS2010_22.png

    -Customer changed

    Module6_video04_BCS_VS2010_23.png

    -Expand Customer | Change Identifier1 the CustomerID

    Module6_video04_BCS_VS2010_24.png

    -In the Properties window of the CustomerID, Name Type: select System.Int32

    Module6_video04_BCS_VS2010_25.png

    -Select Message and renamed CustomerName, Type Name: System.String

    Module6_video04_BCS_VS2010_26.png

    -Click to Customer | Add Type Descriptor

    Module6_video04_BCS_VS2010_27.png

    -Named the City, the Type Name: System.String

    Module6_video04_BCS_VS2010_28.png

Solution Explorer window | Select Class Entity1

        Module6_video04_BCS_VS2010_29.png

  • Renamed Customer

    Module6_video04_BCS_VS2010_30.png

  • Declare the following code

    Module6_video04_BCS_VS2010_31.png

    public partial class Customer
        {
           
    / / TODO: Implement Additional properties here.
           
    / / The Message property 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;}
        }

Method Details window BDC - CustomerEntity | collap ReadItem method, ReadList

  • Click the Add method in the combobox and select the Create Delete Method Member

    Module6_video04_BCS_VS2010_32.png

  • Interface as follows

    Module6_video04_BCS_VS2010_33.png

  • Double click on the file and found ReadItem method appears CustomerEntityService.cs
  • Using the following declaration code

    Module6_video04_BCS_VS2010_34.png

    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
     
  • Declare the SqlConnection method returns the name GetSqlConnection

    Module6_video04_BCS_VS2010_35.png

    GetSqlConnection static SqlConnection ()
            {
                string ConnectionString =
    "server = (local) database = BCSForSharePoint2010; Integrated Security = true";
                SqlConn SqlConnection = new SqlConnection (ConnectionString);
                sqlConn
    return;
            }
     
  • In the alternative method ReadItem error code with the following code:

    Module6_video04_BCS_VS2010_36.png

    public static ReadItem Customer (int id)
            {

                Customer customer = new Customer ();

                SqlConn = GetSqlConnection SqlConnection ();

                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;
            }

     

  • In the method declaration code ReadList

    Module6_video04_BCS_VS2010_37.png

    / / / <summary>
            / / / This is a sample method for Entity1 finder.
            / / / If You Want to delete or rename the method to think about changing the xml file in the BDC model 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 [] = new Customer customerList [allCustomers.Count];

                    for (int customerCounter = 0; customerCounter <= allCustomers.Count - 1; customerCounter + +)

                    {

                        customerList [customerCounter] = allCustomers [customerCounter];
                    }
                    return (customerList);
                }
                catch (Exception ex)
                {
                    Tellme String = 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 ();
                }
            }

  • In the Delete method declaration code

    Module6_video04_BCS_VS2010_38.png

    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 ();
            }

After completion code hi build and Deploy project

Module6_video04_BCS_VS2010_39.png

Open Central Administration | Application Management | Manage service selected applications

Module6_video04_BCS_VS2010_40.png

You choose to Business Data Connectivity Service

Module6_video04_BCS_VS2010_41.png

You check CustomerEntity BCSCustomer.BdcModel1 with namespace and select Icon Set Object Permissions

Module6_video04_BCS_VS2010_42.png

Add your user rights for that user and select

Module6_video04_BCS_VS2010_43.png

Open the site with the port as was at first Debungging ( http://acer:8888 ) and select All Site Content

Module6_video04_BCS_VS2010_44.png

Choose Create

Module6_video04_BCS_VS2010_45.png

Data at the category | select External List | select Create

Module6_video04_BCS_VS2010_46.png

BCSCustomer named below and select the icon to add External Content Type

Module6_video04_BCS_VS2010_47.png

Choose BdcModel1 with External Content Type and select OK BCSCustomer.BdcModel1.CustomerEntity

Module6_video04_BCS_VS2010_48.png

Choose Create

Module6_video04_BCS_VS2010_49.png

Thus BCSCustomer List has been shown to Method ReadList

Module6_video04_BCS_VS2010_50.png

Item can Delete | Click on a Combobox Item | Add Delete

Module6_video04_BCS_VS2010_51.png

OK

Module6_video04_BCS_VS2010_52.png

Item removed

Module6_video04_BCS_VS2010_53.png

Back to SQL Server to see the old data as follows and select Execute SQL Icon

Module6_video04_BCS_VS2010_54.png

Result Row Item deleted

Module6_video04_BCS_VS2010_55.png

So we finished creating the VS 2010 BCS method ReadItem, ReadList, Delete, and other similar methods

erver'>