|
hi every body.I am coding with c# .net core and my first app is a multilayer database project.
this app get datat from user input control and add/del/edit on it.
I have made 3 project in my solution:
1 : Datalayer 2:Business Layer 3:View
in my forms I should Join three or four or more table with each other and show in a datagridview to the user.so I have coded all sql query in datalayer with ado.net bcoz it is easy to join table instead of EF.
I put my code here and you please see it and notice me about each layers.(I think in BLL layer I just didn't do anything special)
and In the UI I put all of my code that I think it is not very interesting.
so see it :
DAL :
public static DataTable GetAll()
{
using DataTable dt = new();
try
{
string query = "
select
l.LineID,
s.SheetID,
z.ZoneID,
j.JointTypeID,
j.JointClassID ,
j.PipeClassID ,
j.HoldID,
l.[LineNo] ,
s.Sheet,
s.Rev ,
s.RevIndex ,
z.Unit,
z.Area,
s.SheetStatus as LineStatus,
j.JointID,
j.JointNo ,
j.JointIndex ,
j.JointStatus ,
cast(CAST(j.JointSize as decimal(18,2)) as float) as JointSize,
j.[Location],
j.Spool,
jt.JointType,
jc.JointClassName as Class,
pc.PipeClass,
m.MaterialName as Mat,
j.Thk
from
Joint as j
inner join Sheet as s on s.SheetID = j.SheetID and s.VoidDate is null
inner join Line as l on l.LineID=s.LineID
left join Zone as z on z.ZoneID = j.ZoneID
left join JointType as jt on jt.JointTypeID = j.JointTypeID
left join JointClass as jc on jc.JointClassID = j.JointClassID
left join PipingClass as pc on pc.PipeClassID = j.PipeClassID
left join Material as m on m.MaterialID = pc.MaterialID
left join MTO as m1 on m1.SheetID = j.SheetID AND m1.PTNo = Mat1
left join MTO as m2 on m2.SheetID = j.SheetID AND m2.PTNo = Mat2
";
using SqlCommand cmd = new SqlCommand(query, Connection.Conn);
Connection.Open();
using SqlDataReader rd = cmd.ExecuteReader();
dt.Load(rd);
}
catch (SqlException ex)
{
Error = ex.Message;
}
finally
{
Connection.Close();
}
return dt;
}
BLL:
public static DataTable GetAll()
{
return JointDAL.GetAll();
}
UI:
DG_Joint.DataSource = JointBLL.GetAll();
I put code in dgv contentclick() for show selected rows into textbox above it.
(There is another way like bindingsource for do it but it use only that table but my query joined many tables and it doesn't work.
I don't know if I wrote it explicitly or not, but what I mean by creating this topic is to review the multi-layer program, what codes should be written in each layer.
|
|
|
|
|
There are quite a few problems with the code you've shown.
Firstly, your code won't compile. You can't have line-breaks in a standard string. You would need to use either a verbatim string[^] or a raw string[^] for your query.
You should make the query a local const so that you're not tempted to try to inject parameter values into it incorrectly and introduce a SQL Injection[^] vulnerability into your code.
You seem to be using a shared database connection instance. That's a terrible idea - either your code must be restricted to only service one request at a time, or you'll end up with cross-contamination when multiple users try to access your application, since the connection is not thread-safe. Instead, create the connection when you need it, and wrap it in a using block to ensure it's disposed of properly.
Similarly, the SqlCommand and SqlDataReader instances need to be wrapped in using blocks.
Your code currently swallows any exceptions, and returns an empty DataTable instead. The caller is expected to examine the Error property to determine whether an error occurred, and retrieve a tiny portion of the details of that error - assuming the property hasn't been overwritten by a call from a different user in the meantime. Instead, you should let the exception propagate to the caller naturally. If you need to add more details, then throw a different exception, making sure to include the original exception as the InnerException .
public static DataTable GetAll()
{
const string Query = """
SELECT
...
FROM
...
""";
try
{
using (var connection = Connection.CreateAndOpenConnection())
using (var command = new SqlCommand(connection, Query))
using (var reader = command.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(reader);
return dt;
}
}
catch (SqlException ex)
{
throw new YourCustomException("There was an error retrieving the list of all things. See the inner exception for details.", ex);
}
}
Once you've dealt with those issues, then you can start working out what you need to put in the other layers. For example, having a BLL which simply passes all calls through to the DAL and returns the results unchanged doesn't make a lot of sense.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have all my data stored in DataTables, when I run my program using multi-threading I get an error message. I don't remember the exact wording but, it said index out of bounds of the array. However it does not appear to be out of bounds. When I run the program without using the multi-threading everything is fine.
Also, most of my methods are static, I lock the datatables when loading data.
I can't find where the problem exist, because where I believe the program line failed it does not show it as being out of bounds.
Any help would be greatly appreciated, thanks in advance.
Michael
|
|
|
|
|
If you're deleting in one thread while retrieving in another, you can get an out of bounds if you don't protect against it.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Without seeing the code it's impossible for anyone to tell you what's going on.
|
|
|
|
|
My program inserts thousands of records into a SQLite database. That works fine.
I do it by building an INSERT statement with 1000 VALUE groups, each group representing a different record to add. If I were to execute a separate INSERT statement for each record, then the insert would take hours, so I make the compound INSERT statement.
So the question is, how can I transform the INSERT query into a parameterized query, if every VALUE group has unique values?
There are four fields that need to be populated for each record, so that means I must create a SQLiteCommand with 4000 parameters?
Or is there a better way?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I don't think you have much of a choice. Sqlite doesn't support any bulk insert so you're either going to reuse the same command over and over again or build a block of inserts.
You also have a command length limit of 1GB but, unless you go nuts building a huge block of insert statements, you'll not likely hit that.
The other problem is a limit on the number of parameters you can have. The default using numbered named parameters is 32766 for recent versions of Sqlite.
Quote: Maximum Number Of Host Parameters In A Single SQL Statement
A host parameter is a place-holder in an SQL statement that is filled in using one of the sqlite3_bind_XXXX() interfaces. Many SQL programmers are familiar with using a question mark ("?") as a host parameter. SQLite also supports named host parameters prefaced by ":", "$", or "@" and numbered host parameters of the form "?123".
Each host parameter in an SQLite statement is assigned a number. The numbers normally begin with 1 and increase by one with each new parameter. However, when the "?123" form is used, the host parameter number is the number that follows the question mark.
SQLite allocates space to hold all host parameters between 1 and the largest host parameter number used. Hence, an SQL statement that contains a host parameter like ?1000000000 would require gigabytes of storage. This could easily overwhelm the resources of the host machine. To prevent excessive memory allocations, the maximum value of a host parameter number is SQLITE_MAX_VARIABLE_NUMBER, which defaults to 999 for SQLite versions prior to 3.32.0 (2020-05-22) or 32766 for SQLite versions after 3.32.0.
The maximum host parameter number can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_VARIABLE_NUMBER,size) interface.
|
|
|
|
|
Thanks, just wanted to be sure I wasn't overlooking something.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
SQLite isn't open to SQL Injection, because it doesn't support multiple commands in the same command*: it doesn't support bulk inserts, even via a DataAdapter. it is meant to be a "lite" version, after all!
So if you build your command with multiple VALUE fields, it is technically safe (but needs to be commented in case someone later converts to MSSql / MySql)
I'd recommend using a transaction though, just to speed the operation up if nothing else...
This may help: https://stackoverflow.com/questions/1711631/improve-insert-per-second-performance-of-sqlite[^]
* Unless you use the CommandText property instead of a SqlLiteCommand - then you are wide open to SQL Injection.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: * Unless you use the CommandText property instead of a SqlLiteCommand - then you are wide open to SQL Injection. Uh oh, that's what I'm using. I'm setting the CommandText property of a SQLiteCommand to the long INSERT statement.
I wasn't aware that there's any different way. Is there?
EDIT: I misread your post. What is the other way of using the CommandText property instead of a SQLiteCommand? (Just curious)
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
No, the CommandText is a property of the SqLiteCommand object: SqliteCommand.CommandText Property (Microsoft.Data.Sqlite) | Microsoft Learn[^]
SqLite is just that: a "lite" version of SQL - it isn't meant for big jobs.
I'd be tempted to say that you might be better off using SQL Server if you are working with large databases - but that's your decision and I don't know enough about your system(s) to reccomend it.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Anyway, I appreciate you pointing out that it's not susceptible to injection attacks. That was the reason for my question.
Thanks!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Did you actually time the separate inserts? "Thousands" isn't very much and the server is "local". The usual consideration is don't do "mass inserts" with an index ... drop and build the index afterwards.
I've experience seconds, but never "hours".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I might have used hyperbole, but the database will contain upwards of 4 million rows.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Using Marc Jacobi's VST.NET2, I'm able to get the effect name and vendor for a VST2 DLL file, but an error is thrown, if I try it on a VST3 file. There must be an easy way to do this. I had read somewhere that VST3 files are basically just DLLs, but it appears there's a diff for what i'm trying to do. I've been searching and testing for days. Not a whole lot of C# examples. Seems C++ is used nearly exclusively.
|
|
|
|
|
Why not ask in the forums on his articles.
|
|
|
|
|
|
Hi,
I want to become a freelancer programmer able to take jobs posted
on freelancing sites or be part of a team where I can do at the begining small tasks for gaining experience.
For that I have started to learn C++ and then WINAPI and when I was asking here questions about my WINAPI isues the experienced guys here told me that I am learnig an old tool and a very nice member here that helped me a lot recommended me .NET field with the book of Charles Petzold - .NET book zero, that was an excelent book because it dosen't entered in details.
Now I have finished this book to, what tool you recommend me to learn next, what steps should I take
that help me gaining experience and to be able to start working on real life
project? I think that now would help me books or tutorials that are treating topics on surface.
I want to mention that also I had installed, connected and tested a local database trough c++ and some years ago I installed an local apache server and played with html, php.
Guide me please on what tools I have to learn next to be able to enter on the market, now I am working on another technical field and I want to gradually sustain myself from programming.
Thank you în advance for your help.
|
|
|
|
|
If you have finished the book - and done all the exercises - then you are ready for a bigger, more in-depth book: Petzold is excellent, but only an introduction! Addison Wesley, Wrox, and MS Press all do excellent books on C#, but they tend to average around the 1000 page mark rather than 250 odd of Petzold!
Experience is the key: practice, practice, and then practice some more - C# is a pretty small language (you can learn the whole of it in an afternoon if you have solid C++ OOPs experience) ... but the .NET framework is enormous, and comes in several flavours now, as do the presentation framework part of it (WinForms, WPF, Blazor, Xamarin all target differently and what works in one doesn't work in the other!)
So decide what framework you will specialise in to start with, and get a book on that - then practice, practice, practice! You can always add a second presentation framework later when you have solid experience under your belt.
Good luck!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
To support what Griff has already said, the thing you need to do most in order to enter the market as a professional developer is to create working applications. Find projects that pique your interest. There are plenty of articles here on CP that represent working applications.
Look at the CP home page. Set your filter to C#, and then browse the articles that it shows. Good luck!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thank you for your responses.
Blazor seems intresting because it is used for frontend and for backend development, this thing looks atractive I will take a look on it.
Thank you for your time.
|
|
|
|
|
coco243 wrote: Blazor seems intresting because it is used for frontend
I suggest javascript rather than that. There is more market for javascript.
|
|
|
|
|
imho, you need to make decisions about:
1) type of app(s): windows, mac, linux ... or, app that runs on all those, or, only runs in browsers.
2) if you choose the .NET framework, language choice gets easier.
3) front-end, and/or back-end ? MS facility or non-MS backend tools ?
i suggest you start by using WinForms ... get familiar with interacting with controls using events ... understand inheritance (classes, subclasses), and using interfaces. study use of abstract and virtual.
There excellent books on learning C# WinForms.
imho, what you learn will help you move to another .NET language and/or .NET framework.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
When I finished to read the C# introduction book thats were the questions în my head, to start with UI windows programming or web developing?
The ideea is that I've spended a lot of time with C++ and WINAPI without
succeding entering in the freelanging programming market and now I wish to get the way that leads me to be able to work and make an income while I am adding more and more knowledge, I want to learn tools that help me not those that pushes me away from my goal.
Thank you
|
|
|
|
|
coco243 wrote: I want to learn tools that help me not those that pushes me away from my goal. i've given you my best advice ... how long are you going to sit by the river, thirsty, thinking about what's the best water ?
Make choices, and get to work. Research skills most in use now, that companies are hiring entry-level positions for.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|