Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HashContext db = new HashContext();
- Hashtag hashtag = new Hashtag() { Posts = new List<Post>() };
- Post post = new Post() { Hashtags = new List<Hashtag>() };
- string hashtagsList = "";
- string sqlCommand;
- var hashsetList = new HashSet<string>(listOfHashtags, StringComparer.OrdinalIgnoreCase);
- post.PostSource = PostSource;
- post.Avatar = Avatar;
- post.Date = Date;
- post.Username = Username;
- post.ContentDescription = ContentDescription;
- if (ContentImageUrl != "")
- {
- post.ContentImageUrl = ContentImageUrl;
- }
- for (int x = 0; x < hashsetList.Count; x++)
- {
- hashtagsList += " If not exists(select HashtagId, hashtagname from Hashtags where HashtagName like '" + hashsetList.ElementAt(x) + "')" +
- " begin " +
- "INSERT INTO Hashtags(HashtagName) VALUES('" + hashsetList.ElementAt(x) + "'); " +
- "declare @HashtagId"+x+" int; " +
- "Set @HashtagId"+x+" = @@IDENTITY; " +
- "end " +
- "else " +
- "begin " +
- "set @HashtagId"+x+ " = (select HashtagId from Hashtags where HashtagName like '" + hashsetList.ElementAt(x) + "'); " +
- "end; " +
- "update Hashtags set HashtagName='" + hashsetList.ElementAt(x) + "' where hashtagId like 'jacek'" +
- "IF @@ROWCOUNT = 0"+
- " INSERT INTO PostHashtags(Post_PostId, Hashtag_HashtagId) VALUES(@PostId, @HashtagId"+x+ ")";
- }
- sqlCommand = " If Not Exists(select contentdescription from posts where ContentDescription like '" + ContentDescription + "') begin " +
- "Insert into Posts(Date, Avatar, Username, ContentDescription " +
- ", ContentImageUrl, DirectLinkToStatus, PostSource) values(SYSDATETIME(),'" + Avatar + "', '" + Username + "', '" + ContentDescription + "', '"
- + ContentImageUrl + "', '" + UrlAddress + "', '" + PostSource + "') " +
- "declare @PostId int; " +
- "Set @PostId = @@IDENTITY; " + hashtagsList + "end ";
- string _connString = ConfigurationManager.ConnectionStrings["HashContext"].ConnectionString;
- using (var connection = new SqlConnection(_connString))
- {
- connection.Open();
- using (SqlCommand command = new SqlCommand(sqlCommand, connection))
- {
- command.ExecuteReader();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement