❔ Connecting a MySQL server to a C# project
Recently ive had to learn MySQL and how it works but i cannot find out how to connect the actual MySQL server to a C# project in Visual Studio
57 Replies
ive installed the connector and all of that
i have it all installed on my pc, i just dont know how to actually connect it
Usually you use your database lib and connect to a db using connection string, that is it
Dunno which lib you personally use but for things like SQLite or Npgsql (postgres) you do something like
This is obviously a pseudocode
Whatever it is that you use to connect to the database... read the documentation of that
Did you install MySQLConnector or official Oracle Connector?
Because official Oracle Connector is problematic and should be omitted
ye ive installed all of it
its just the trouble of actually connecting the database or server idk how exactly its called to the actual project i have
ill get it done
Add reference
To your domain project
And also check for the same version
Also create an App.config file
To declare your ConnectionString
Don't use that
Why?
It's Oracles driver which is very problematic
Hmm, which then?
MySqlConnector 2.2.5
A truly async MySQL ADO.NET provider, supporting MySQL Server, MariaDB, Percona Server, Amazon Aurora, Azure Database for MySQL and more.
Thet is community made driver written from scratch
Does it connect to the Workbench?/
Compared to MySql.Data (i.e., Oracle’s MySQL Connector/NET), MySqlConnector has the following benefits:
Asynchronous I/O support
Higher performance (see benchmarks) and lower memory usage
More reliable (see bugs fixed)
Leading edge: first MySQL driver to support .NET Core, DbBatch, DateOnly, DbDataSource and other new .NET features
Better compatibility with non-MySQL databases (MariaDB, Amazon Aurora, etc.)
Better license (MIT, not GPL 2.0 with Universal FOSS Exception or commercial license)
Faster pace of development (regular NuGet releases, not once per quarter)
Development happens in the open on GitHub
To database, yes
But it's not available via workbench, but via nuget
So I can test my Persistence with the MySqlConnector?
yes
I ll give a shot
@Karix quick question, should the ConnectionString be declared through App.config?
got this err
Is that your code?
Yes
Don't handle exceptions like that
You're throwing away useful information
should I throw exc?
No
Just remove the catch entirely
In any case, what's the exception being thrown?
That's what I don't understand
Where is the mistake while making the connection
Show your query
It's connecting just fine
It fired off a query, for back a result, but you asked for a column that wasn't in the result
So that's my DB
Should I send an export without data or script
?
No
I don't want to see your database
I want to see the query your code is running
Show the $code surrounding this
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/You're reading from device in that code
But from building in your screenshot
Omgg
Also, you should ditch that catch block
The throw is replacing the exception's stack trace
A proper rethrow is just
throw;
I will try.
so the problem is
my db
Does the device table have a building column?
No
I foudn
Problem is
Why is it jumping into the devicemapper
since I am asking for the buildings
If you're asking for buildings
Then why are you assigning to a Device?
And device is in aggregation with the place/building
devicemapper?
Yes
And that is?
Class
Am I going to have to pull teeth?
Provide details
So
You're asking for a building column
But assigning to a Device instance
Yet you said devices don't have a building column
True
I have to recheck my code
Since the mappers were badly made
Now I see what my error were. Im glad you helped me!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.Any help on why the ConnectionString is still empty and its not getting the info from the constructor? Also added a watch to the ConnectionString and to the Buildings List.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.