本文介绍了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显示数据库中的表值?
发布评论