asp.net一些全局常用函数

  1 using  System;
  2 using  System.Data;
  3 using  System.Configuration;
  4 using  System.Web;
  5 using  System.Web.Security;
  6 using  System.Web.UI;
  7 using  System.Web.UI.WebControls;
  8 using  System.Web.UI.WebControls.WebParts;
  9 using  System.Web.UI.HtmlControls;
 10 using  System.Data.OleDb;
 11
 12 /// <summary>
 13/// Inc 的摘要说明:
 14/// 一些全局常用函数
 15/// </summary>

 16 public   class  Inc : System.Web.UI.Page
 17 {
 18    public string Today;
 19    public OleDbConnection conn;
 20    public Inc()
 21    {
 22        conn = new OleDbConnection(ConfigurationSettings.AppSettings["ConnStr"]);            //数据库连接串
 23        Today = DateTime.Today.ToString("D"+ " &nbsp;" + DateTime.Today.ToString("dddd");    //今天日期和星期
 24        conn.Close();
 25    }
                                                                            //类初始化
 26    public int GetNewID(string s_tablename)
 27    {
 28        string sql="update pub_sno set n_value=n_value+1 where s_tablename='" + s_tablename + "'";
 29        ExecuteNonQuery(sql);                                                                //先将原流水号+1
 30        sql = "select n_value from pub_sno where s_tablename='" + s_tablename + "'";
 31        int id = int.Parse(ExecuteScalar(sql).ToString());                                    //取得新的流水号
 32        return id;
 33    }
                                                //获取新的流水号
 34    public void Alert(string msg)
 35    {
 36        Response.Write("<Script>alert('" + msg + "');</Script>");
 37    }
                                                        //只弹出消息无转向
 38    public void Alert(string msg, bool endresponse)
 39    {
 40        Alert(msg);
 41        if (endresponse == true)
 42            Response.End();
 43    }
                                        //弹出消息并终止运行
 44    public void Alert(string msg, string returnurl)
 45    {
 46        if (returnurl == "")
 47            Response.Write("<Script>alert('" + msg + "');history.back()</Script>");
 48        else
 49            Response.Write("<Script>alert('" + msg + "');location='" + returnurl + "'</Script>");
 50    }
                                        //弹出消息并转向指定连接
 51    public void Alert(string msg,string returnurl,bool endresponse)
 52    {
 53        Alert(msg, returnurl);
 54        if (endresponse == true)
 55            Response.End();
 56    }
                        //转向连接并终止运行
 57    public void ExecuteNonQuery(string sql)
 58    {
 59        if (conn.State != ConnectionState.Open) conn.Open();
 60        OleDbCommand cm = new OleDbCommand(sql, conn);
 61        cm.ExecuteNonQuery();
 62        cm.Dispose();
 63    }
                                                //执行无返回数据的SQL语句
 64    public object ExecuteScalar(string sql)
 65    {
 66        if (conn.State != ConnectionState.Open) conn.Open();
 67        OleDbCommand cm = new OleDbCommand(sql, conn);
 68        object obj = cm.ExecuteScalar();
 69        cm.Dispose();
 70        return obj;
 71    }
                                                //执行返回单个数据的SQL语句
 72    public OleDbDataReader ExecuteReader(string sql)
 73    {
 74        if (conn.State != ConnectionState.Open) conn.Open();
 75        OleDbCommand cm = new OleDbCommand(sql, conn);
 76        OleDbDataReader dr = cm.ExecuteReader();
 77        cm.Dispose();
 78        return dr;
 79    }
                                        //执行返回DataReader的SQL语句
 80    public DataSet ExecuteDataSet(string sql)
 81    {
 82        if (conn.State != ConnectionState.Open) conn.Open();
 83        OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
 84        DataSet ds = new DataSet();
 85        da.Fill(ds);
 86        da.Dispose();
 87        if (ds.Tables[0].Rows.Count == 0)
 88            ds.Tables[0].NewRow();
 89        return ds;
 90    }
                                            //执行返回DataSet的SQL语句
 91    public void LoadListItem(DropDownList ddl, string sql)
 92    {
 93        ddl.DataSource = ExecuteDataSet(sql);
 94        ddl.DataBind();
 95    }
                                //读取选择列表数据项
 96    public void LoadListItem(DropDownList ddl, string sql, int selectedindex)
 97    {
 98        LoadListItem(ddl, sql);
 99        ddl.SelectedIndex = selectedindex;
100    }
            //读取选择列表数据项并设置默认选择项
101    public void LoadListItem(RadioButtonList rbl, string sql)
102    {
103        rbl.DataSource = ExecuteDataSet(sql);
104        rbl.DataBind();
105    }
                            //读取单选列表数据项
106    public void LoadListItem(RadioButtonList rbl, string sql, int selectedindex)
107    {
108        LoadListItem(rbl, sql);
109        rbl.SelectedIndex = selectedindex;
110    }
            //读取单选列表数据项并设置默认选择项
111    public void LoadListItem(GridView gv, string sql)
112    {
113        gv.DataSource = ExecuteDataSet(sql);
114        gv.DataBind();
115        gv.Visible = true;
116    }
                                    //绑定数据到GridView
117    public string BMBsql(int typeid)
118    {
119        return "SELECT * FROM pub_dictitem WHERE n_typeid=" + typeid + " ORDER BY s_itemid";
120    }
                                                        //查询编码表数据的SQL语句
121    public int Cint(string str)
122    {
123        if (str==nullreturn 0;
124        int number;
125        try
126        {
127            number = int.Parse(str);
128        }

129        catch
130        {
131            return 0;
132        }

133        return number;
134    }
                                                            //转换字符串为数字
135    public int Cint(char c)
136    {
137        return Cint(c.ToString());
138    }
                                                                //转换字符为数字
139    public int Cint(object obj)
140    {
141        return Cint(obj.ToString());
142    }
                                                            //转换对象为数字
143    public long Clng(string str)
144    {
145        if (str == nullreturn 0;
146        long number;
147        try
148        {
149            number = long.Parse(str);
150        }

151        catch
152        {
153            return 0;
154        }

155        return number;
156    }
                                                            //转换字符为长整型数字
157    public long Clng(char c)
158    {
159        return Clng(c.ToString());
160    }
                                                                //转换字符为长整型数字
161    public long Clng(object obj)
162    {
163        return Clng(obj.ToString());
164    }
                                                            //转换对象为长整型数字
165}

166

你可能感兴趣的:(asp.net)