question

BernardBailey-0080 avatar image
0 Votes"
BernardBailey-0080 asked ·

.NET 5 Database Connection. How do you connect using .NET5?

I have looked everywhere to find a way to connect a database in a .NET5 Windows Form Application. I can do it using a Windows Frameworks WIndows Forms Application but I am trying to figure out how to do this with the .NET5. Is there any way to do this or is a feature that is being worked on. I am learning C# right now and would like to be able to use .Net5 to build application. Thanks, Bernard A. Bailey, Jr.

dotnet-csharp
· 8
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

To give a little more clarification on what I am looking for is how to create a data source that can be bound to a control such as a ListBox, textbox, or label. I need to find a way to select the data source since the Conventional way of selecting a data source does not work for .Net projects.
Thanks,

0 Votes 0 ·

Un sure what you mean with

find a way to select the data source since the Conventional way of selecting a data source does not work for .Net projects.

But I've added the following to the code I posted in my first reply.

  • Data Binding for one TextBox

  • A BindingSource (well worth exploring benefits)

  • Column mapping

Please provide more details if the revised code does not fit your needs as this below, also in the revised code is conventional data binding.

 ProductNameTextBox.DataBindings.Add("Text", _bindingSource, "ProductName");




1 Vote 1 ·

Karen,
Thank you for your help. Everything you showed me worked. I am learning C# right now. This has helped a great deal
I feel this needs to be posted on the MSDN site for people looking for binding and data source information for .NET5. Everything
worked as it should. I need to play around with some more controls but I tried a label and it worked.

Thanks,

Bernard A. Bailey, Jr.

0 Votes 0 ·
Show more comments

Hi @BernardBailey-0080,
>>I need to find a way to select the data source since the Conventional way of selecting a data source does not work for .Net projects.
You can use the DataTable as DataSource, and I have modified my code example that filling data into the listbox control you can refer to.
If it is not what you want, please explain in detail.
Best Regards,
Daniel Zhang

0 Votes 0 ·

I know exactly what you're talking about, except I'm using Visual Basic (WinForms) and trying to connect to MS Access local databases under .NET 5.

I asked the same question in here just a few minutes ago before coming across yours. I'm not sure the folks at Microsoft are even aware we can't connect to a database vis the Data Sources wizard under .NET Core 5 like we could under the .NET Framework. I have been learning Object Pascal very seriously, lately. After all these shortcomings I'm coming across in .NET 5, I'm strongly considering dumping all of my Microsoft development products and porting all of my apps to OP.


78045-untitled.png

0 Votes 0 ·
untitled.png (25.4 KiB)

To give a little more clarification on what I am looking for is how to create a data source that can be bound to a control such as a ListBox, textbox, or label. I need to find a way to select the data source since the Conventional way of selecting a data source does not work for .Net projects.
Thanks,

0 Votes 0 ·

@BernardBailey-0080

I finally got mine to work (partially). After a bit of fiddling around on my own because I still couldn't find an answer in here or in Google, I figured out I had to install 'System.Data.OleDb' via the ridiculous NuGet manager. I still can't create a connection through the data source wizard like we can for .NET Framework apps, but at least now my project recognizes the System.Data.OleDb.dll library, and now I can successfully code in complete access to my database - no drag & drop though.

Microsoft works as hard as it can these days to make people hate them. And they work hard and making Visual Studio more and more difficult rather than user-friendly and intuitive like it used to be. Ridiculous!!

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

Hello,

See my code sample on GitHub which should set you up for interacting with SQL-Server. The interface has just enough to show it works so some columns for identifiers are showing which is actually good for learning purposes.

76142-f1.png



f1.png (23.9 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered ·

Hi BernardBailey-0080,
When you create a new project, you need to choose Windows Forms App(.NET).
76094-310.png
Then right click you project name in solution explore->Properties->In the Application page, change your target framework to .NET 5.0.
76121-3101.png
And I made a test via sql server, it worked fine.
Here is a code example you can refer to.

 using System.Data.SqlClient;
 private void button1_Click(object sender, EventArgs e)
 { 
     SqlConnection cnn;
     string connStr ="ConnectString"
     cnn = new SqlConnection(connStr);
     cnn.Open();
     DataSet ds = new DataSet();
     SqlDataAdapter adapter = new SqlDataAdapter("SELECT Name from test", cnn);
     adapter.Fill(ds);
    this.listBox1.DataSource = ds.Tables[0];
    this.listBox1.DisplayMember = "Name";
 }

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



310.png (15.8 KiB)
3101.png (11.8 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered ·

ADO.NET is ADO.NET. It basically makes no difference what version of .NET you use, if you know the basic fundlementals of how to use ADO.NET.

https://www.c-sharpcorner.com/UploadFile/18fc30/understanding-the-basics-of-ado-net/

https://dotnettutorials.net/lesson/ado-net-using-sql-server/

· 2 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The problem with this assertion is that most every example or tutorial out there demonstrates connection to the database via the data source wizard, which is disabled or simply unavailable under .NET 5 as in my posted screen shot above.

0 Votes 0 ·
DuaneArnold-0443 avatar image DuaneArnold-0443 BrandonStewart-4522 ·

Well you should learn how use ADO,NET manually by writing the same code Mr. Wizard did for you doing it yourself.
There are articles, tutorials and books that cover how to use ADO.NET effectively doing coding with no Mr. Wizard, which can be applied to any version of .NET being used. ADO.NET is ADO.NET no matter what.

0 Votes 0 ·