Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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_Datatable_Sqldatasource - Fatal编程技术网

C# 将数据表加载到数据网格视图中会出现问题

C# 将数据表加载到数据网格视图中会出现问题,c#,sql,datagridview,datatable,sqldatasource,C#,Sql,Datagridview,Datatable,Sqldatasource,我试图从SQL server获取信息,并根据用户选择的参数将其加载到datagridview中。我在本问题末尾发布的示例适用于程序中的早期函数,但现在不是。这使我相信问题在于实际向DGV输出数据的线路。有没有想过为什么它没有填满DGV?我举了两个例子,两个都不起作用。出于某种原因,他们只是没有将任何信息输入到DGV中,尽管我从调试中知道他们确实成功地从服务器中提取了信息 SqlConnection DBConnection = new SqlConnection(ConnectionString

我试图从SQL server获取信息,并根据用户选择的参数将其加载到datagridview中。我在本问题末尾发布的示例适用于程序中的早期函数,但现在不是。这使我相信问题在于实际向DGV输出数据的线路。有没有想过为什么它没有填满DGV?我举了两个例子,两个都不起作用。出于某种原因,他们只是没有将任何信息输入到DGV中,尽管我从调试中知道他们确实成功地从服务器中提取了信息

SqlConnection DBConnection = new SqlConnection(ConnectionString);

        //Opens the connection
        DBConnection.Open();

        //Creates a string to hold the query
        string query = "SELECT * FROM PRD WHERE PRD_NUM LIKE '" +OutputBeforeIncrement + "%'";

        //Creates an SQLCommand object to hold the data returned by the query 
        SqlCommand queryCommand = new SqlCommand(query, DBConnection);        

        //Uses the aforementioned SQLCommand to create an SQLDataReader object
        SqlDataReader queryCommandReader = queryCommand.ExecuteReader();      

         //Creates a DataTable to hold the data                               
        DataTable dataTable = new DataTable();

        //This part actually loads the data from the query into the table     
        dataTable.Load(queryCommandReader);
        dgvOutput.DataSource = dataTable;
另一个例子是:

using (SqlDataAdapter newDA = new SqlDataAdapter(query, DBConnection))
            {
                DataTable Table = new DataTable();
                newDA.Fill(Table);

                dgvOutput.DataSource = Table;
            }

您可以尝试以下方法或类似方法:

SqlDataAdapter myDataAdapter;
SqlCommandBuilder myCommandBuilder;
SqlCommand mySqlCommand = new SqlCommand(myQuery, MySQLConnection);
//I think this is the default command type, and thus can be omitted
mySqlCommand.CommandType = CommandType.Text; 
myDataAdapter = new SqlDataAdapter(mySqlCommand);
//Automates your insert/update/delete
myCommandBuilder = new SqlCommandBuilder(myDataAdapter);
myDataAdapter.Fill(myDataTable);
dgvOutput.DataSource = myDataTable.DefaultView;

“不工作”是什么意思?没有数据?是否引发异常?您是否已调试以确定表中是否包含任何数据?增量前输出的值是多少?啊,抱歉,我的描述不正确!我已经更新了这个问题。OutputBeforeIncrement是I_______%(I后跟9个下划线)。它成功地提取了所有以I开头的至少有10个字符的记录。