0% found this document useful (0 votes)
9 views10 pages

Biblioteca

The document outlines a C# application for library management that includes user login, book management, and user management functionalities. It establishes a SQLite database with tables for Users, Books, and Loans, and provides methods for user authentication and data manipulation. The application features forms for managing books and users, allowing for operations such as adding, searching, and displaying records.

Uploaded by

alcada300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Biblioteca

The document outlines a C# application for library management that includes user login, book management, and user management functionalities. It establishes a SQLite database with tables for Users, Books, and Loans, and provides methods for user authentication and data manipulation. The application features forms for managing books and users, allowing for operations such as adding, searching, and displaying records.

Uploaded by

alcada300
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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

You might also like