Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying to create a program in VS2022. I spent all day yesterday trying to get a recordset of data from a MS SQL server.

Have you got any idea? At All? The documentation is AWFUL! The advice from SO is what I have been using successfully in VS2015 and VS2017.

It does not work in VS2019.

What I have tried:

MS documentation, Stack Overflow.

I must be missing something....
Posted
Updated 21-Mar-22 5:00am
Comments
Maciej Los 21-Mar-22 9:50am    
Richard MacCutchan 21-Mar-22 10:44am    
What have you tried and what results do you get? We cannot guess what your code is doing.

1 solution

Here's my toolbox boilerplate code:
C#
string strConnect = SMDBSupport.SMInstanceStorage.GetInstanceConnectionString("MyDBName");
string sql = "SELECT Id, Desc FROM MyTable";
using (SqlConnection con = new SqlConnection(strConnect))
    {
    try
        {
        con.Open();
        using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
            {
            using (DataTable dt = new DataTable())
                {
                da.Fill(dt);
                myDataGridView.DataSource = dt;
                }
            }
        }
    catch (Exception ex)
        {
        Debug.WriteLine(ex.ToString());
        }

The connection string comes from a "common source" datafile: Instance Storage - A Simple Way to Share Configuration Data among Applications[^] - that may be overkill for you, so this may also help: Simple SQL Connection String Creation[^]
 
Share this answer
 
Comments
Slow Eddie 21-Mar-22 11:29am    
Thank you, Griff. I believe this will work particularly after looking at the link to your article.

BTW: My wife, a major lover of England, and all things English is planning a trip as soon as Covid subsides. I would love to come meet you in Wales and thank you personally, not just for me but all of the others you help on this forum. - Ed
OriginalGriff 21-Mar-22 11:59am    
I'm currently just hoping that Covid does come to an end sometime ... :sigh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900