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;
}
}
}