从db搜索数据并使用一个固定列名显示到datagridview [英] Search data form db and display to datagridview with one fixed column name

查看:53
本文介绍了从db搜索数据并使用一个固定列名显示到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的数据库中搜索数据并将数据显示在gridview中的固定列上。

我试过这个但不行。请帮助预付







i want to search data from my db and display data onto the fixed column in the gridview.
i tryed this but not working. please helpmeout



private void txtDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Configuratiing = 
             "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
         
            string sqlQuery = null;
            sqlQuery = "select * from DistrictnT where District = ''" + txtDistrict.Text + "''";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            try
            {
                while (dr.Read())
                {
                    dgvDistrict.Rows[dgvDistrict.Rows.Count - 1].Cells["clmTowns"].Value = 
                      dr[2].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message, "Error", 
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn.Close();
            }
	}

推荐答案

这有一些问题:

)您正在连接字符串以形成SQL语句,这使您可以对SQL注入进行全面开放,这可能会损坏或破坏您的数据库。改为使用参数化查询。

1)你没有在SELECT语句中列出你的字段(这是一个相当差的想法),但你明确使用了第三个(也是第三个)列。返回的数据,是否存在。更好的方法是只返回您感兴趣的一列 - 这会减少使用的带宽以及确保数据可用。

2)如果您的DataGridView没有行,那么您的 dgvDistrict.Rows [dgvDistrict.Rows.Count - 1] 将失败并出现索引异常。

3)您在同一单元格中写入所有数据DataGridView,它有点使循环冗余。 (这可能是您关心的问题)相反,您需要使用变量将行号从0更改为阅读器中的行数。



但是不要这样做 - 它很容易出问题而且不值得做。

如果你想搜索并过滤结果,请使用BindingSource来提供整个数据到DataGridView,并使用它的Filter属性只显示匹配的行。它更快,更简单,更容易!
There are a few things wrong with this:
0) You are concatenating strings to form an SQL statement, which leave you wide open to SQL Injection which can damage or destroy your database. Use parametrized queries instead.
1) You don''t list your fields in the SELECT statement (which is a fairly poor idea) but you explicitly use the third (and only the third) column of the returned data, whether it exists or not. A better approach would be to only return the one column you are interested in - this reduces bandwidth used as well as ensuring that the data is available.
2) If your DataGridView has no rows, then your dgvDistrict.Rows[dgvDistrict.Rows.Count - 1] will fail with an indexing exception.
3) You write all the data in the same cell of the DataGridView, which kinda makes the loop redundant. (This is probably the problem you are concerned about) Instead, you need to use a variable to change the row number from 0 to the number of rows in the reader.

But don''t do it like that - it is so liable to problems that it isn''t worth doing.
If you want to search and filter the results, use a BindingSource to supply the whole data to the DataGridView, and use it''s Filter property to show only the matching rows. It''s quicker, simpler and a lot easier!


这篇关于从db搜索数据并使用一个固定列名显示到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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