简体   繁体   中英

I want to retrieve data from sharepoint list using c# and store them into SQL server DB tables. How can I do this?

string strUrl = "some url";
        using (SPSite oSite = new SPSite(strUrl))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                SPList list = oWeb.Lists["TargetList"];

                foreach (SPField field in list.Fields)
                {
                    Console.WriteLine(field.Title);
                }
            }
        }

This code is entering break state showing "file not found exception" .Dont know where the problem is. Also I want to parse the sharepoint list. After this how will I insert the data into tables created in my database

        string strUrl = "some url";
        using (SPWeb myWeb = new SPSite(strUrl).OpenWeb())
        {
                SPListItemCollection items = myWeb.Lists["YourListName"].Items;

                string fieldValue1 = "";
                //...
                string fieldValuen = "";
                SqlConnection con = new SqlConnection("connection string");
                cmd.Connection = con;

                using (SqlCommand cmd = new SqlCommand())
                {
                    con.Open();
                    foreach (SPListItem item in items)
                    {
                        fieldValue1 = item["column 1 name in SPList"].ToString();
                        //...
                        fieldValuen = item["column n name in SPList"].ToString();

                        string sqlCommand = "INSERT INTO TABLE_NAME(field1ColumnName, ..., fieldnColumnName) VALUES('" + fieldValue1 + "', ...,'" + fieldValue1 + "')";

                        cmd.CommandText = sqlCommand;

                        cmd.ExecuteNonQuery();
                    }

                    con.Close();
                }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM