0
点赞
收藏
分享

微信扫一扫

SQL测试+TABLE动态创建

艾晓雪 2022-11-11 阅读 88


SQL测试+TABLE动态创建_sql2005

SQL测试+TABLE动态创建_sql2005_02

SQL测试+TABLE动态创建_sql2005_03

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SQL---test.aspx.cs" Inherits="SQL___test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<style type="text/css">
table{ border-collapse:collapse;}
td{ border:1px solid gray}

</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>SQL--TEST</h1>
<p>
<asp:TextBox ID="txt_sql" runat="server" TextMode="MultiLine" Height="62px" ></asp:TextBox>
<asp:Button ID="btn_test" runat="server" Text="测试" οnclick="btn_test_Click" />
</p>
<asp:Table ID="tbl_result" runat="server">
</asp:Table>
</div>
</form>
</body>
</html>

.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class SQL___test : System.Web.UI.Page
{ string con = "server=localhost\\SQL2005 ;uid=sa;pwd=1111qq;database=userinfo";
SqlConnection conn;
SqlCommand cmd;
SqlDataReader datar;
string str_sql;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btn_test_Click(object sender, EventArgs e)
{
conn = new SqlConnection(con);
conn.Open();

str_sql = txt_sql.Text;
cmd = new SqlCommand(str_sql, conn);
datar = cmd.ExecuteReader();

int i_fcount = datar.FieldCount;//字段数量
//构建表头
TableRow tr = new TableRow();
for (int i = 0; i < i_fcount; i++) {
TableCell td = new TableCell();
td.Text = datar.GetName(i);
tr.Cells.Add(td);
}
tbl_result.Rows.Add(tr);
//向表格里插入数据
while (datar.Read()) {
tr = new TableRow();
for (int i = 0; i < i_fcount; i++)
{
TableCell td = new TableCell();
td.Text = datar[i].ToString();
tr.Cells.Add(td);
}
tbl_result.Rows.Add(tr);
}

conn.Close();
}

}

效果如下:

SQL测试+TABLE动态创建_sql_04



举报

相关推荐

0 条评论