📄 friends.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public interface IFriends
{
//向数据库中插入好友
bool InsertFriends(string FriendName,string UserName);
//删除好友
bool DeleteFriends(int id);
//取得所有的好友信息
SqlDataReader GetFriendAll(string UserName);
//判断好友是否存在
bool IsExists(string FriendName, string UserName);
//更新好友
bool UpdateFriends(int id,DateTime addtime);
string GetFriendName(int id);
//通过好友名字得到好友信息
SqlDataReader GetFriendMessageByFriendName(string FriendName);
}
/// <summary>
/// Friends 的摘要说明
/// </summary>
public class Friends :IFriends
{
public bool InsertFriends(string FriendName,string UserName)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
bool flag = false;
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand("insert into Friends(FriendName,UserName) values(@FriendName,@UserName)", conn);
comm.Parameters.Add("@FriendName", SqlDbType.VarChar, 50);
comm.Parameters.Add("@UserName", SqlDbType.VarChar, 50);
comm.Parameters["@FriendName"].Value = FriendName;
comm.Parameters["@UserName"].Value = UserName;
int isOk = comm.ExecuteNonQuery();
if (isOk > 0)
{
flag = true;
}
else
{
flag = false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return flag;
}
public bool DeleteFriends(int id)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
string SqlStr = "DELETE from Friends where id="+id;
SqlConnection conn = new SqlConnection(connStr);
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
int flag = 0;
SqlCommand comm = new SqlCommand(SqlStr, conn);
flag = comm.ExecuteNonQuery();
if (flag > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
public SqlDataReader GetFriendAll(string UserName)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
string SqlStr = "select distinct id,FriendName,UserName,AddDate from Friends where UserName='"+UserName+"' order by id desc ";
SqlDataReader dr = null;
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
dr = comm.ExecuteReader();
}
catch (Exception ex)
{
throw new Exception(ex.Message,ex);
}
return dr;
}
public bool IsExists(string FriendName, string UserName)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
string SqlStr = "select * from Friends where FriendName='"+FriendName+"' and UserName='"+UserName+"'";
bool flag = false;
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
flag = true;
}
else
{
flag = false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return flag;
}
public bool UpdateFriends(int id,DateTime addtime)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
string SqlStr = "update Friends set AddDate='" + addtime + "' where id=" + id;
bool flag = false;
SqlConnection conn=new SqlConnection(connStr);
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
int i = comm.ExecuteNonQuery();
comm.Dispose();
if (i > 0)
{
flag = true;
}
else
{
flag = false;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return flag;
}
public SqlDataReader GetFriendMessageByFriendName(string FriendName)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
string SqlStr = "select * from Users where UserName='"+FriendName+"'";
SqlConnection conn = new SqlConnection(connStr);
SqlDataReader dr = null;
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
dr = comm.ExecuteReader();
comm.Dispose();
}
catch (Exception ex)
{
if (conn.State.ToString() == "Open")
conn.Close();
throw new Exception(ex.Message, ex);
}
return dr;
}
public string GetFriendName(int id)
{
string connStr = ConfigurationManager.ConnectionStrings["DBConnectionStrings"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
string name = "";
string sqlFriendName = "select * from Friends where id="+id;
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand com = new SqlCommand(sqlFriendName, conn);
SqlDataReader druser = com.ExecuteReader();
if (druser.Read())
{
name = druser["FriendName"].ToString();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
return name;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -