During my university small project doing Internet Technology, having opportunities doing Social Networking Service Website with using ASP.NET. I have done some coding that might help you and me. The coding below will accomplish some tasks, such as getting list of friend, list of friend of friend, and getting friendly times string.
List of Friend
First of all, we need a Friend Table and User Table, I have created like this
- create table Users (
- uid int constraint ctUser_pk primary key(uid) identity,
- username char(100) not null default ”,
- password char(32) not null default ”,
- gender int default ‘0’,
- email char(100) default ”,
- address char(255) default ”,
- avatar char(255) default ”
- );
- create table Friend (
- uid int not null,
- fuid int not null,
- fusername char(30) not null,
- status int,
- dateline datetime
- );
Then I have created a Friend Class with the method implementation below/
- public DataTable getFriendList(int uid, int limit)
- {
- SqlConnection sqlConn = Util.openDB();
- SqlCommand sqlCommand = new SqlCommand(“Select Top(@limit) f.*,u.* From Friend f Left Join Users u On u.Uid = f.Uid Where f.uid = @uid”, sqlConn);
- sqlCommand.Parameters.Add(“uid”, uid);
- sqlCommand.Parameters.Add(“limit”, limit);
- SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
- DataTable dt = new DataTable();
- sqlAdapter.Fill(dt);
- Util.closeDB();
- return dt;
- }
List of Friend of Friend
Method implementation of getting friend of friend:
- public DataTable gerFriendOfFriend(int uid, int limit)
{ - SqlConnection sqlConn = Util.openDB();
- SqlCommand sqlCommand = new SqlCommand(“select distinct Top(@limit) f.fuid, f.fusername, u.avatar from friend f left join users u on u.uid = f.fuid where f.uid in (select fuid from friend where uid = @uid) and f.fuid != @uid”, sqlConn);
- sqlCommand.Parameters.Add(“uid”, uid);
- sqlCommand.Parameters.Add(“limit”, limit);
- SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCommand);
- DataTable dt = new DataTable();
- sqlAdapter.Fill(dt);
- Util.closeDB();
- return dt;
- }
Friendly Time String
I have created another class called Util. The method implementation of getting friendly time string as below:
- public static string gmtime(DateTime t)
{ - string timeString = “”;
- // show x seconds ago, 1-59 minutes 1-23 hours 1-7 days ago, otherwise original
- DateTime now = DateTime.Now;
- // convert into 1970 time format (this called unix timestamp)
- long nowtime = Util.ConvertToTimestamp(now);
- long time = Util.ConvertToTimestamp(t);
- long diff = nowtime – time;
- if (diff < 0)
- {
- timeString = “Invalid Time”;
- }
- else if (diff < 60)
- {
- timeString = diff + ” seconds ago”;
- }
- else if (diff < 3600)
- {
- decimal minute = Math.Round((decimal) diff / 60, 0);
- timeString = minute + ” minutes ago”;
- }
- else if (diff < 86400)
- {
- decimal hour = Math.Round((decimal) diff / 60 / 60, 0);
- timeString = hour + ” hours ago”;
- }
- else if (diff < 604800) // 7 days before
- {
- decimal day = Math.Round((decimal) diff / 60 / 60 / 24, 0);
- timeString = day + ” days ago”;
- }
- else
- {
- timeString = t.ToString(); // show original after 7 days
- }
- return timeString;
- }
- // reference: http://www.dreamincode.net/code/snippet2094.htm
- // convert datetime to unix timestamp
- public static long ConvertToTimestamp(DateTime value)
- {
- //create Timespan by subtracting the value provided from
- //the Unix Epoch
- TimeSpan span = (value – new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
- //return the total seconds (which is a UNIX timestamp)
- return (long)span.TotalSeconds;
- }