代码:(谢谢!)
namespace yjs.DataAccess
{
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using yjs.Common.Data;
using yjs.Common;
using yjs.SystemFramework;
/// <summary>
/// Mark 的摘要说明。
/// </summary>
public class Mark: IDisposable
{
// Sql Command for Insert
private SqlDataAdapter dsCommand;
public Mark()
{
// Create the adapter
dsCommand = new SqlDataAdapter();
// Create the DataSetCommand SelectCommand
dsCommand.SelectCommand = new SqlCommand();
dsCommand.SelectCommand.Connection = new SqlConnection(Configuration.ConnectionString);
//
// Establish the mapping rules from the database columns to the dataset
//
// NOTE: Having the mappings allows the adapter to create the table and its
// columns directly in the dataset.
//
dsCommand.TableMappings.Add("Table",MarkData.CJ_TABLE);// TODO: 在此处添加构造函数逻辑
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true); // as a service to those who might inherit from us
}
protected virtual void Dispose(bool disposing)
{
if (! disposing)
return; // we're being collected, so let the GC take care of this object
if (insertCommand != null)
{
if (insertCommand.Connection != null)
insertCommand.Connection.Dispose();
insertCommand.Dispose();
insertCommand = null;
}
}
public MarkData GetMarkByStudentId(String studentId)
{
return FillMarkData("GetMarkByStudentId", "@XH", studentId);
}
public MarkData GetMarkByLesson(String lesson)
{
return FillMarkData("GetMarkByLesson", "@KCMC", lesson);
}
public MarkData GetMarkByYear(String year)
{
return FillMarkData("GetMarkByYear", "@niandu", year);
}
public MarkData GetMarkByType(String type)
{
return FillMarkData("GetMarkByType", "@stu_type", type);
}
private MarkData FillMarkData(String commandText, String paramName, String paramValue)
{
if (dsCommand == null )
{
throw new System.ObjectDisposedException( GetType().FullName );
}
MarkData data = new MarkData();
SqlCommand command = dsCommand.SelectCommand;
command.CommandText = commandText;
command.CommandType = CommandType.StoredProcedure; // use stored proc for perf
SqlParameter param = new SqlParameter(paramName, SqlDbType.NVarChar, 255);
param.Value = paramValue;
command.Parameters.Add(param);