Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. HashContext db = new HashContext();
  2.             Hashtag hashtag = new Hashtag() { Posts = new List<Post>() };
  3.             Post post = new Post() { Hashtags = new List<Hashtag>() };
  4.  
  5.             string hashtagsList = "";
  6.             string sqlCommand;
  7.             var hashsetList = new HashSet<string>(listOfHashtags, StringComparer.OrdinalIgnoreCase);
  8.             post.PostSource = PostSource;
  9.             post.Avatar = Avatar;
  10.             post.Date = Date;
  11.             post.Username = Username;
  12.             post.ContentDescription = ContentDescription;
  13.             if (ContentImageUrl != "")
  14.             {
  15.                 post.ContentImageUrl = ContentImageUrl;
  16.             }
  17.  
  18.             for (int x = 0; x < hashsetList.Count; x++)
  19.             {
  20.                 hashtagsList += " If not exists(select HashtagId, hashtagname from Hashtags where HashtagName like '" + hashsetList.ElementAt(x) + "')" +
  21.                    " begin " +
  22.                     "INSERT INTO Hashtags(HashtagName) VALUES('" + hashsetList.ElementAt(x) + "'); " +
  23.                     "declare @HashtagId"+x+" int; " +
  24.                     "Set @HashtagId"+x+" = @@IDENTITY; " +
  25.                     "end " +
  26.                     "else " +
  27.                     "begin " +
  28.                     "set @HashtagId"+x+ " = (select HashtagId from Hashtags where HashtagName like '" + hashsetList.ElementAt(x) + "'); " +
  29.                     "end; " +
  30.                     "update Hashtags set HashtagName='" + hashsetList.ElementAt(x) + "' where hashtagId like 'jacek'" +
  31.                     "IF @@ROWCOUNT = 0"+
  32.                     " INSERT INTO PostHashtags(Post_PostId, Hashtag_HashtagId) VALUES(@PostId, @HashtagId"+x+ ")";
  33.             }
  34.             sqlCommand = " If Not Exists(select contentdescription from posts where ContentDescription like '" + ContentDescription + "') begin " +
  35.                "Insert into Posts(Date, Avatar, Username, ContentDescription " +
  36.                    ", ContentImageUrl, DirectLinkToStatus, PostSource) values(SYSDATETIME(),'" + Avatar + "', '" + Username + "', '" + ContentDescription + "', '"
  37.                     + ContentImageUrl + "', '" + UrlAddress + "', '" + PostSource + "') " +
  38.                    "declare @PostId int; " +
  39.                    "Set @PostId = @@IDENTITY; " + hashtagsList + "end ";
  40.             string _connString = ConfigurationManager.ConnectionStrings["HashContext"].ConnectionString;
  41.             using (var connection = new SqlConnection(_connString))
  42.             {
  43.                 connection.Open();
  44.                 using (SqlCommand command = new SqlCommand(sqlCommand, connection))
  45.                 {
  46.                     command.ExecuteReader();
  47.                 }
  48.  
  49.  
  50.  
  51.  
  52.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement