簡體   English   中英

如何遍歷表以更新每一行

[英]How To Loop Through A Table To Update Each Row

我正在嘗試使用 instagramIds 更新一列空值,這是我目前的方法,但控制台應用程序只是繼續運行並且不會更新數據庫中的任何值。

public static async Task<InstagramUser> ScrapeInstagram(string url)
{
    using (var client = new HttpClient())
    {
        var response = await client.GetAsync(url);
        if (response.IsSuccessStatusCode)
        {
            // create html document
            var htmlBody = await response.Content.ReadAsStringAsync();
            var htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(htmlBody);

            // select script tags
            var scripts = htmlDocument.DocumentNode.SelectNodes("/html/body/script");

            // preprocess result
            var uselessString = "window._sharedData = ";
            var scriptInnerText = scripts[0].InnerText
                .Substring(uselessString.Length)
                .Replace(";", "");

            // serialize objects and fetch the user data
            dynamic jsonStuff = JObject.Parse(scriptInnerText);
            dynamic userProfile = jsonStuff["entry_data"]["ProfilePage"][0]["graphql"]["user"];

            List<String> columnData = new List<String>();


            //Update database query 
            string connectionString = @"Server=myProject-dev-db.cothtpanmcn7.ap-southeast-2.rds.amazonaws.com;Database=Projectdb;User Id=testadmin;Password=U8gs7vb7C7yvakXf;MultipleActiveResultSets=true;Trusted_Connection=False;";

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                //get null values from database  
                string query = "Select * from ApplicationUser where InstagramId is null";
                using (SqlCommand command = new SqlCommand(query, con))
                {
                    command.Connection.Open();

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            columnData.Add(reader.GetString(0));
                        }
                    }
                }

                for (int index = 0; index < columnData.Count(); index++)
                {
                    //get username and scrape info 
                    var instagramInfo = new InstagramUser
                    {   
                        Id = userProfile.id,   
                    };
                    columnData.Add(instagramInfo.ToString());
                }

                SqlCommand cmd = new SqlCommand("Update ApplicationUser Set InstagramId = '" + columnData + "'" + "where InstagramUsername =  '" + userprofile.username + "'", con);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();

            }

            // create an InstagramUser
            var instagramUser = new InstagramUser
            {
                FullName = userProfile.full_name,
                FollowerCount = userProfile.edge_followed_by.count,
                FollowingCount = userProfile.edge_follow.count,
                Id = userProfile.id,
                url = url
            };
            return instagramUser;
        }
        else
        {
            throw new Exception($"Something wrong happened {response.StatusCode} - {response.ReasonPhrase} - {response.RequestMessage}");
        }
    }
}

我目前的方法是創建一個列表,將所有為空的 instagramID 添加到該列表中。 從那里我將所有 instagramIds 添加到該列表中,然后在 Instagram 上抓取他們的用戶名,例如https://www.instagram.com/therock/?__a=1

然后我用他們的 Instagram Id 更新列 InstagramUsername

我不會告訴你整個邏輯,但只有一些步驟希望它對你有用

  • 使用 istagramUserName 的過濾器參數直接運行更新查詢,例如更新表集 IstagramID = '123' where istagramUserName = "OwaisShahab"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM