📄 news.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections ;
namespace Hr.component
{
public class News:DatabaseOperation
{
public News()
{
}
public static DataSet GetNews()
{
strSQL = "SELECT * FROM news";
try
{
return ExecuteReturnDS(strSQL);
}
catch
{
throw new Exception("获取所有新闻信息失败!");
}
}
public DataView GetNewsPicture(int newsId)
{
String strsql;
DataSet myDs;
strsql="select picture from news where Id="+newsId;
myDs=ExecuteReturnDS(strsql);
return myDs.Tables[0].DefaultView ;
}
public DataView GetNewsLatest10()
{
String strsql;
DataSet myDs;
strsql="select top 10 Id,title from news order by Id DESC";
myDs=ExecuteReturnDS(strsql);
return myDs.Tables[0].DefaultView ;
}
public DataView GetNewsImportant10()
{
String strsql;
DataSet myDs;
strsql="select top 10 Id,title from news order by hits desc";
myDs=ExecuteReturnDS(strsql);
return myDs.Tables[0].DefaultView ;
}
public void AddNews(ArrayList tempary)
{
SqlConnection myConn=new SqlConnection(strConn);
SqlCommand myCm=new SqlCommand("AddNews",myConn);
myCm.CommandType =CommandType.StoredProcedure ;
myCm.Parameters.Add(new SqlParameter("@publicDate",SqlDbType.DateTime));
myCm.Parameters["@publicDate"].Value =tempary[0];
myCm.Parameters.Add(new SqlParameter("@source",SqlDbType.VarChar,30 ));
myCm.Parameters["@source"].Value =tempary[1];
myCm.Parameters.Add(new SqlParameter("@title",SqlDbType.VarChar,100));
myCm.Parameters["@title"].Value =tempary[2];
myCm.Parameters.Add(new SqlParameter("@content",SqlDbType.Text ));
myCm.Parameters["@content"].Value =tempary[3];
myCm.Parameters.Add(new SqlParameter("@picture",SqlDbType.VarChar, 100 ));
myCm.Parameters["@picture"].Value =tempary[4];
myCm.Parameters.Add(new SqlParameter("@hits",SqlDbType.Int));
myCm.Parameters["@hits"].Value =0;
try
{
myConn.Open() ;
myCm.ExecuteNonQuery() ;
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
finally
{
myCm.Dispose() ;
myConn.Close() ;
}
}
public static void DeleteNewsById(int Id)
{
strSQL = "Delete From news Where Id="+Id;
try
{
ExecuteNonQuery(strSQL);
}
catch
{
throw new Exception("delete failed!");
}
}
public SqlDataReader GetPicNews()
{
string strsql;
SqlDataReader result;
try
{
SqlConnection myConn=new SqlConnection(strConn);
strsql="select top 10 id,title,descr=' '+SUBSTRING(content,0,100)+'......', picture image from news where picture is not null and picture<>'' order by hits DESC";
SqlCommand myCm=new SqlCommand(strsql,myConn);
myConn.Open ();
result=myCm.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
public SqlDataReader GetNewsByHits()
{
string strsql;
SqlDataReader result;
try
{
SqlConnection myConn=new SqlConnection(strConn);
strsql="select top 10 id,title,descr=' '+SUBSTRING(content,0,100)+'......',image=case when(not picture<>'') then ' <images src=Img/'+picture+' Border=1 width=70 height=100>' else ' <images src=images/pic.jpg border=1 width=70 height=100>' end from news order by hits DESC";
SqlCommand myCm=new SqlCommand(strsql,myConn);
myConn.Open ();
result=myCm.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
public DataRow GetNewsDetail(int newsId)
{
string strsql;
DataSet myDs;
try
{
strsql="select Id,title,source,publicDate,hits,content from news where Id= " + newsId;
myDs=ExecuteReturnDS(strsql);
return myDs.Tables[0].Rows[0];
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
public DataRow GetNewsDetail(string name)
{
string strsql;
DataSet myDs;
try
{
strsql="select Id,title,source,publicDate,hits,content from news where title= " + name;
myDs=ExecuteReturnDS(strsql);
return myDs.Tables[0].Rows[0];
}
catch(System.Data.SqlClient.SqlException er)
{
throw new Exception(er.Message);
}
}
public static void Hits(string newsId)
{
strSQL = "Update news set hits=hits+1 Where Id= " + newsId;
try
{
ExecuteReturnDS(strSQL);
}
catch
{
throw new Exception("更新新闻点击次数失败!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -