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
Difference between Sharepoint Site and Application pages.
This shows how to Create Custom Properties for Sharpoint WebParts?
This shows how to Create Mailbox and configuring Sharepoint outgoing email settings?
A brief path for administering sharepoint sites.
This shows procedure for Configuring Site level security in sharepoint 2007.
This shows what is connectable webparts in sharpoint and how to connect two webparts in sharepoint.
Transfer SQL database contents to Sharpoint List (1171 Views)