Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, am a newbie in C# and i have been try somethings out. the one that i want to do next is
if i have a form with a textbox where a user will enter an SQL command then the data is displayed in a datagrid. any suggestions will be appreciated
Posted

Hi, since you are new to C#, You can use the below code ,what you are asking is similar to SQL Query analyser where in SQL editor you type SQL commands/statements and run to get results

Put a textbox and datagridview on windows form with button for action, and on button click event write below code, put the database connection appropriately

private void button1_Click(object sender, EventArgs e)
     {
         string DBCommand = textBox1.Text;
         SqlDataAdapter sqlDa = new SqlDataAdapter(DBCommand, "Data Source=localhost;Initial Catalog=Northwind;User ID=sa;Password=sa");
         sqlDa.Fill(ds, "Tables");
         dataGridView1.DataSource = ds.Tables[0];
     }
 
Share this answer
 
v3
a textbox where a user will enter an SQL command

You should NEVER do such kind of design. Google for SQL Injection.

As for retrieving the data, read about ADO.NET here.[^]
 
Share this answer
 
v2

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