Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单击按钮从表中选择数据_C#_Sql_Datagridview - Fatal编程技术网

C# 单击按钮从表中选择数据

C# 单击按钮从表中选择数据,c#,sql,datagridview,C#,Sql,Datagridview,我试图将一个表提取到DataGridView中,但它显示了一个空表 private void button1_Click(object sender, EventArgs e) { var dt = new DataTable(); var SDA = new SqlDataAdapter("Select * from "+txt_Search.Text+" ", conn); SDA.Fill(dt); dgv.DataSource = dt; tx

我试图将一个表提取到
DataGridView
中,但它显示了一个空表

private void button1_Click(object sender, EventArgs e)
{
    var dt = new DataTable();

    var SDA = new SqlDataAdapter("Select * from "+txt_Search.Text+" ", conn);

    SDA.Fill(dt);
    dgv.DataSource = dt;
    txt_Search.Text = "";
}

您提供的代码段没有提供太多上下文。请尝试使用我提供的以下代码段。也许你看到了你错过的东西。希望这有帮助

private void button1_Click(object sender, EventArgs e)
{
    var dt = new DataTable();

    var SDA = new SqlDataAdapter("Select * from "+txt_Search.Text+" ", conn);

    SDA.Fill(dt);
    dgv.DataSource = dt;
    txt_Search.Text = "";
}
const string connectionString = "";
SqlConnection connection = new SqlConnection(connectionString)

public DataTable ExecuteCommandToDataTable(string commandText, CommandType 
commandType, int timeout, params SqlParameter[] parameters)
{
    if (this.connection == null)
        throw new NullReferenceException("The connection was not initialized.");

    SqlCommand command = new SqlCommand(commandText, 
    this.connection.SqlConnection);            
    command.CommandType = commandType;
    command.CommandTimeout = timeout;

    for (int i = 0; parameters != null && i < parameters.Length; i++)
        command.Parameters.Add(parameters[i]);


    this.connection.Open();

    DataTable tbl = new DataTable();
    using (SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand))
    {
        adapter.Fill(tbl);
    }

    this.connection.Close();

    return tbl;
}
const string connectionString=“”;
SqlConnection连接=新的SqlConnection(connectionString)
公共数据表ExecuteCommandToDataTable(字符串commandText,CommandType
commandType,int timeout,params SqlParameter[]参数)
{
if(this.connection==null)
抛出新的NullReferenceException(“连接未初始化”);
SqlCommand=newSQLCommand(commandText,
this.connection.SqlConnection);
command.CommandType=CommandType;
command.CommandTimeout=超时;
对于(int i=0;parameters!=null&&i
首先要做的是在代码中设置一个断点,并检查dt在指定为数据源时是否有任何数据。@ChrisBerger--不,首先要做的是寻找掩护,因为sql注入攻击将是灾难性的。很难判断这里出了什么错。首先,确保您没有忽略XXXXException的第一次机会…。并测试每个步骤:
conn
是否成功打开?表名是保留关键字吗?(可能要用[]或``引用该名称)表是否包含数据。。。etc@Hogan-嗯,你说得有道理。我假设唯一会在txt_搜索中输入内容的人是程序员,因为它看起来就像是一个试图学习如何编码的人。但必须指出的是,您永远不应该在查询字符串中包含用户提供的文本。通常,您会使用参数化查询,但当然不能使用表名。如果OP真的想这样做,他们将需要验证它是否是一个有效的表名。(也许不应该这么做。)一旦问题解决了,你需要考虑一下。因为恶意用户可能试图搜索类似“随机”表的内容;删除表格“重要表格”。