ASP.NET 数据库操作类---DataSet类

using System;
using System.Data;
using System.Data.SqlClient;

namespace Demo3
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   Demo dm = new Demo();
   dm.DoDemo();
   //
  }
 }
 class Demo
 {
  public void DoDemo()
  {
   DataSet ds = new DataSet();
   ExecuteOptions oExecute = new ExecuteOptions();
   ds = oExecute.ExecuteandFill();
   //Display all sales transactions on the console
   
   //An action query doesnt generate any results for processing
   oExecute.ExecuteNonQuery();

   //Use the reader to walkthrough the results in a forward-only manner
   oExecute.ExecuteReader();
   
   //Return the total number of sale transactions in the database
   int nSum=oExecute.ExecuteScalar();
   Console.WriteLine("Record count is " +nSum.ToString());
   
  }
 }
 class ExecuteOptions
 {
  public SqlDataReader ExecuteReader()
  {
   SqlConnection con = new SqlConnection("Server=localhost; Integrated Security=SSPI; database=mydatabase; Max Pool Size=75; Min Pool Size=5");
   SqlCommand cmdTitle = new SqlCommand("select Username,password,score from scoretable",con);
   cmdTitle.CommandType=CommandType.Text;

   SqlDataReader dr;
   con.Open();
   dr =cmdTitle.ExecuteReader(CommandBehavior.CloseConnection);
   return dr;
  }
  public int ExecuteScalar()
  {
   SqlConnection con = new SqlConnection("Server=localhost; Integrated Security=SSPI; database=mydatabase;Max Pool Size=75; Min Pool Size=5");
   SqlCommand cmdTitleCount = new SqlCommand("select count(*) from scoretable",con);
   cmdTitleCount.CommandType=CommandType.Text;
   con.Open();
   return Convert.ToInt32(cmdTitleCount.ExecuteScalar().ToString());
  }
  public void ExecuteNonQuery()
  {
   SqlConnection con = new SqlConnection("Server=localhost; Integrated Security=SSPI; database=mydatabase;Max Pool Size=75; Min Pool Size=5");
   SqlCommand cmdUpdateSales = new SqlCommand("Update scoretable set score = score+200 where username='成龙'",con);
   cmdUpdateSales.CommandType=CommandType.Text;
   con.Open();
   cmdUpdateSales.ExecuteNonQuery();
  }
  public DataSet ExecuteandFill()
  {
   SqlConnection con = new SqlConnection("Server=localhost; Integrated Security=SSPI; database=mydatabase;Max Pool Size=75; Min Pool Size=5");
   SqlDataAdapter da = new SqlDataAdapter("select username, password,score from scoretable",con);
   DataSet ds = new DataSet();
   con.Open();
   da.Fill(ds);
   return ds;
  }
 }


}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值