C
C#12mo ago
Firesteelix

I'm having an issue adding stuff to a database and im not really sure how to fix it

No description
27 Replies
Firesteelix
FiresteelixOP12mo ago
i keep getting this error... ive never done anything with databases before. this is my first time. Help will be very much apreciated. Im also quite new to C#
Omnissiah
Omnissiah12mo ago
could paste the method here? $code
MODiX
MODiX12mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Angius
Angius12mo ago
ConnectionStrings.com - Forgot that connection string? Get it here!
All connection strings in one place. Find the syntax for your database connection using ADO.NET, ADO, ODBC, OLEDB, C#, VB, VB.NET, ASP.NET and more.
Angius
Angius12mo ago
Check if your connection string is valid
Omnissiah
Omnissiah12mo ago
in fact i wanted to see the ending of it
Firesteelix
FiresteelixOP12mo ago
void SaveEmployeePhoneToDatabase(string employeePhone)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\My files\\Computer Scineces\\Coding\\C#\\project\\AnnsData.accdb;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();

string sqlInsertPhone = "INSERT INTO Employee (Phone) VALUES (@Phone)";
using (OleDbCommand command = new OleDbCommand(sqlInsertPhone, connection))
{
command.Parameters.AddWithValue("@Phone", employeePhone);
command.ExecuteNonQuery();
}

connection.Close();
}
}
void SaveEmployeePhoneToDatabase(string employeePhone)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\My files\\Computer Scineces\\Coding\\C#\\project\\AnnsData.accdb;";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();

string sqlInsertPhone = "INSERT INTO Employee (Phone) VALUES (@Phone)";
using (OleDbCommand command = new OleDbCommand(sqlInsertPhone, connection))
{
command.Parameters.AddWithValue("@Phone", employeePhone);
command.ExecuteNonQuery();
}

connection.Close();
}
}
there you go ive asked chat gpt about the error im getting and it says i need to install the drivers. but ive installeld the drivers. ive even tried reinstalling them. and on top of that its asked me to go through HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Wow6432Node\Microsoft\Office\16.0\Common\InstalledPackages\AccessDatabaseEngine_X64 and i dont get the last 2 parts to it when i follow the path
Angius
Angius12mo ago
MS Access is shit of a database, but not so shit to require you to fiddle with the registry Ah SqlConnection is used with SQL Server Not with Access You want... something else, dunno what. Something from the OleDB family I'd bet?
Firesteelix
FiresteelixOP12mo ago
ive changed that earlier tho to OleDbConnection but the error im getting is System.InvalidOperationException: 'The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.'
Angius
Angius12mo ago
That 12.0 will change depending on the version of MS Access you have installed
Firesteelix
FiresteelixOP12mo ago
yh i didnt think that i would've needed to do that but i cant follow the path all the way since the last two bits are missing i installed the one from the MS 365 thing
Angius
Angius12mo ago
Microsoft Access 2007 = ACE 12; Microsoft Access 2010 = ACE 14; Microsoft Access 2013 = ACE 15
is what I found on Stack So, dunno, try incrementing the version one by one until it maybe works lmao
Firesteelix
FiresteelixOP12mo ago
so i need to download the 2007 version? or just change the ACE part not sure which one i need tho
Angius
Angius12mo ago
Change the ACE
Firesteelix
FiresteelixOP12mo ago
how would i do that? and what to?
Angius
Angius12mo ago
You would edit your code Where it says 12 you would put 13
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\My files\\Computer Scineces\\Coding\\C#\\project\\AnnsData.accdb;";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\My files\\Computer Scineces\\Coding\\C#\\project\\AnnsData.accdb;";
would become
string connectionString = "Provider=Microsoft.ACE.OLEDB.13.0;Data Source=C:\\My files\\Computer Scineces\\Coding\\C#\\project\\AnnsData.accdb;";
string connectionString = "Provider=Microsoft.ACE.OLEDB.13.0;Data Source=C:\\My files\\Computer Scineces\\Coding\\C#\\project\\AnnsData.accdb;";
What to? Idk Try incrementing it by one until, dunno, 25 or so and see if any of those works Or try to look up what version is needed for your version of Access Or start using a real database instead of Access KEKW
Firesteelix
FiresteelixOP12mo ago
yh ill see if i can find that i cant... i have to use access for my project sadly
Angius
Angius12mo ago
My condolences
Firesteelix
FiresteelixOP12mo ago
hahaha what databases should i use in the future tho?
Angius
Angius12mo ago
Postgres Or SQLite if you want something small and file-based
Firesteelix
FiresteelixOP12mo ago
No description
Firesteelix
FiresteelixOP12mo ago
this is what my version is i believe but tha tcant be because there are multiple versions betweeen those years
Firesteelix
FiresteelixOP12mo ago
No description
Firesteelix
FiresteelixOP12mo ago
that might be better?
Angius
Angius12mo ago
Maybe? Never worked with Access, so it'll take trial and error
Firesteelix
FiresteelixOP12mo ago
nws dont mind how long it takes to figure out just as long as i can get it sorted
Omnissiah
Omnissiah12mo ago
there should be a command to list (in an easy way) the installed drivers, for not shooting in the dark too much
Want results from more Discord servers?
Add your server