c#总结

不能不懂的类和对象,抽空整理出来的!

引入系统中的命名空间。

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Security.Cryptography;
/// <summary>
///common 的摘要说明
/// </summary>

定义公共基础类
public class common
{
 public common()
 {
       
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }

连接数据库字符串的公共方法并返回对象conn!

    public SqlConnection Getconn()
    {
        string connstr = ConfigurationManager.ConnectionStrings["suoweiConnectionString2"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstr);
        return conn;
    }

执行一个T-sql语句时,用此方法返回受影响的行数并返回一个整型数值进行判断是否操作数据库成功!

很性感的一个方法!

    public int sqlcmd(string sqlstr)
    {
        SqlConnection conn = Getconn();
        conn.Open();
        SqlCommand cmd = new SqlCommand(sqlstr, conn);
        try
        {
            cmd.ExecuteNonQuery();
            return 1;
        }
        catch
        {
            return 0;
        }
        finally
        {
            conn.Dispose();
        }
    }

将查询到的数据填进数据表里,此方法很好用!

    public DataTable dt(string sqlstr)
    {
        SqlConnection conn = Getconn();
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return (ds.Tables[0]);
    }

数据读取器读取数据将其放入

    public SqlDataReader dr(string sqlstr)
    {
        SqlConnection conn = Getconn();
        conn.Open();
        SqlCommand cmd = new SqlCommand(sqlstr, conn);
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return dr;
    }

弹出对话框方法

    public string MessageBox(string str, string url)
    {
        string mystr = "<script language=javascript>alert('" + str + "');location='" + url + "'</script>";
        return mystr;
    }

    public string MessageBox(string txtstr)
    {
        string str = "<script language=javascript>alert('" +txtstr + "')</script>";
        return str;
    }

MD5对数据进行加密

    public string md5(string strpwd)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.Default.GetBytes(strpwd);
        byte[] md5data = md5.ComputeHash(data);
        md5.Clear();
        string str = "";
        for (int i = 0; i < md5data.Length - 1; i++)
        {
            str += md5data[i].ToString("x").PadLeft(2, '0');
        }
        return str;
    }
}

posted @ 2013-01-21 09:28  左手边的书  阅读(100)  评论(0编辑  收藏  举报