最简单的ASP.NET数据库操作示例源代码
来源:互联网 发布:内控风险控制矩阵 编辑:程序博客网 时间:2024/05/29 13:21
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// SqlConnection con = new SqlConnection("server=dbtalbe;uid=sa;pwd=sa;database=addb;");
//con.Open();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["strconn"]);
con.Open();
/*写入数据库
SqlCommand cmd = new SqlCommand("INSERT INTO [Ad_Sort] ([sortname]) VALUES ('首页广告')",con);
cmd.ExecuteNonQuery();
Response.Write("写入成功!");
con.Close();
*/
string strsql = "select * from ad_sort order by id desc";
//读取数据
SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "ad_sort");
this.hotcommentlist.DataSource = ds;
this.hotcommentlist.DataBind();
//读取单个数据
SqlDataAdapter Set = new SqlDataAdapter(strsql, con);
DataSet Rs = new DataSet();
Set.Fill(Rs,"ad_sort");
//DataRowView mydrview = Rs.Tables["ad_sort"].DefaultView[0];
this.sortname.InnerHtml = Convert.ToString(Rs.Tables["ad_sort"].DefaultView[0].Row[1]);
/*
SqlDataAdapter Set = new SqlDataAdapter(strsql,con);
DataSet Rs = new DataSet();
Set.Fill(Rs,"ad_sort");
this.sortname.InnerHtml = Convert.ToString(Rs.Tables["ad_sort"].DefaultView[0].Row[1]);
this.sortname.InnerHtml = Convert.ToString(Rs.Tables["ad_sort"].DefaultView[0].Row[1]);
*/
Swww.169mnk.comqlCommand cmd = new SqlCommand(strsql, con);
SqlDataReader Res1 = cmd.ExecuteReader();
string listbody="";
listbody += ("<table border=0 ");
listbody+=("<tr><td>ExecuteReader读取数据</td></tr>");
listbody += ("<tr><th>编号</th><th>名称</th></tr>");
while (Res1.Read())
{
listbody += ("<tr>");
listbody += ("<td align='center'>" + Res1["id"].ToString() + "</td>");
listbody += ("<td align='left'>" + Res1["sortname"].ToString() + "</td>");
listbody += ("</tr>");
}
listbody += ("</table>");
list2.InnerHtml = listbody;
Res1.Close();
con.Close();
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// SqlConnection con = new SqlConnection("server=dbtalbe;uid=sa;pwd=sa;database=addb;");
//con.Open();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["strconn"]);
con.Open();
/*写入数据库
SqlCommand cmd = new SqlCommand("INSERT INTO [Ad_Sort] ([sortname]) VALUES ('首页广告')",con);
cmd.ExecuteNonQuery();
Response.Write("写入成功!");
con.Close();
*/
string strsql = "select * from ad_sort order by id desc";
//读取数据
SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
DataSet ds = new DataSet();
sda.Fill(ds, "ad_sort");
this.hotcommentlist.DataSource = ds;
this.hotcommentlist.DataBind();
//读取单个数据
SqlDataAdapter Set = new SqlDataAdapter(strsql, con);
DataSet Rs = new DataSet();
Set.Fill(Rs,"ad_sort");
//DataRowView mydrview = Rs.Tables["ad_sort"].DefaultView[0];
this.sortname.InnerHtml = Convert.ToString(Rs.Tables["ad_sort"].DefaultView[0].Row[1]);
/*
SqlDataAdapter Set = new SqlDataAdapter(strsql,con);
DataSet Rs = new DataSet();
Set.Fill(Rs,"ad_sort");
this.sortname.InnerHtml = Convert.ToString(Rs.Tables["ad_sort"].DefaultView[0].Row[1]);
this.sortname.InnerHtml = Convert.ToString(Rs.Tables["ad_sort"].DefaultView[0].Row[1]);
*/
Swww.169mnk.comqlCommand cmd = new SqlCommand(strsql, con);
SqlDataReader Res1 = cmd.ExecuteReader();
string listbody="";
listbody += ("<table border=0 ");
listbody+=("<tr><td>ExecuteReader读取数据</td></tr>");
listbody += ("<tr><th>编号</th><th>名称</th></tr>");
while (Res1.Read())
{
listbody += ("<tr>");
listbody += ("<td align='center'>" + Res1["id"].ToString() + "</td>");
listbody += ("<td align='left'>" + Res1["sortname"].ToString() + "</td>");
listbody += ("</tr>");
}
listbody += ("</table>");
list2.InnerHtml = listbody;
Res1.Close();
con.Close();
}
}
- 最简单的ASP.NET数据库操作示例源代码
- 最简单的asp.net 操作 access 数据库方法
- [ASP/ASP.net]最简单的XML创建、写入操作
- 最简单的ASP网页计数器源代码
- 简单的数据库操作示例
- FileUpload上传组件在ASP.net中最简单的运用示例
- Asp.Net判断一个字符串是否包含某个字符的最简单方法!(示例)
- 数据库操作简单示例
- zTree在asp.net中读取数据库简单使用示例
- .NET关于操作进程的简单示例
- .NET关于操作进程的简单示例
- .NET关于操作进程的简单示例
- .NET关于操作进程的简单示例
- .NET关于操作进程的简单示例
- 最简单的asp.net分页
- 最简单的asp.net分页
- .NET Remoting 最简单示例
- .NET Remoting 最简单示例
- 初学者asp.net学习教程之进步流程
- android_Component之自定义组合控件
- (转)基于H.264的远程视频监控
- ASP.NET生成静态HTML源码示例(C#)
- LA3026 KMP 关于循环节的问题
- 最简单的ASP.NET数据库操作示例源代码
- Autoencoders
- ios socket编程初步:iphone客户端与java服务端通信
- Linux系统下获取帮助
- Trie 集合
- ASP.NET生成静态HTML页面的方法
- x86函数调用堆栈的操作
- struts2续
- EL 表达式