用C 用CCI和MACD两种指标配合
创始人
2024-04-23 12:40:45
0

下面是用C#写的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.SqlClient;  
  11. namespace Mysqlserver  
  12. {  
  13.     /// < summary>  
  14.     /// SqlServerDataBase 的摘要说明  
  15.     /// < /summary>  
  16.     public class SqlServerDataBase  
  17.     {  
  18.         private string strError = null;  
  19.         private int intCount = 0;  
  20.         public SqlServerDataBase()  
  21.         {  
  22.             //  
  23.             // TODO: 在此处添加构造函数逻辑  
  24.             //  
  25.         }  
  26.         /// < summary>  
  27.         /// 公开方法DBConn,返回数据库连接  
  28.         /// < /summary>  
  29.         /// < returns>< /returns>  
  30.         public SqlConnection DBconn()  
  31.         {  
  32.             string strConn = "Server=(local);Database=GlobalMeetings;Uid=sa;pwd=";  
  33.             try 
  34.  
  35.            {  
  36.                 return new SqlConnection(strConn);  
  37.             }  
  38.             catch (Exception)  
  39.  
  40.             {  
  41.                 return null;  
  42.             }  
  43.         }  
  44.         /// < summary>  
  45.         /// 公开属性ErrorMessage,返回错误信息  
  46.         /// < /summary>  
  47.         public string ErrorMessage  
  48.         {  
  49.             get 
  50.             {  
  51.                 return strError;  
  52.             }  
  53.         }  
  54.  
  55.         /// < summary>  
  56.         /// 根据查询语句从数据库检索数据  
  57.         /// < /summary>  
  58.         /// < param name="strSelect">查询语句< /param>  
  59.  
  60.  
  61.         /// < param name="SqlConn">数据库连接< /param>  
  62.         /// < returns>有数据则返回DataSet对象,否则返回null< /returns>  
  63.         public DataSet Select(string SelectString, SqlConnection sqlConn)  
  64.         {  
  65.             strError = "";  
  66.             SqlConnection conn;  
  67.             if (sqlConn == null)  
  68.             {  
  69.  
  70.                 conn = DBconn();  
  71.             }  
  72.             else 
  73.             {  
  74.                 conn = sqlConn;  
  75.             }  
  76.             try 
  77.             {  
  78.                 //若数据库连接的当前状态是关闭的,则打开连接  
  79.                 if (conn.State == ConnectionState.Closed)  
  80.                 {  
  81.  
  82.                    conn.Open();  
  83.                 }  
  84.                 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();  
  85.                 SqlCommand selectCommand = new SqlCommand(SelectString, conn);  
  86.                 selectCommand.CommandType = CommandType.Text;  
  87.                 mySqlDataAdapter.SelectCommand = selectCommand;  
  88.                 DataSet myDS = new DataSet();  
  89.                 mySqlDataAdapter.Fill(myDS);  
  90.  
  91.                 return myDS;  
  92.             }  
  93.             catch (Exception e)  
  94.             {  
  95.                 strError = "数据检索失败:" + e.Message;  
  96.                 return null;  
  97.             }  
  98.  
  99.             finally 
  100.             {  
  101.                 if (conn.State != ConnectionState.Closed)  
  102.                 {  
  103.                     conn.Close();  
  104.                 }  
  105.             }  
  106.         }  
  107.         /// < summary>  
  108.         /// 更新数据库  
  109.         /// < /summary>  
  110.         /// < param name="UpdateString">Update Sql语句< /param>  
  111.         /// < param name="SqlConn">数据库连接< /param>  
  112.         /// < returns>更新成功返回true< /returns>  
  113.         public bool Update(string UpdateString, SqlConnection SqlConn)  
  114.  
  115.         {  
  116.             return udiDataBase(UpdateString, SqlConn);  
  117.         }  
  118.         /// < summary>  
  119.         /// 从数据库中删除数据  
  120.  
  121.        /// < /summary>  
  122.         /// < param name="DeleteString">Delete Sql语句< /param>  
  123.         /// < param name="SqlConn">数据库连接< /param>  
  124.         /// < returns>删除成功返回true< /returns>  
  125.         public bool Delete(string DeleteString, SqlConnection SqlConn)  
  126.         {  
  127.             return udiDataBase(DeleteString, SqlConn);  
  128.         }  
  129.         /// < summary>  
  130.         /// 把数据插入数据库  
  131.         /// < /summary>  
  132.         /// < param name="InsertString">Insert Sql语句< /param>  
  133.         /// < param name="SqlConn">数据库连接< /param>  
  134.         /// < returns>插入成功返回true< /returns>  
  135.         public bool Insert(string InsertString, SqlConnection SqlConn)  
  136.         {  
  137.             return udiDataBase(InsertString, SqlConn);  
  138.         }  
  139.         /// < summary>  
  140.         /// 根据Sql语句更新数据库  
  141.         /// < /summary>  
  142.  
  143.         /// < param name="UDIString">更新语句< /param>  
  144.         /// < param name="SqlConn">数据库连接< /param>  
  145.         /// < returns>更新成功则返回true< /returns>  
  146.         public bool udiDataBase(string UDIString, SqlConnection SqlConn)  
  147.         {  
  148.             strError = "";  
  149.             SqlConnection conn;  
  150.             if (SqlConn == null)  
  151.             {  
  152.                 conn = DBconn();  
  153.             }  
  154.             else 
  155.             {  
  156.                 conn = SqlConn;  
  157.             }  
  158.             try 
  159.             {  
  160.                 if (conn.State == ConnectionState.Closed)  
  161.                 {  
  162.  
  163.                    conn.Open();  
  164.                 }  
  165.                 SqlCommand cmd = new SqlCommand(UDIString, conn);  
  166.  
  167.                 cmd.CommandType = CommandType.Text;  
  168.                 intCount = cmd.ExecuteNonQuery();  
  169.                 return !(intCount <  1);  
  170.             }  
  171.             catch (Exception e)  
  172.             {  
  173.                 strError = "更新数据库失败:" + e.Message;  
  174.                 return false;  
  175.             }  
  176.             finally 
  177.             {  
  178.                 if (conn.State != ConnectionState.Closed)  
  179.                 {  
  180.  
  181.                    conn.Close();  
  182.                 }  
  183.             }  
  184.         }  
  185.     }  
  186. }  

-----------------------------

ASP.NET数据库操作类写好了,下面是两种调用方法

1、

  1. string strUserPsw = UserPsw.Text.Trim();  
  2. string UserPassWord = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPsw, "MD5");//md5加密  
  3.  
  4. SqlServerDataBase obj = new SqlServerDataBase();  
  5. obj.Insert("insert into asUserInfo (UserName,UserPassword,Question,Answer,CreateTime) values('" + UserName.Text.Trim() + "','" + UserPassword + "','" + Question.Text.Trim() + "','" + Answer.Text.Trim() + "','" + DateTime.Now.ToString() + "' )"null);  

2、

  1. private bool IsUsernameExist(string strUsername)  
  2. {  
  3.     bool bRet = true;  
  4.     SqlServerDataBase db = new SqlServerDataBase();  
  5.  
  6.     DataSet ds = db.Select("select * from asUserInfo where UserName = '" + strUsername + "'"null);  
  7.     if (ds == null ds.Tables.Count == 0 ds.Tables[0].Rows.Count == 0)  
  8.     {  
  9.         bRet = false;  
  10.     }  
  11.     else 
  12.     {  
  13.         bRet = true;  
  14.     }  
  15.  
  16.     return bRet;  
  17. }  

以上就介绍了用C#写的ASP.NET数据库类及调用方法。

【编辑推荐】

  1. ASP.NET数据库图片存储到Sql2000中
  2. ASP.NET自定义错误处理页面的添加
  3. ASP.NET中的session存储模式运用
  4. ASP.NET中的文件上传下载方法集合
  5. ASP.NET中的cookie读写方法介绍

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...