C
C#2y ago
Hiccup

❔ Method WriteToTable(); isn't doing anything to the database file

My code:
private void button1_Click(object sender, EventArgs e)
{
WriteToTable();
}

public void WriteToTable()
{
string DBpath = @"Data Source=.\StudentInfoDB.db;Version=3;";
using SQLiteConnection SQLConnection = new SQLiteConnection(DBpath);
using var cmd = new SQLiteCommand(DBpath, SQLConnection);
SQLConnection.Open();
cmd.CommandText = "INSERT INTO Students (Name) VALUES ('Test')";
cmd.ExecuteNonQuery();
SQLConnection.Close();
}
private void button1_Click(object sender, EventArgs e)
{
WriteToTable();
}

public void WriteToTable()
{
string DBpath = @"Data Source=.\StudentInfoDB.db;Version=3;";
using SQLiteConnection SQLConnection = new SQLiteConnection(DBpath);
using var cmd = new SQLiteCommand(DBpath, SQLConnection);
SQLConnection.Open();
cmd.CommandText = "INSERT INTO Students (Name) VALUES ('Test')";
cmd.ExecuteNonQuery();
SQLConnection.Close();
}
In my database file, I have one table called Students and 3 columns called name, grade and phone number. I'm trying to ensure I correctly connected to the database so I decided I should try adding text under the Name column before I add anymore code. I expected this code to output "Text" under the "Name" column to my database file, however after I press button1 nothing happens. No errors show up and no crashes occur. I have tried debugging the code and searching online for common mistakes but sadly it did not help
5 Replies
hiyosilver
hiyosilver2y ago
I'm not massively experienced with SQLite, but I'm not entirely sure passing DBpath into the SQLiteCommand constructor is right.
hiyosilver
hiyosilver2y ago
Mmh, well I guess you are setting it afterwards. I'm not sure that won't mess it up though.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server