C# 中SqlDataReader怎么获取列名

C# 中SqlDataReader怎么获取列名,第1张

  SqlConnection thisConnection = new SqlConnection(ConfigurationManagerAppSettings["ConnectionString"]ToString());

            // Open connection

            thisConnectionOpen();

            // Create command for this connection

            SqlCommand thisCommand = thisConnectionCreateCommand();

            // Specify SQL query for this command

            thisCommandCommandText = sCommandText;

            // Execute DataReader for specified command

            SqlDataReader thisReader = thisCommandExecuteReader();

            // While there are rows to read

            //

            //

            View_LogDetailEntity = new List<CView_LogDetail>();//初始化集合

 string sColumnName = stringEmpty;

  while (thisReaderRead())

            {

                CView_LogDetail myCView_LogDetail = new CView_LogDetail();

                for (int i = 0; i < thisReaderFieldCount; i++)

                {

                      sColumnName = thisReaderGetName(i)Trim();

                }

           }

获取GridView里某行某列的值示例

//GridView设置

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"

CellPadding="4" Font-Size="9pt" ForeColor="#333333" GridLines="None"

Width="500px" OnRowDeleting="GridView1_RowDeleting"

OnPageIndexChanging="GridView1_PageIndexChanging"

OnRowDataBound="GridView1_RowDataBound" PageSize="6" Height="1px">

<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<RowStyle BackColor="#E3EAEB" />

<Columns>//codegonet/tags/11/1/

<asp:BoundField DataField="ST_n_id" HeaderText="文章ID" />

<asp:BoundField DataField="ST_n_title" HeaderText="文章主题" />

<asp:BoundField DataField="ST_n_hit" HeaderText="人气指数" />

<asp:BoundField DataField="ST_n_re" HeaderText="评论" />

<asp:HyperLinkField DataNavigateUrlFields="ST_n_id" DataNavigateUrlFormatString="ViewContentaspxid={0}"

HeaderText="查看信息" Text="查看信息" />

<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />

<asp:TemplateField HeaderText="选择">

<ItemTemplate>

<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="9pt" Width="9px" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<SelectedRowStyle BackColor="#C5BBAF" ForeColor="#333333" Font-Bold="True" />

<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#7C6F57" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

//加载数据库数据

SqlData da = new SqlData();

private static string ID;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

/判断是否登录/

ST_check_Login();

}

//定义一个查询用户信息的SQL语句

string sqlstr = "select from tb_Blog where UserName='" + Session["UserName"] + "'";

//调用公共类中的GetDataSet方法执行SQL语句,并返回DataSet类型的数据集

DataSet mydataset = daGetDataSet(sqlstr, "tb_Blog");

//创建数据表的视图

DataRowView rowview1 = mydatasetTables["tb_Blog"]DefaultView[0];

//获取注册博客的编号

ID = rowview1["BlogID"]ToString();

//调用自定义绑定方法绑定数据

ST_Article_Bind();

}

//绑定数据库显示数据

public void ST_Article_Bind()

{

//根据获取的用户ID查询其所有文章

string sqlstr = "select from ST_news where BlogID='" + ID + "'";

//调用公共类中的ExceDS方法,返回一个DataSet类型的数据集,并作为GridView控件的数据源

GridView1DataSource = daExceDS(sqlstr);

//获取GridView控件中的主键字段

GridView1DataKeyNames = new string[] { "ST_n_id" };

//从数据库中绑定数据到列表控件中

GridView1DataBind();

}

//查看某行某列信息跳转ViewContentaspx

<asp:Label ID="labSubject" runat="server" Width="316px" Font-Size="9pt" ></asp:Label>//显示主题

<asp:TextBox ID="txtContent" runat="server" Font-Size="9pt" Height="257px" TextMode="MultiLine"

Width="405px"></asp:TextBox>//显示内容

//加载绑定数据库内容显示数据

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

try

{

string str;

string str2 = PageRequest["id"]ToString();

//此处用来实现将数据绑定到前台

SqlConnection mycon = new SqlConnection(ConfigurationManagerAppSettings["conStr"]);

myconOpen();

SqlDataAdapter myada = new SqlDataAdapter("select from ST_news where ST_n_id='" + str2 + "'", mycon);

DataSet ds = new DataSet();

myadaFill(ds, "ST_news");

//创建DataRowView对象的一个实例

DataRowView rowview = dsTables["ST_news"]DefaultView[0];

//将文章主题读取到labSubject文本框中

thislabSubjectText = rowview["ST_n_title"]ToString();

//将文章内容读取到txtContent文本框中

thistxtContentText = rowview["ST_n_content"]ToString();

myconClose();

}

catch (Exception ex)

{

ResponseWrite(exMessage);

}

}

}

GridView1Rows[eNewSelectedIndex]Cells[4]Text

以上就是关于C# 中SqlDataReader怎么获取列名全部的内容,包括:C# 中SqlDataReader怎么获取列名、怎么获取gridview绑定行某列的值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9547496.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存