using (SqlConnection conn = new SqlConnection(_connstr))
using(SqlConnectionconn=newSqlConnection(_connstr))这个using有什么作用?...
using (SqlConnection conn = new SqlConnection(_connstr))
这个using 有什么作用? 展开
这个using 有什么作用? 展开
5个回答
展开全部
就是把图片以二进制存储到数据库,具体方法可见
1、建所需数据库和表,语句如下:
--建立数据库
create database test
--使用该数据库
use test
--建立存放图片的表
create table piclist(
id int Identity primary key,
pic Image not null
)
2、制作上传图片的模块,代码如下:
前台html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpPhoto.aspx.cs" Inherits="Test_UpPhoto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpPhoto" name="UpPhoto" runat="server" type="file" />
<asp:Button id="btnAdd" runat="server" Text="上传" OnClick="btnAdd_Click"></asp:Button>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class Test_UpPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
//连接数据库
string ConStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection conn = new SqlConnection(ConStr);
string strSql = "Insert into piclist(pic) values(@pic)";
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.Parameters.Add("@pic", SqlDbType.Image);
cmd.Parameters["@pic"].Value = PhotoArray;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("图片上传成功");
}
}
3、制作显示图片的模块(单独显示图片,即没用到datalist):
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Test_ShowPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd=new SqlCommand(strSql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}
}
补充步骤3,用datalist显示图片方法:
建立两个asp.net 页面,名称为piclist.aspx和StreamImg.aspx。
piclist.aspx前台代码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="piclist.aspx.cs" Inherits="Test_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; background-image: url(Image/标头.jpg); height: 26px;">
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"id") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
piclist.aspx后台代码为:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select id from piclist";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "tb_pic");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}
StreamImg.aspx无前台代码,后台代码为:
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;
using System.IO;
public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["id"]);
ShowPic(id);
}
private void ShowPic(int id)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist where id='"+ id +"'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作
1、建所需数据库和表,语句如下:
--建立数据库
create database test
--使用该数据库
use test
--建立存放图片的表
create table piclist(
id int Identity primary key,
pic Image not null
)
2、制作上传图片的模块,代码如下:
前台html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpPhoto.aspx.cs" Inherits="Test_UpPhoto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpPhoto" name="UpPhoto" runat="server" type="file" />
<asp:Button id="btnAdd" runat="server" Text="上传" OnClick="btnAdd_Click"></asp:Button>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
public partial class Test_UpPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//获得图象并把图象转换为byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
//连接数据库
string ConStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection conn = new SqlConnection(ConStr);
string strSql = "Insert into piclist(pic) values(@pic)";
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.Parameters.Add("@pic", SqlDbType.Image);
cmd.Parameters["@pic"].Value = PhotoArray;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("图片上传成功");
}
}
3、制作显示图片的模块(单独显示图片,即没用到datalist):
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Test_ShowPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd=new SqlCommand(strSql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}
}
补充步骤3,用datalist显示图片方法:
建立两个asp.net 页面,名称为piclist.aspx和StreamImg.aspx。
piclist.aspx前台代码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="piclist.aspx.cs" Inherits="Test_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; background-image: url(Image/标头.jpg); height: 26px;">
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"id") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
piclist.aspx后台代码为:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select id from piclist";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "tb_pic");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}
StreamImg.aspx无前台代码,后台代码为:
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;
using System.IO;
public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["id"]);
ShowPic(id);
}
private void ShowPic(int id)
{
//连接数据库
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist where id='"+ id +"'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作
展开全部
using就是调用的意思,就是说在接下来using的代码范围内你能使用conn这个对象,一旦你超出了using的代码范围,就没得conn这个对象给你用了
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
相当于在using块结束的时候手动调用conn.Dispose()方法!
属于优化资源配置!^_^
属于优化资源配置!^_^
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
意思是,退出using后,会自动调用conn的Dispose方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当using里的代码都运行完了就将conn的连接关闭
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询