且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用从文本框生成的sql命令在数据网格中显示行

更新时间:2023-12-03 13:11:46

用户将在其中输入SQL命令的文本框

您永远都不要做这种设计. Google for SQL注入.

关于检索数据,请在此处了解有关ADO.NET的信息. [ ^ ]

您好,由于您是C#的新手,因此可以使用下面的代码,所要询问的内容与SQL Query分析器类似,其中您可以在SQL编辑器中键入SQL命令/语句并运行以获取结果

在Windows窗体上放置一个文本框和datagridview并使用按钮进行操作,并在按钮click事件上编写以下代码,并适当地放置数据库连接

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];
     }


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

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.[^]


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];
     }