A modern C# ASP.NET Core web application for querying and managing MS SQL Server databases. This application provides both a web interface and REST API for database operations.
- Modern Web UI: Clean, responsive Bootstrap-based interface
- REST API: Complete CRUD operations with Swagger documentation
- Advanced Querying: Search, filter, and statistical analysis
- Entity Framework Core: Code-first database approach
- Real-time Statistics: Database analytics and reporting
- Responsive Design: Works on desktop and mobile devices
- ASP.NET Core 8.0
- Entity Framework Core
- MS SQL Server / LocalDB
- Bootstrap 5.3
- Font Awesome Icons
- JavaScript (ES6+)
- Swagger/OpenAPI
SqlQueryApp/
├── Controllers/
│ ├── ProductsController.cs # API controller for CRUD operations
│ └── HomeController.cs # MVC controller for web UI
├── Data/
│ └── ApplicationDbContext.cs # Entity Framework DbContext
├── Models/
│ └── Product.cs # Product entity model
├── Views/
│ ├── Shared/
│ │ └── _Layout.cshtml # Main layout template
│ └── Home/
│ ├── Index.cshtml # Product listing page
│ ├── Create.cshtml # Add new product form
│ └── Statistics.cshtml # Database statistics
├── SqlQueryApp.csproj # Project file with dependencies
├── Program.cs # Application startup and configuration
└── appsettings.json # Configuration including connection string
- .NET 8.0 SDK or later
- SQL Server or SQL Server Express LocalDB
- Visual Studio 2022, VS Code, or any .NET-compatible IDE
git clone <repository-url>
cd SqlQueryAppUpdate the connection string in appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=SqlQueryAppDb;Trusted_Connection=true;MultipleActiveResultSets=true"
}
}For SQL Server, use:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=SqlQueryAppDb;Integrated Security=true;TrustServerCertificate=true"
}
}dotnet restoredotnet runThe application will start and be available at:
- Web Interface: https://localhost:5001 or http://localhost:5000
- API Documentation: https://localhost:5001/swagger
The application uses Entity Framework Code-First approach. The database and tables will be created automatically when you first run the application.
The application includes seed data with sample products:
- Laptop ($1,299.99)
- Smartphone ($899.99)
- Wireless Headphones ($299.99)
If you need to manually manage the database:
# Create migration
dotnet ef migrations add InitialCreate
# Update database
dotnet ef database update
# Drop database (if needed)
dotnet ef database drop| Method | Endpoint | Description |
|---|---|---|
| GET | /api/products |
Get all products with optional filtering |
| GET | /api/products/{id} |
Get specific product by ID |
| POST | /api/products |
Create new product |
| PUT | /api/products/{id} |
Update existing product |
| DELETE | /api/products/{id} |
Delete product |
| GET | /api/products/search-by-price-range |
Search products by price range |
| GET | /api/products/low-stock |
Get low stock products |
| GET | /api/products/statistics |
Get database statistics |
search: Search in name and descriptionminPrice: Minimum price filtermaxPrice: Maximum price filterisActive: Filter by active statusthreshold: Stock threshold for low-stock query
# Get all products
curl https://localhost:5001/api/products
# Search products
curl "https://localhost:5001/api/products?search=laptop&minPrice=1000"
# Get low stock products
curl "https://localhost:5001/api/products/low-stock?threshold=10"
# Create new product
curl -X POST https://localhost:5001/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Gaming Mouse",
"description": "High-precision gaming mouse",
"price": 79.99,
"stock": 30,
"isActive": true
}'- List View: Searchable and filterable product table
- Create: Add new products with validation
- Edit: Update existing products
- Delete: Remove products with confirmation
- Details: View complete product information
- Search & Filter: Real-time search with multiple criteria
- Statistics Dashboard: Database analytics and metrics
- Low Stock Alerts: Identify products needing restocking
- Price Range Queries: Find products within specific price ranges
- Total number of products
- Total inventory value
- Average product price
- Minimum and maximum prices
- Total stock items
- Custom query results
- Create model in
Models/folder - Add DbSet to
ApplicationDbContext - Create controller for CRUD operations
- Add views for web interface
- Run migration commands
The application uses Bootstrap 5.3 with custom CSS. Modify:
Views/Shared/_Layout.cshtmlfor overall layout- Individual view files for specific pages
- Add custom CSS in the layout file
Visit /swagger when running the application to see interactive API documentation powered by Swagger/OpenAPI.
-
Database Connection Errors
- Verify SQL Server is running
- Check connection string format
- Ensure database permissions
-
Migration Errors
- Delete
Migrations/folder and recreate - Check Entity Framework tools installation
- Delete
-
Port Conflicts
- Modify
launchSettings.jsonto use different ports - Check firewall settings
- Modify
To start with a fresh database:
dotnet ef database drop --force
dotnet runFor production deployment, consider:
- Use secure connection strings with proper authentication
- Implement proper authorization and authentication
- Add input validation and sanitization
- Use HTTPS certificates
- Configure CORS policies appropriately
- Add rate limiting and logging
- Add database indexes for frequently queried columns
- Implement pagination for large datasets
- Use async/await patterns consistently
- Consider caching for read-heavy operations
- Monitor database query performance
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License. See LICENSE file for details.
For questions or issues:
- Check the troubleshooting section
- Review API documentation at
/swagger - Create an issue in the repository