简体   繁体   English

更新 SQL 查询不起作用 ASP.NET Core C# 控制台应用程序

[英]Update SQL Query not working ASP.NET Core C# Console App

Working on a project which will update a column in my database with the users instagram ID.正在处理一个项目,该项目将使用用户 Instagram ID 更新我的数据库中的一列。 When I try run the query it isn't updating the column I've specified in my query.当我尝试运行查询时,它不会更新我在查询中指定的列。

I'm trying to update a table based off the Instagram ID of a user but nothing is being updated on my dev db.我正在尝试根据用户的 Instagram ID 更新表,但我的开发数据库上没有任何更新。 I feel like it has something to do with my SQL query我觉得这与我的 SQL 查询有关

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"];

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

                    using (SqlConnection con = new SqlConnection(connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Update ApplicationUser Set InstagramId = '" + userProfile.id + "'" + "where Instagram =  '" + userProfile.username + "'", con);


                    }
                   
                    // 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}");
                }
            }
        }

Answered my own question:回答了我自己的问题:

Was missing the following lines of code:缺少以下代码行:

cmd.Connection.Open();
cmd.ExecuteNonQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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