关闭

如何用C#创建一个类并在其他Web页中调用该类的方法(1)

2452人阅读 评论(0) 收藏 举报
  一.   如何创建类,调用类中的方法.如下在SFCWEB项目中增加了Stone.cs.
using System.Collections ;using System.Data;
using System.Data.OleDb;using System.Configuration;//这个是连接Access数据库所需要的命名空间
using System.Web.UI.HtmlControls;using System.ComponentModel;
using System.Web;using System.Web.SessionState;
using System.Web.UI;using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Drawing;
 
namespace SFCWEB.bin
{
     ///<summary>
     /// Summary description for Stone.
     ///</summary>
     public class Stone
     {
         public Stone()//类的构造函数
         {
         }
         //该函数的作用是根据项目名返回该项目的oracle的链接字符,如果有sql的也可以
         public string neworacleconn(string project)
         {
              string a="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c://inetpub//wwwroot//SFCWEB//StationTable.aspx";
              string strCon="null";
              try
              {
                   OleDbConnection myconn= new OleDbConnection(a);    myconn.Open ();                  string sql1="select * from ProjectTable where Model='"+project+"' order by id";
                   OleDbCommand myCommand = new OleDbCommand(sql1,myconn);
                   OleDbDataReader oqlr=nulloqlr= myCommand.ExecuteReader();
                   if(oqlr.Read())
                   {//oracle数据库的连接字符                      strCon="SERVER=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST="+oqlr["Ip"].ToString ()+")(PORT=1521)))(CONNECT_DATA=(SID="+oqlr["Sid"].ToString ()+")(SERVER=DEDICATED)));uid="+oqlr["Uid"].ToString ()+";pwd="+oqlr["Pwd"].ToString ();
                        oqlr.Close();
                   }
                   myconn.Close();    myconn.Dispose(); return strCon;
              }
              catch(OracleException)
              {
                   return strCon;
              }
     }
//该函数返回通过sql语句查找出的个数select count(*) as total from Score; .
public int GetCountRecordNew(string sql,string project,OracleConnection conn,OracleCommand cmd)
{
     try
     {
         int icount=0; string strcount;   OracleDataReader sqlr=null;
          cmd.CommandText=sql;   sqlr=cmd.ExecuteReader();   sqlr.Read();       strcount=sqlr["total"].ToString();//与sql语句中的total对应
         icount=Int32.Parse(strcount);
         return icount;
     }
     catch(OracleException)
     {    return 0; }
}
}
}
0
0

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