I am working on my first architectural project in C# and I came across a problem that I can't solve and can't find the answer to on the internet (probably because I do not even know what to google). So I thought I should try and find some help from someone who is familiar with what I am trying to do.
In my DAL I have the following code:
using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
namespace DAL
{
public class ClsDataLayer
{
SqlConnection SqlConn = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=3tireexample;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
public List<String> Testlijst()
{
string commandText_Testlijst = "Select * from Usermst";
List<String> Gebruikers = new List<string>();
using (SqlConn)
{
SqlConn.Open();
using (SqlCommand cmd = new SqlCommand(commandText_Testlijst, SqlConn))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Gebruikers.Add(reader.GetString(0));
}
}
}
}
return Gebruikers;
;
}
public bool InsertDataBool(string _name, string _city, string _email)
{
string commandText = "Insert into Usermst values (@name, @city, @email)";
bool test = false;
using (SqlConnection connection = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=3tireexample;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.Parameters.Add("@name", SqlDbType.NVarChar);
command.Parameters["@name"].Value = _name;
command.Parameters.Add("@city", SqlDbType.NVarChar);
command.Parameters["@city"].Value = _city;
command.Parameters.Add("@email", SqlDbType.NVarChar);
command.Parameters["@email"].Value = _email;
command.ExecuteNonQuery();
test = true;
}
return test;
}
}
}
I am trying to pass the info that I have received from my List to my business layer. I was reading some other StackOverflow posts and I saw that somebody said to use a class instance for this problem, but the problem is that I have no clue how. How do I transfer the data from my public List(that is in my DAL) to my UserData.cs(That is the name of my class instance)? My question really is how should my UserData.cs file look like (code-wise) I am new to all of this so it would be nice if you could explain what you do and why really simple.
Thank you very much in advance!
We say this as concept of DTO (Data Transfer Objects).
One can create another namespace for DTO with DTO classes.
In your particular instance dto would like
Add reference of DTO in both Business Layer and DAL.
Call DAL from business layer as below
You will have to update DAL layer to return DTO.
Benefit of using DTO is that you can send multiple values instead of just one value which you are currently sending