C# 如何循环表以更新每一行
我正在尝试使用instagramIds更新一列空值,这是我目前的方法,但console应用程序一直在运行,不更新数据库中的任何值C# 如何循环表以更新每一行,c#,sql,sql-server,asp.net-core,C#,Sql,Sql Server,Asp.net Core,我正在尝试使用instagramIds更新一列空值,这是我目前的方法,但console应用程序一直在运行,不更新数据库中的任何值 public static async Task<InstagramUser> ScrapeInstagram(string url) { using (var client = new HttpClient()) { var response = await client.GetAsync(url); if
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添加到该列表中。在删除Instagram中的用户名后,我将所有Instagramid添加到该列表中
然后,我用instagram ID更新InstagramMuserName专栏,我不打算告诉您全部逻辑,只希望其中的一些步骤对您有效 使用istagramUserName的筛选器参数直接运行更新查询 e、 g更新表集IstagramID='123',其中istagramUserName=OWAISSHAAB
使用它是一个好主意。这将自动执行转换和转义序列。我正在尝试对数据库中的所有空值执行此操作,而不仅仅是一个值。好的,现在可以再启动一次1。以json格式2从api获取数据。使用serialization 3将其转换为c对象。在该对象上使用循环从数据库中逐行获取数据并进行更新,希望对您有所帮助