使用从文本框生成的sql命令在数据网格中显示行 [英] Display rows in data grid using an sql command generated from a textbox

查看:76
本文介绍了使用从文本框生成的sql命令在数据网格中显示行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是C#的新手,我已经尝试了一些方法.我接下来要做的是
如果我有一个带有文本框的表单,用户将在其中输入SQL命令,那么数据将显示在datagrid中.

解决方案

用户将在其中输入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];
     }


这篇关于使用从文本框生成的sql命令在数据网格中显示行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆