关闭

c#写的一个简单数据库操作类

标签: 数据库c#stringexceptiondatasetcmd
855人阅读 评论(2) 收藏 举报
分类:

using System;
using System.Data;
using System.Data.SqlClient;
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;


public class allsite
{
    public static string connStr;
 public allsite()
 {

 }
    public static  SqlConnection  ConnAllSite()
    {
        connStr = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
        SqlConnection con = new SqlConnection(allsite.connStr);
        return con;
    }
    public static DataSet SelectFromAllSite(string condition)
    {
        string strComm;
        SqlConnection conn = ConnAllSite();
        conn.Open();
        if (condition == "全部")
        {
            strComm = "select * from allSites";
        }
        else
        {
            strComm = "select * from allSites where name like '%" + condition + "%'";
        }
        SqlDataAdapter sda = new SqlDataAdapter(strComm ,conn);
        DataSet ds=new DataSet ();
        sda.Fill (ds);
        conn.Close();
        return ds;
    }
    public static Boolean InsertAllSite(string name, string siteAddress, string description, string category, string imgUrl, string recommendingNumber, string speed, string accuracy, string satisfication)
    {
        string strComm;
        SqlConnection conn = ConnAllSite();
        DateTime dt= DateTime.Now;
        SqlCommand cmd = new SqlCommand("AddAllSite",conn );
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@name", name);
        cmd.Parameters.Add("@siteAddress", siteAddress );
        cmd.Parameters.Add("@description",description );
        cmd.Parameters.Add("@category",category );
        cmd.Parameters.Add("@imgUrl", imgUrl );
        cmd.Parameters.Add("@collectTime",dt.ToString ());
        cmd.Parameters.Add("@recommendingNumber",recommendingNumber );
        cmd.Parameters.Add("@speed",speed );
        cmd.Parameters.Add("@accuracy",accuracy);
        cmd.Parameters.Add("@satisfication", satisfication );
        conn.Open();
        try
        {
            cmd.ExecuteReader();
            conn.Close();
            return true ;
        }
        catch (Exception ee)
        {
            conn.Close();
            return false;
        }
       
    }
    public static Boolean DeleteFromAllSite(string id)
    {
        SqlConnection conn =ConnAllSite ();
        SqlCommand cmd = new SqlCommand("delete from allSites where allSiteId=@allSiteId", conn);
        SqlParameter par = new SqlParameter("@allSiteId", SqlDbType.Int, 4);
        par.Value = id;
        cmd.Parameters.Add(par);
        conn.Open();

        try
        {
            cmd.ExecuteNonQuery ();
            conn.Close();
            return true;
        }
        catch (Exception eee)
        {
            conn.Close();
            return false;
        }
    }
    public static Boolean UpdateAllSite(string id,string recommendingNumber, string speed, string accuracy, string satisfication)
    {
        SqlConnection conn =ConnAllSite ();
        SqlCommand cmd = new SqlCommand("update allSites set recommendingNumber=@recommendingNumber,speed=@speed,accuracy=@accuracy,satisfication=@satisfication where allSiteId=@allSiteId", conn);
        SqlParameter par0 = new SqlParameter("@allSiteId", SqlDbType.Int , 4);
        par0.Value = id;
        SqlParameter par1 = new SqlParameter("@recommendingNumber", SqlDbType.Int , 4);
        par1.Value = recommendingNumber;
        SqlParameter par2 = new SqlParameter("@speed", SqlDbType.Int, 4);
        par2.Value = speed;
        SqlParameter par3 = new SqlParameter("@accuracy", SqlDbType.Int, 4);
        par3.Value = accuracy;
        SqlParameter par4 = new SqlParameter("@satisfication", SqlDbType.Int, 4);
        par4.Value = satisfication;
        cmd.Parameters.Add(par0);
        cmd.Parameters.Add(par1);
        cmd.Parameters.Add(par2);
        cmd.Parameters.Add(par3);
        cmd.Parameters.Add(par4);
        conn.Open();
        try
        {
            cmd.ExecuteNonQuery();
            conn.Close();
            return true;
        }
        catch (Exception eeee)
        {
            conn.Close();
            return false;
        }
    }
    public static Boolean InsertAllSiteRemark(string remark)
    {


        string strComm;
        SqlConnection conn = ConnAllSite();
        DateTime dt = DateTime.Now;
        SqlCommand cmd = new SqlCommand("AddAllSiteRemark", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@remark",remark);
        cmd.Parameters.Add("@createTime",dt);
       
        conn.Open();
        try
        {
            cmd.ExecuteReader();
            conn.Close();
            return true;
        }
        catch (Exception ee)
        {
            conn.Close();
            return false;
        }
    }

    public static Boolean DeleteFromAllSiteRemark(string id)
    {
        SqlConnection conn = ConnAllSite();
        SqlCommand cmd = new SqlCommand("delete from allSiteRemark where allSiteRemarkId=@allSiteRemarkId", conn);
        SqlParameter par = new SqlParameter("@allSiteRemarkId", SqlDbType.Int, 4);
        par.Value = id;
        cmd.Parameters.Add(par);
        conn.Open();

        try
        {
            cmd.ExecuteNonQuery();
            conn.Close();
            return true;
        }
        catch (Exception eee)
        {
            conn.Close();
            return false;
        }
    }
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24470次
    • 积分:348
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:11篇
    • 译文:0篇
    • 评论:4条
    最新评论