SQL and Visual Studio Retrieving a Table
Hi - I'm trying to select all the "emails" in a table called "users" with c# in visual studio. Currently I have this command:
I'm not sure how to get the data from this though, would it be doing smth like: ?
10 Replies
Something like SqlReader reader = cmd.ExecuteReader();
Then iterate over the reader.
There is a popular package called Dapper that has functionality similar ( but not identical ) to what you have written
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader
https://www.nuget.org/packages/Dapper/
Thank you !
Here is a very simple example of using DataReader:
https://github.com/Treit/MiscBenchmarks/blob/main/SqlDataSetVsDataReader/Benchmark.cs
thanks both of you I think I've got datareader working now - however, and probably more importantly, whenever I try to open a SqlConnection I get an error I'm not sure what's going wrong here as you can tell Im not experienced with SQL at all and have just followed a basic tutorial to setup a small database alongside my c# code, the file path seems to be correct..?
Can you share your connection string without passwords?
cn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\username\OneDrive\Documents\Computing NEA Project Current(DB EDIT)\Computing NEA Project Current\Computing NEA Project Current\Computing NEA Project Current\Client Chat App\Client Chat App\Database1.mdf;Integrated Security=True";
the file path is really stupid because its a folder in tons of folders because of extracting/sending to zip all the time
Try and move it to a simpler park outside of one drive; one drive can cause a lot of issues with locking of files
I didn't know you could just attach to an mdf file without actually using a server
Oh I assumed it was installed
Though tbh I’m not a localdb expert
Thank you moving it off one drive worked - I have one more and hopefully final issue:
I'm trying to add username password and email to my users table
And then get the ID of this new user, however I get this error: