Professional Documents
Culture Documents
A. Database Name: Studentdb B. Table Name: Student C. Fields or Column Names
A. Database Name: Studentdb B. Table Name: Student C. Fields or Column Names
2. Create Database.
A. Database Name: studentdb
B. Table Name: student
C. Fields or Column Names:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand(query, sqlconn);
int res = cmd.ExecuteNonQuery();
if (res >= 1)
return true;
}
}
finally
{
this.CloseConnection();
}
return false;
}
// Getting data from database and creating a collection of it using List Class.
public List<StudentModel> GetData()
{
try
{
List<StudentModel> list = new List<StudentModel>();
string query = "select *from student;";
if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand(query, sqlconn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(new StudentModel()
{
ID = int.Parse(reader["id"].ToString()),
Name = reader["name"].ToString(),
Department = reader["department"].ToString(),
DOB = reader["dob"].ToString()
});
}
this.CloseConnection();
return list;
}
return null;
}
catch { return null; }
}
// Check if Record exists or not
public bool CheckRecord(int id)
{
try
{
if (this.OpenConnection() == null)
{
SqlCommand cmd = new SqlCommand("select count(*) from student where id = @id", sqlconn);
cmd.Parameters.AddWithValue("@id", id);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
return true;
}
}
finally
{
this.CloseConnection();
}
return false;
}