使用csharp在运行时从数据网格视图中的数据库中检索colmn [英] colmn to be retrieved from the data base in the data grid view in the run time using csharp

查看:144
本文介绍了使用csharp在运行时从数据网格视图中的数据库中检索colmn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库如下;





SNO int

学院代码varchar

教师姓名varchar

手机版本


数据库记录中有




列在运行时从数据网格视图中的数据库中检索。



,表格加载代码如下;



Database as follows;


SNO int
Faculty code varchar
Faculty Name varchar
Mobile varchar

in the data base records are there.

column to be retrieved from the data base in the data grid view in the run time.

in the form load code as follows;

            con.Open();
SqlCommand cmd = new SqlCommand("select  Faculty_Name from Tb_SCH_Faculty_Details", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                datagridView.Columns[2].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[3].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[4].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[5].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[6].HeaderText = dr[2].ToString().Trim();
                dr.Close();
                con.Close();
            }



当我运行消息时显示索引超出了数组范围。





请帮帮我。我想通过数据阅读器使用数据阅读器我从数据库中获取Faculty Name。


when i run message shows index was outside the bound of array.


please help me. i want to use the data reader through data reader i get the Faculty Name from the data base.

推荐答案

SqlCommand cmd = new SqlCommand(从Tb_SCH_Faculty_Details中选择Faculty_Name, CON); 这里只有一个列值Faculty_Name。所以我们可以使用dr [0]





(或)



您必须更改查询



SqlCommand cmd = new SqlCommand(选择SNO,Faculty_code,来自Tb_SCH_Faculty_Details的Faculty_Name,con);
SqlCommand cmd = new SqlCommand("select Faculty_Name from Tb_SCH_Faculty_Details", con); Here you are get only one column value "Faculty_Name". so we can use dr[0]


(or)

You must change the query

SqlCommand cmd = new SqlCommand("select SNO,Faculty_code, Faculty_Name from Tb_SCH_Faculty_Details", con);






在你的select语句中你只调用一个参数。

所以,每当你调用SQLDataReader时你提到的对象是正确的。



ex:



你的呼唤像这样的博士[2] ],所以请避免这个

使用dr [0]。



索引超出数组范围

这个错误意思在你的datareader中你只有dr [0]位置但是你打电话给dr [2]。所以,你越过了可能导致这个问题的原因错误。







我认为这可能对你有帮助
Hi,

In your select statement your calling only one parameter.
So, whenever you call the SQLDataReader object you mentioned the possition is properly.

ex:

your calling something like this dr[2], so please be avoid this
use dr[0].

index was outside the bound of array
this error meaning is in your datareader u have only dr[0] position only but you call dr[2].So, you cross the limit that possition that''s the reason to cause this error.



I think this might be help for u


设置dr.Close()和con.Close();在末端括号下面}。现在在1行之后你在尝试获得第二行时关闭连接。

此外所有列都获得相同的headertext。 dr [2]应该是dr [1](从0开始计数(c-原理))

并阅读有关学习使用USING



Set dr.Close() and con.Close(); below the end bracket }. Now after 1 row you close the connection while trying to get a second row.
Furthermore all Columns get the same headertext. dr[2] should be dr[1] (start counting with 0 (c-principle))
And read about learning to use USING

using (SqlConnection con = ...)
{  
   con.Open();
   using (SqlCommand cmd = new SqlCommand("select SNO, Faculty_Name from Tb_SCH_Faculty_Details", con))
     {
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                datagridView.Columns[2].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[3].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[4].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[5].HeaderText = dr[2].ToString().Trim();
                datagridView.Columns[6].HeaderText = dr[2].ToString().Trim();
                
            }
         }
         dr.Close();
     }
     con.Close();
}


这篇关于使用csharp在运行时从数据网格视图中的数据库中检索colmn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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