using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ASP.NETDemo
{
class SQLHelper
{
private static string connString = "Server=.;DataBase=StudentManageDB;Uid=sa;Pwd=sasa";
public static object GetSingleResult(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
object result = cmd.ExecuteScalar();
conn.Close();
return result;
}
public static int Update(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();
return result;
}
public static SqlDataReader GetReader(string sql)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
}
static void Main(string[] args)
{
string sql = "select Count(*) from Students";
object result = SQLHelper.GetSingleResult(sql);
Console.WriteLine(result);
Console.ReadLine();
}
static void Main(string[] args)
{
string sql = "update Students set StudentName='刘晶晶' where StudentId=100005";
int result = SQLHelper.Update(sql);
Console.WriteLine(result);
Console.ReadLine();
}
static void Main(string[] args)
{
string sql = "select StudentName from Students";
SqlDataReader objReader = SQLHelper.GetReader(sql);
while (objReader.Read())
{
Console.WriteLine(objReader["StudentName"]);
}
objReader.Close();
Console.ReadLine();
}
|