I am trying to make an app that stores some text, so I would like to use database connection (File I/O is discouraged)? And, How do i learn that and where to learn from? I don't know how to start, what to start and all, I have made GUI and want to make it working!
Asked
Active
Viewed 480 times
-2
-
1I'd do it from C#. – Eljay Jun 15 '21 at 12:49
-
I use VS to develop Windows apps. I program in C# and in C++ (and other languages). If you are using [CLR](https://en.wikipedia.org/wiki/Common_Language_Runtime), to me that means you probably ought to be using C# (or F#). Your best place to start would be with a good introductory and/or tutorial book. If you choose F#, I recommend [The Book of F#](https://nostarch.com/fsharp). If you choose C#, I don't have a particular book recommendation — probably something from Microsoft Press or O'Reilly. – Eljay Jun 15 '21 at 15:52
-
@Darpan, is any update? Please check if my answer works for you. – Jack J Jun Jun 18 '21 at 02:25
1 Answers
0
You could refer to the following code to insert some information from textbox to database.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ connstr = "connstr";
SqlConnection^ connection = gcnew SqlConnection(connstr);
connection->Open();
String^ sql = "Insert into Employee(ID,Name,Address)values(@ID,@Name,@Address)";
SqlCommand^ command = gcnew SqlCommand(sql, connection);
command->Parameters->AddWithValue("@ID", txtID->Text);
command->Parameters->AddWithValue("@Name",txtName->Text);
command->Parameters->AddWithValue("@Address", txtAddress->Text);
command->ExecuteNonQuery();
connection->Close();
MessageBox::Show("success");
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
dataGridView1->DataSource = 0;
String^ connstr = "connstr";
SqlConnection^ connection = gcnew SqlConnection(connstr);
connection->Open();
String^ sql = "Select * from Employee";
//SqlCommand^ command = gcnew SqlCommand(sql, connection);
SqlDataAdapter^ adapter = gcnew SqlDataAdapter(sql, connection);
DataTable^ table = gcnew DataTable();
adapter->Fill(table);
dataGridView1->DataSource = table;
connection->Close();
}
Note:Button1_click is used to insert data to database. Button2_click is used to show data from database in the datagridview. Please use \\
to replace \
in the connectionstring.
Result:

Jack J Jun
- 5,633
- 1
- 9
- 27
-
Thanks, Jack! Your answer was useful, but I couldn't vote the answer. Sorry for that! – Darpan Jun 18 '21 at 07:47
-
@Darpan, is there any other question about my answer? If not, you can click '✔' to mark your reply as an answer. It will also help others to solve the similar issue. – Jack J Jun Jun 18 '21 at 07:53
-
Jack, nothing more, but I am **new** to this, so sorry to ask you one more question How do I specify the source of the database, or how will my program locate it?? Could you please help me in this? – Darpan Jun 18 '21 at 09:46
-
@Darpan, you could refer to the link to know how to get connection string from database. [How to get the connection String from a database](https://stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database) – Jack J Jun Jun 18 '21 at 09:52