We can fetch SQL query result set data in RichTextBox on the row by row basis.
Now suppose there is a table "DB_Employee" having columns EmpID, EmpName, EmpDepartment, EmpAddress in database "TestDB". So we have to fetch available rows on a button1 click event into RichTextBox1.
- private void bitton1_click(object sender, EventArgs e) {
-
- try {
-
- SqlConnection con = new SqlConnection("server=localhost; database=TestDB; integrated security=sspi;");
-
- SqlCommand cmd = new SqlCommand("Select * From DB_Test", con);
-
- con.Open();
-
- using(SqlDataReader dr = cmd1.ExecuteReader()) {
-
- While(dr.Read()) {
-
- RichTextBox1(dr.AppendText(dr.GetValue(0).ToString()));
- RichTextBox1(dr.AppendText(dr.GetValue(1).ToString()));
- RichTextBox1(dr.AppendText(dr.GetValue(2).ToString()));
- RichTextBox1(dr.AppendText(dr.GetValue(3).ToString()));
-
-
- RichTextBox1 += system.Environment.NewLine;
- }
- }
- } catch (Exception ex) {
- MessageBox.Show(ex.Message);
- } finally {
- con.Close();
- }
- }