CNSA-212-Personal/Database3/StudentDataTier.cs

100 lines
3.3 KiB
C#
Raw Normal View History

2024-02-13 14:30:26 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
2024-02-13 15:46:12 -05:00
namespace Database3
2024-02-13 14:30:26 -05:00
{
public class StudentDataTier
{
static String connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
static SqlConnection myConn = new SqlConnection(connString);
static System.Data.SqlClient.SqlCommand cmdString = new System.Data.SqlClient.SqlCommand();
2024-02-13 16:59:13 -05:00
public DataSet GetStudents(string studid, string lname, DateTime dob)
2024-02-13 14:30:26 -05:00
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
2024-02-13 15:46:12 -05:00
cmdString.CommandText = "SearchStudent";
2024-02-13 14:30:26 -05:00
// Define input parameter
cmdString.Parameters.Add("@studentid", SqlDbType.VarChar, 6).Value = studid;
2024-02-13 15:46:12 -05:00
cmdString.Parameters.Add("@lname", SqlDbType.VarChar, 25).Value = lname;
cmdString.Parameters.Add("@dob", SqlDbType.Date).Value = dob;
2024-02-13 14:30:26 -05:00
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
public DataSet GetStudents(string StuID)
{
try
{
// open connection
myConn.Open();
//clear any parameters
cmdString.Parameters.Clear();
// command
cmdString.Connection = myConn;
cmdString.CommandType = CommandType.StoredProcedure;
cmdString.CommandTimeout = 1500;
cmdString.CommandText = "GetByStudentIDS";
// Define input parameter
cmdString.Parameters.Add("@studentid", SqlDbType.VarChar, 6).Value = StuID;
// adapter and dataset
SqlDataAdapter aAdapter = new SqlDataAdapter();
aAdapter.SelectCommand = cmdString;
DataSet aDataSet = new DataSet();
2024-02-13 14:30:26 -05:00
// fill adapter
aAdapter.Fill(aDataSet);
// return dataSet
return aDataSet;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
finally
{
myConn.Close();
}
}
2024-02-13 14:30:26 -05:00
}
}