Cannot connect to sql connection string

Raymond21775

New member
Joined
Jul 2, 2025
Messages
1
Programming Experience
3-5
I'm unable to connect to BikeShop.DAL files sql connection string. See attachment. I am able to connect to accounting web app, but I need to be able to connect to BikeShop.DAL. A sample of the code is shown in customeradapter.txt file. I am running my project in visual studio 2022, to open a webpage. Any help in resolving this issue would be greatly appreciated.
 

Attachments

  • lesson8 screenshot of solution explorer.png
    lesson8 screenshot of solution explorer.png
    40.8 KB · Views: 3
  • customeradapter.txt
    customeradapter.txt
    2.5 KB · Views: 1
Welcome to the forum. In the future, you should post your code in code blocks to make it easier for people to try to help you. Making them go through the extra step of downloading a separate help could easily dissuade someone and they'll just move on to the next easier post to read.

When you say that you are "unable to connect to the BikeShop DAL", what do you mean exactly? Are you getting an error or exception? If so what is the error?

Anyway, here's your code from the file attachment:
C#:
Expand Collapse Copy
//using BikeShop1.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Dapper;


public class CustomerAdapter : ICustomerAdapter
{
    private string connectionString = @"Data Source=C:\Sqlite\BikeShop.db";
    public IEnumerable<Customer> GetAll()
    {
        string sql = "SELECT CustomerId, FirstName, LastName FROM Customer";
        using (SqliteConnection connection = new SqliteConnection(connectionString))
        {
            return connection.Query<Customer>(sql);
        }
    }
    public Customer GetById(int id)
    {
        string sql = @"SELECT CustomerId, FirstName, LastName FROM Customer WHERE CustomerId = @CustomerId";
        using (SqliteConnection connection = new SqliteConnection(connectionString))
        {
            return connection.QueryFirst<Customer>(sql, new { CustomerId = id });
        }
    }
    public bool InsertCustomer(Customer customer)
    {
        string sql = "INSERT INTO Customer (FirstName, LastName) VALUES (@FirstName, @LastName)";
        using (SqliteConnection connection = new SqliteConnection(connectionString))
        {
            int rowsAffected = connection.Execute(sql, customer);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
    public bool UpdateCustomer(Customer customer)
    {
        string sql = @"UPDATE Customer SET FirstName = @FirstName, 
            LastName = @LastName WHERE CustomerId = @CustomerId";
        using (SqliteConnection connection = new SqliteConnection(connectionString))
        {
            int rowsAffected = connection.Execute(sql, customer);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    public bool DeleteCustomerById(int id)
    {
        string sql = "DELETE FROM Customer WHERE CustomerId = @CustomerId";
        using (SqliteConnection connection = new SqliteConnection(connectionString))
        {
            int rowsAffected = connection.Execute(sql, new { CustomerId = id });
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
 
Back
Top Bottom