Transfer SQL database contents to Sharpoint List

By Perry

This shows procedure to Transfer SQL database contents to Sharpoint List.

I have taken Visual studio SQL database here. Create database from VS by selecting connection and right click, create database and create example table "mytable".

Servername will be (local)\sqlexpress
Databasename: CustomerDB
tablename: customers which has name and phone columns

Follow below procedure from your code to list the customers table in your sharpoint list:

Using System.Data.SqlClient;
SPsite sitecollection = new SPSite(your url);
SPWeb web = sitecollection.Openweb();
SPList list = web.Lists["customers"];
SqlConnection con = new SqlConnection(constring); => You can copy connection string from database property
SqlCommand cmd = new SqlCommand("Select * from customers", con);
con.Open();
sqlDataReader reader = cmd.ExecuteReader();
SPListItem item;
While(reader.Read()) {
item = list.Items.Add();
item["title"] = reader["custName"].ToString();
item["phone"] = reader["phone"].ToString();
item.Update(); -> Don't forget to put this for each items
}
reader.Close();
con.Close();

Want to learn more? See my other Sharepoint FAQs listed below.

Related FAQs

This shows what is connectable webparts in sharpoint and how to connect two webparts in sharepoint.
Sometime you need to user your created webpart in other site. You need not to create it again. You can import and export created webpart to new site.
This shows a method for Geting the current user who using the Sharepoint site.
This shows how to Create Custom Properties for Sharpoint WebParts?
This shows how to Configure List and Item level security in Sharepoint 2007?
This shows procedure for Configuring Site level security in sharepoint 2007.
Transfer SQL database contents to Sharpoint List  (587 Views)
Create New Account