asp.net可以做什么
发布网友
发布时间:2022-04-22 02:35
我来回答
共5个回答
热心网友
时间:2022-05-01 02:46
1.提取字段列表
/*
<%@ Import Namespace="System.Data.SqlClient"%>
*/
//<%
using(SqlConnection cn=new SqlConnection("Data Source=localhost;Initial Catalog=pubs;UserID=sa;Password=")) {
SqlCommand cmd=new SqlCommand("Select * from authors");
cn.Open();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read()) {
Response.Write(dr("au_fname"));
}
dr.Close();
cn.Close();
}
//%>
2.返回数据集函数
/*
<%@ Import Namespace="System.Data.SqlClient"%>
*/
//<%
public DataSet GetData()
{
using(SqlConnection cn=new SqlConnection("Data Source=localhost;Initial Catalog=pubs;UserID=sa;Password="))
{
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("Select * from authors",cn);
DataSet ds=new DataSet();
da.Fill(ds,"Authors");
cn.Close();
return ds;
}
return null;
}
//%>
3.从XML文件中读取数据
/*
<%@ Import Namespace="System.Data.SqlClient"%>
*/
//<%
DataSet dst=new DataSet();
dst.ReadXml(MapPath(%%1)); //"*.xml"
DataGrid1.DataSource=dst;
DataGrid1.DataBind();
//%>
4.把数据写入XML文件
/*
<%@ Import Namespace="System.Data.SqlClient"%>
*/
//<%
using(SqlConnection conn=new SqlConnection("Data Source=localhost;Initial Catalog=pubs;UserID=sa;Password="))
{
SqlDataAdapter da=new SqlDataAdapter("Select * from authors",conn);
DataSet ds=new DataSet();
da.Fill(ds,"Authors");
ds.WriteXml(MapPath(%%1)); //"*.xml"
cn.Close();
}
//%>
5.遍历XML文档
/*
<%@ Import Namespace="System.Xml"%>
*/
//<%
XmlDocument objParser=new XmlDocument();
Object objRoot,objNode;
objParser.Load(Server.MapPath(%%1)); //"*.xml"
objRoot=objParser.documentElement();
for(int i=0;i<objRoot.childNodes.count;i++)
{
objNode=objRoot.childNodes[i];
Response.Write("<tr><td>");
Response.Write(objNode.getAttribute("name") & "</td><td align='center'>");
Response.Write(objNode.childNodes.count)
}
}
//%>
6.搜索XML元素
/*
<%@ Import Namespace="System.Xml"%>
*/
//<%
XmlDocument objDocument=new XmlDocument();
XmlElement objRoot;
XmlNodeList objNodeList;
objDocument.Load(Server.MapPath(%%1)); //"*.xml"
objRoot=objDocument.DocumentElement;
objNodeList=objRoot.SelectNodes("//ablum[@artist='" & txtArtist.Text & "']");
if(objNodeList.Count==0)
{
lblMessage.Text="No records"
}
else
{
DataSet objDataSet=new DataSet();
DataTable objDataTable;
DataColumn objColumn;
DataRow objRow;
objDataTable=objDataSet.Tables.Add();
objColumn=new DataColumn("album");
objDataTable.Columns.Add(objColumn);
objColumn=new DataColumn("year");
objDataTable.Columns.Add(objColumn);
foreach(XmlNode objNode in objNodeList)
{
objRow=objDataTable.NewRow();
objRow["album"]=objNode.Attributes["name"].Value;
objRow["year"]=objNode.Attributes["releaseDate"].Value;
objDataTable.Rows.Add(objRow);
}
}
DataGrid1.DataSource=objDataSet;
DataGrid1.DataBind();
//%>
7.下载文件
/*
<%@ Import Namespace="System.IO"%>
*/
string FullFileName=%%1;
FileInfo DownloadFile = new FileInfo(FullFileName); //设置要下载的文件
Response.Clear(); //清除缓冲区流中的所有内容输出
Response.ClearHeaders(); //清除缓冲区流中的所有头
Response.Buffer = false; //设置缓冲输出为false
//设置输出流的 HTTP MIME 类型为application/octet-stream
Response.ContentType = "application/octet-stream";//将 HTTP 头添加到输出流
Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode
(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());//将指定的文件直接写入 HTTP
内容输出流。
Response.WriteFile(DownloadFile.FullName);
Response.Flush(); //向客户端发送当前所有缓冲的输出
Response.End(); //将当前所有缓冲的输出发送到客户端
8.调用存储过程查询
/*
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data"%>
*/
string myConnectString="Data Source=localhost;Initial Catalog=pubs;UserID=sa;Password=";
using(SqlConnection myconn=new SqlConnection(myConnectString))
{
SqlDataAdapter mycmd=new SqlDataAdapter("mypro",myconn);
mycmd.SelectCommand.CommandType=CommandType.StoreProcere;
DataSet ds=new DataSet();
mycmd.Fill(ds,"Titles");
DataGrid1.DataSource=ds.Tables["Titles"].DefaultView;
DataGrid1.DataBind();
}
9.参数存储过程查询
//Select * from authors where State=@State
/*
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Data"%>
*/
string myConnectString="Data Source=localhost;Initial Catalog=pubs;UserID=sa;Password=";
using(SqlConnection myconn=new SqlConnection(myConnectString))
{
SqlDataAdapter mycmd=new SqlDataAdapter("mypro",myconn);
mycmd.SelectCommand.Parameters.Add(new SqlParamter("@State",SqlDbType.NVarChar,2));
mycmd.SelectCommand.CommandType=CommandType.StoreProcere;
DataSet ds=new DataSet();
mycmd.Fill(ds,"Titles");
DataGrid1.DataSource=ds.Tables["Titles"].DefaultView;
DataGrid1.DataBind();
}
10.验证码
/*
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
*/
//建立位图对象
Bitmap newBitmap = new Bitmap(36, 16, PixelFormat.Format32bppArgb);
//根据上面创建的位图对象创建绘图面
Graphics g = Graphics.FromImage(newBitmap);
//创建字体对象
Font textFont = new Font("Times New Roman", 10);
//创建RectangleF结构指定一个区域
RectangleF rectangle = new RectangleF(0, 0, 36, 16);
//创建随机数对象
Random rd = new Random();
//取得随机数
int valationNo = 1000 + rd.Next(9000);
//使用指定的颜色填充上面RectangleF结构指定的矩形区域
g.FillRectangle(new SolidBrush(Color.BurlyWood), rectangle);
//在上面填充的矩形区域中填充上面生成的随机数
g.DrawString(valationNo.ToString(), textFont, new SolidBrush(Color.Blue), rectangle);
//把创建的位图保存到指定的路径
// 方法一
//newBitmap.Save(Server.MapPath("img") + "\\VaImg.gif", ImageFormat.Gif);
// 方法二
Response.ContentType = "image/jpeg";
newBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
///释放占用的资源
newBitmap.Dispose();
11.中文验证码
/*
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
*/
public static object[] CreateRegionCode(int strlength)
{
string[] rBase=new String [16]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};//定
义一个字符串数组储存汉字编码的组成元素
Random rnd=new Random();
object[] bytes=new object[strlength];//定义一个object数组用来
for(int i=0;i<strlength;i++)
{
//区位码第1位
int r1=rnd.Next(11,14);
string str_r1=rBase[r1].Trim();
//区位码第2位
rnd=new Random(r1*unchecked((int)DateTime.Now.Ticks)+i);//更换随机数发生器的种子避免产生重复
值
int r2;
if (r1==13)
{r2=rnd.Next(0,7);}
else
{r2=rnd.Next(0,16);}
string str_r2=rBase[r2].Trim();
//区位码第3位
rnd=new Random(r2*unchecked((int)DateTime.Now.Ticks)+i);
int r3=rnd.Next(10,16);
string str_r3=rBase[r3].Trim();
//区位码第4位
rnd=new Random(r3*unchecked((int)DateTime.Now.Ticks)+i);
int r4;
if (r3==10)
{r4=rnd.Next(1,16);}
else if (r3==15)
{r4=rnd.Next(0,15);}
else
{r4=rnd.Next(0,16);}
string str_r4=rBase[r4].Trim();
//定义两个字节变量存储产生的随机汉字区位码
byte byte1=Convert.ToByte(str_r1 + str_r2,16);
byte byte2=Convert.ToByte(str_r3 + str_r4,16);
byte[] str_r=new byte[]{byte1,byte2};//将两个字节变量存储在字节数组中
bytes.SetValue(str_r,i);//将产生的一个汉字的字节数组放入object数组中
}
return bytes;
}
private void CreateImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 21)),
22);
Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();//生成随机生成器
g.Clear(Color.White);//清空图片背景色
for(int i=0; i<20; i++)//画图片的背景噪音线
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("楷体", 12, (System.Drawing.FontStyle.Bold));
System.Drawing.Drawing2D.LinearGradientBrush brush = new
System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f,
true);
g.DrawString(checkCode, font, brush, 2, 2);
for(int i=0; i<100; i++)//画图片的前景噪音点
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);//画图片的边
框线
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{g.Dispose();
image.Dispose();}
}
// 在此处放置用户代码以初始化页面
Encoding gb=Encoding.GetEncoding("gb2312");
object[] bytes=CreateRegionCode(4);//调用函数产生4个随机中文汉字编码
//根据汉字编码的字节数组解码出中文汉字
string str1=gb.GetString((byte[])Convert.ChangeType(bytes[0], typeof(byte[])));
string str2=gb.GetString((byte[])Convert.ChangeType(bytes[1], typeof(byte[])));
string str3=gb.GetString((byte[])Convert.ChangeType(bytes[2], typeof(byte[])));
string str4=gb.GetString((byte[])Convert.ChangeType(bytes[3], typeof(byte[])));
Session["CheckCode"] = str1 + str2 +str3 +str4;//用于与输入的验证码作对比
CreateImage(str1 + str2 +str3 +str4);
热心网友
时间:2022-05-01 04:04
做你使用IE在上网时看到的一切
热心网友
时间:2022-05-01 05:39
做网站
热心网友
时间:2022-05-01 07:30
可以赚到MONEY.这总可以了吧.
热心网友
时间:2022-05-01 09:38
可以把MM的相片放在万维网中!