using System;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace LibraryManagement
public partial class LoginForm : Form
public LoginForm()
InitializeComponent();
Database.InitializeDatabase();
private void btnLogin_Click(object sender, EventArgs e)
using (var connection = Database.GetConnection())
connection.Open();
string query = "SELECT * FROM Users WHERE Username = @Username AND Password
= @Password";
using (var command = new SQLiteCommand(query, connection))
command.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
command.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
using (var reader = command.ExecuteReader())
{
if (reader.HasRows)
MessageBox.Show("Login successful!", "Success", MessageBoxButtons.OK,
MessageBoxIcon.Information);
MainForm mainForm = new MainForm();
mainForm.Show();
this.Hide();
else
MessageBox.Show("Invalid username or password.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
public static class Database
private const string DatabaseFile = "library.db";
private const string ConnectionString = "Data Source=" + DatabaseFile + ";Version=3;";
public static SQLiteConnection GetConnection()
return new SQLiteConnection(ConnectionString);
public static void InitializeDatabase()
{
if (!System.IO.File.Exists(DatabaseFile))
SQLiteConnection.CreateFile(DatabaseFile);
using (var connection = GetConnection())
connection.Open();
string createUsersTable = @"CREATE TABLE IF NOT EXISTS Users (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Username TEXT NOT NULL UNIQUE,
Password TEXT NOT NULL
);";
string createBooksTable = @"CREATE TABLE IF NOT EXISTS Books (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Title TEXT NOT NULL,
Author TEXT NOT NULL,
ISBN TEXT NOT NULL UNIQUE,
Copies INTEGER NOT NULL,
Image BLOB
);";
string createLoansTable = @"CREATE TABLE IF NOT EXISTS Loans (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
UserId INTEGER NOT NULL,
BookId INTEGER NOT NULL,
LoanDate TEXT NOT NULL,
ReturnDate TEXT,
DocumentImage BLOB,
FOREIGN KEY (UserId) REFERENCES Users(Id),
FOREIGN KEY (BookId) REFERENCES Books(Id)
);";
using (var command = new SQLiteCommand(createUsersTable, connection))
command.ExecuteNonQuery();
using (var command = new SQLiteCommand(createBooksTable, connection))
command.ExecuteNonQuery();
using (var command = new SQLiteCommand(createLoansTable, connection))
command.ExecuteNonQuery();
public partial class MainForm : Form
public MainForm()
InitializeComponent();
private void btnManageBooks_Click(object sender, EventArgs e)
{
ManageBooksForm booksForm = new ManageBooksForm();
booksForm.ShowDialog();
private void btnManageUsers_Click(object sender, EventArgs e)
ManageUsersForm usersForm = new ManageUsersForm();
usersForm.ShowDialog();
private void btnManageLoans_Click(object sender, EventArgs e)
ManageLoansForm loansForm = new ManageLoansForm();
loansForm.ShowDialog();
public partial class ManageBooksForm : Form
public ManageBooksForm()
InitializeComponent();
LoadBooks();
private void LoadBooks()
using (var connection = Database.GetConnection())
connection.Open();
string query = "SELECT * FROM Books";
using (var command = new SQLiteCommand(query, connection))
using (var adapter = new SQLiteDataAdapter(command))
DataTable booksTable = new DataTable();
adapter.Fill(booksTable);
dataGridViewBooks.DataSource = booksTable;
private void btnSearchBook_Click(object sender, EventArgs e)
using (var connection = Database.GetConnection())
connection.Open();
string query = "SELECT * FROM Books WHERE Title LIKE @Search OR Author LIKE
@Search OR ISBN LIKE @Search";
using (var command = new SQLiteCommand(query, connection))
command.Parameters.AddWithValue("@Search", "%" + txtSearchBook.Text.Trim()
+ "%");
using (var adapter = new SQLiteDataAdapter(command))
DataTable booksTable = new DataTable();
adapter.Fill(booksTable);
dataGridViewBooks.DataSource = booksTable;
}
}
private void btnAddBook_Click(object sender, EventArgs e)
using (var connection = Database.GetConnection())
connection.Open();
string query = "INSERT INTO Books (Title, Author, ISBN, Copies, Image) VALUES
(@Title, @Author, @ISBN, @Copies, @Image)";
using (var command = new SQLiteCommand(query, connection))
command.Parameters.AddWithValue("@Title", txtTitle.Text.Trim());
command.Parameters.AddWithValue("@Author", txtAuthor.Text.Trim());
command.Parameters.AddWithValue("@ISBN", txtISBN.Text.Trim());
command.Parameters.AddWithValue("@Copies", (int)numCopies.Value);
if (pictureBoxBook.Image != null)
using (var ms = new MemoryStream())
pictureBoxBook.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
command.Parameters.AddWithValue("@Image", ms.ToArray());
else
command.Parameters.AddWithValue("@Image", DBNull.Value);
}
command.ExecuteNonQuery();
MessageBox.Show("Book added successfully!", "Success", MessageBoxButtons.OK,
MessageBoxIcon.Information);
LoadBooks();
public partial class ManageUsersForm : Form
public ManageUsersForm()
InitializeComponent();
LoadUsers();
private void LoadUsers()
using (var connection = Database.GetConnection())
connection.Open();
string query = "SELECT * FROM Users";
using (var command = new SQLiteCommand(query, connection))
using (var adapter = new SQLiteDataAdapter(command))
DataTable usersTable = new DataTable();
adapter.Fill(usersTable);
dataGridViewUsers.DataSource = usersTable;
private void btnSearchUser_Click(object sender, EventArgs e)
using (var connection = Database.GetConnection())
connection.Open();
string query = "SELECT * FROM Users WHERE Username LIKE @Search";
using (var command = new SQLiteCommand(query, connection))
command.Parameters.AddWithValue("@Search", "%" + txtSearchUser.Text.Trim() +
"%");
using (var adapter = new SQLiteDataAdapter(command))
DataTable usersTable = new DataTable();
adapter.Fill(usersTable);
dataGridViewUsers.DataSource = usersTable;
private void btnAddUser_Click(object sender, EventArgs e)
using (var connection = Database.GetConnection())
{
connection.Open();
string query = "INSERT INTO Users (Username, Password) VALUES (@Username,
@Password)";
using (var command = new SQLiteCommand(query, connection))
command.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
command.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
command.ExecuteNonQuery();
MessageBox.Show("User added successfully!", "Success", MessageBoxButtons.OK,
MessageBox