То, что я написал в другом ответе, в картинках. Здесь используется приложение VB, но это не имеет значения, потому что шаги такие же, и вы на самом деле не собираетесь писать больше одной строки кода
Right click project, add new dataset
Right click surface, add tableadapter
Add a connection string, Next. Name it, Next
Add a query that selects all from your table, WHERE (id column) = @id
Rename the methods to add "ByID". It will be obvious why later. Finish:
Right click table adapter, Add Query
Proceed through choosing "Select that returns rows" and entering a query that seeks users by last name:
Give good names to the new methods. Finish. Save:
Go to the forms designer, make sure Data Sources tool panel is showing (View Menu)
Drag the Users grid node from Data Sources onto the form
It has pre-made a textbox for the ID, because that's the first query in the tableadapter. We'll change it to lastname. Click the button, change its name, change the text it shows. Always make sure your control names are up to date and relevant.
You can see I changed the label too, and I also changed the name of the textbox (you can't see it) and I changed the name of everything in the tray under the designer, so it starts with underscore:
I do this because VB is case insensitive and calling variable the same name as their type is a bad idea in any language, and makes intellisense confusing in VB. You don't have to add the leading underscores in C#. It's enough to discriminate on case alone, though arguably not always wise:
Now we need to change the code. Double click the FillBy button. It goes to code. Maybe you have some code already, maybe not. Make sure the code fills the table using the relevant input. This is the only part of the process that really requires you to think about what you're doing and what your variables are called (they may be different to mine)
The code probably defaulted to saying
_usersTableAdapter.FillByID(_myDataSet.Users, new Guid(_lastNameToolStripTextBox.Text));
Because it used to be set up for you to type an id (guid or int, my db has a guid) in that box but we have changed it for lastname. So we need to change the FillByID (and now you see why we give them sensible names, not FillBy1 and FillBy2) so it's FillByLastName, and we need to change the code so we pass a string lastname, not a guid ID
_usersTableAdapter.FillByLastName(_myDataSet.Users, _lastNameToolStripTextBox.Text);
That's the only code you have to write. Remember I named my things on the form using leading underscores, so my code has leading underscores. If you dont rename your things, your code won't have leading underscores
Now run the app:
введите описание изображения здесь
Посмотрите на всех этих Джонов Смитов! Это, конечно, разные пользователи - у каждого свой ID. Вы даже можете написать здесь новые детали и нажать «Сохранить», чтобы обновить базу данных ..
Из одной строчки кода! :)