❔ How do I connect a SQL table to a datagridview in Winforms?
I'm a beginner to programming and want to have a table where I can add multiple entries, which are stored within a SQL table. I have watched multiple tutorials trying to understand how to do it but most of them go over my head.
3 Replies
First you'd need
using System.Data.SqlClient;
If you've created a new user in logins in db, make sure you enable the login from windows and sql authentication checked
If you can login into db using the new user then fine
After that just create an SQL connection using
Sqlconnection conn = new Sqlconnection (@"Data source= server instance name; Initial catalog= db name; user id= username; password= password");
If you want to run a query
String query = your query;
conn.open();
Sqlcommand cmd = new sqlcomman(query, conn);
cmd.executenonquery();
Conn.close()
Mind you executenonquery is used if you want to insert and don't need to return the val
im trying to connect to a table in a database, do I enter the name of the database or the table?
I'm also not trying to do a login, I'm trying to make a list of items which a randomiser will pick
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.