Asp.Net程序使用gridview显示数据库中的表值?

编程入门 行业动态 更新时间:2024-10-26 18:25:03
本文介绍了Asp.Net程序使用gridview显示数据库中的表值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何编写程序来显示数据库中的表值.. i在我的数据库中有一个表格,如... eid,ename,address,designation ,薪水... i希望在Gridview中显示这些值。在显示表格值之前我想以XMl格式转换,请任何人帮我解决这个问题...

解决方案

1编写存储过程并将该过程与网格视图绑定,请参阅以下链接供您参考: www.codecomplete4u/showing-data-gridview-database-asp-net/ [ ^ ]

public void BindGrid() { using(SqlConnection con = new SqlConnection(ConnectionString)) { SqlCommand cmd = new SqlComman d() { Connection = con, CommandText =StoreProcedureName / sqlquery, CommandType = CommandType.StoredProcedure //如果你的命令文本包含SP名称,则使用CommandType.Text }; //如果需要添加参数,如cmd.Parameters.Add cmd.Connection.Open(); SqlDataReader rdr = cmd.ExecuteReader(); DataTable _dt = new DataTable(); _dt.Load(rdr); //在从DataTable绑定gridview加载xml文件之前 System.IO.StringWriter _writer = new System.IO.StringWriter(); _dt.WriteXml(_writer,XmlWriteMode.WriteSchema,false); string _result = _writer.ToString(); //'_ gridview'是你的Gridview的id _gridview.DataSource = _dt; _gridview.DataBind(); } }

如果有任何问题,请不要忘记检查你的连接字符串

SqlConnection Conn = new SqlConnection( YourConnectionString ); SqlDataReader rdr = null ; string commandString = SELECT * FROM YourTableName ; 尝试 { Conn.Open(); SqlCommand Cmd = new SqlCommand(commandString,Conn); rdr = Cmd.ExecuteReader(); YourGridId .DataSource = rdr; YourGridId .DataBind(); } catch (例外情况) { // 日志错误 } 最后 { if (rdr!= null ) { rdr.Close( ); } if (Conn!= null ) { Conn.Close(); } }

How can i write Program to display table values from the database.. i have a table in my database like... eid,ename,address,designation,salary... i want to display these values in Gridview..and before display table values i want to convert in XMl format, please any one help me regarding this...

解决方案

1 Write stored procedure and bind that procedure with your grid view see below link for your reference: www.codecomplete4u/showing-data-gridview-database-asp-net/[^]

public void BindGrid() { using (SqlConnection con = new SqlConnection("ConnectionString")) { SqlCommand cmd = new SqlCommand() { Connection = con, CommandText = "StoreProcedureName/sqlquery", CommandType = CommandType.StoredProcedure//if your commandtext containts SP Name else use CommandType.Text }; //if required add parameter like cmd.Parameters.Add cmd.Connection.Open(); SqlDataReader rdr = cmd.ExecuteReader(); DataTable _dt = new DataTable(); _dt.Load(rdr); //before binding your gridview load xml file from DataTable System.IO.StringWriter _writer = new System.IO.StringWriter(); _dt.WriteXml(_writer, XmlWriteMode.WriteSchema, false); string _result = _writer.ToString(); //'_gridview' is id of your Gridview _gridview.DataSource = _dt; _gridview.DataBind(); } }

This works if any issue dont forget to check your connection string

SqlConnection Conn = new SqlConnection("YourConnectionString"); SqlDataReader rdr = null; string commandString = "SELECT * FROM YourTableName"; try { Conn.Open(); SqlCommand Cmd = new SqlCommand(commandString, Conn); rdr = Cmd.ExecuteReader(); YourGridId.DataSource = rdr; YourGridId.DataBind(); } catch (Exception ex) { // Log error } finally { if (rdr != null) { rdr.Close(); } if (Conn != null) { Conn.Close(); } }

更多推荐

Asp.Net程序使用gridview显示数据库中的表值?

本文发布于:2023-10-23 23:10:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   程序   Asp   Net   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!