目的:将图片存放在数据库里面(不是存放图片的路径)数据库:
create table ImageStorage
(
mName varchar(30) primary key,
mData image ,
mClass varchar(10),
mIntro varchar(100),
mSize int
)
前台:
<body>
<form id="form1" runat="server">
<center ><div>
<br />
<span style="font-size: 1.4em; color: #ff0066"><strong>asp.net 中文件上传<br />
<br />
选择文件:<asp:FileUpload ID="filesUpload" runat="server" /><br />
<br />
图片说明:
<br />
<asp:TextBox ID="txtIntro" runat="server" Height="104px" TextMode="MultiLine" Width="209px"></asp:TextBox><br />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传文件" Width="173px" /></strong></span></div> </center>
</form>
</body>处理程序段:protected void btnUpload_Click(object sender, EventArgs e)
{
///////获取上传文件的相关信息
string path = this.filesUpload.PostedFile.FileName;
string fileName = Path.GetFileName(path);
int fileLength = this.filesUpload.PostedFile.ContentLength;
if (fileLength < 1)
return;
try
{
//////////////////////////////建立数据库连接,命令
SqlConnection con = new SqlConnection("server=FIRST-FIRST\\LOVE;database=stuTest;uid=sa;password=sa");
SqlCommand cmd = new SqlCommand("insert into ImageStorage values(@name,@image,@class,@discription,@size)", con);
//////////////////////////////读取图片
Stream objectStream = this.filesUpload.PostedFile.InputStream;
byte[] byteContent = new byte[fileLength ];
objectStream.Read(byteContent, 0, fileLength );
//////////////////////////////处理参数
SqlParameter par = new SqlParameter("@name", SqlDbType.VarChar, 30);
par.Value = fileName;
cmd.Parameters.Add(par);
par = new SqlParameter("@image", SqlDbType.VarBinary , fileLength);
par.Value = byteContent;
cmd.Parameters.Add(par);
par = new SqlParameter("@class", SqlDbType.VarChar, this.filesUpload.PostedFile.ContentType.Length);
par.Value = this.filesUpload.PostedFile.ContentType;
cmd.Parameters.Add(par);
par = new SqlParameter("@discription", SqlDbType.VarChar, 100);
par.Value = this.txtIntro.Text.ToString();
cmd.Parameters.Add(par);
par = new SqlParameter("@size", SqlDbType.Int);
par.Value = this.filesUpload.PostedFile.ContentLength;
cmd.Parameters.Add(par); con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception er)
{
Response.Write(er.Message .ToString ());
}
}
运行后:
出现 :"将截断字符串或二进制数据。语句已终止"请问:1、出现这种问题的原因是什么呀?
2、数据库里设定的数据大小和SqlParameter中对应数据的大小有什么关系?设置SqlParameter中数据大小(长度)的依据是什么呢?
谢谢····
create table ImageStorage
(
mName varchar(30) primary key,
mData image ,
mClass varchar(10),
mIntro varchar(100),
mSize int
)
前台:
<body>
<form id="form1" runat="server">
<center ><div>
<br />
<span style="font-size: 1.4em; color: #ff0066"><strong>asp.net 中文件上传<br />
<br />
选择文件:<asp:FileUpload ID="filesUpload" runat="server" /><br />
<br />
图片说明:
<br />
<asp:TextBox ID="txtIntro" runat="server" Height="104px" TextMode="MultiLine" Width="209px"></asp:TextBox><br />
<br />
<br />
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="上传文件" Width="173px" /></strong></span></div> </center>
</form>
</body>处理程序段:protected void btnUpload_Click(object sender, EventArgs e)
{
///////获取上传文件的相关信息
string path = this.filesUpload.PostedFile.FileName;
string fileName = Path.GetFileName(path);
int fileLength = this.filesUpload.PostedFile.ContentLength;
if (fileLength < 1)
return;
try
{
//////////////////////////////建立数据库连接,命令
SqlConnection con = new SqlConnection("server=FIRST-FIRST\\LOVE;database=stuTest;uid=sa;password=sa");
SqlCommand cmd = new SqlCommand("insert into ImageStorage values(@name,@image,@class,@discription,@size)", con);
//////////////////////////////读取图片
Stream objectStream = this.filesUpload.PostedFile.InputStream;
byte[] byteContent = new byte[fileLength ];
objectStream.Read(byteContent, 0, fileLength );
//////////////////////////////处理参数
SqlParameter par = new SqlParameter("@name", SqlDbType.VarChar, 30);
par.Value = fileName;
cmd.Parameters.Add(par);
par = new SqlParameter("@image", SqlDbType.VarBinary , fileLength);
par.Value = byteContent;
cmd.Parameters.Add(par);
par = new SqlParameter("@class", SqlDbType.VarChar, this.filesUpload.PostedFile.ContentType.Length);
par.Value = this.filesUpload.PostedFile.ContentType;
cmd.Parameters.Add(par);
par = new SqlParameter("@discription", SqlDbType.VarChar, 100);
par.Value = this.txtIntro.Text.ToString();
cmd.Parameters.Add(par);
par = new SqlParameter("@size", SqlDbType.Int);
par.Value = this.filesUpload.PostedFile.ContentLength;
cmd.Parameters.Add(par); con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception er)
{
Response.Write(er.Message .ToString ());
}
}
运行后:
出现 :"将截断字符串或二进制数据。语句已终止"请问:1、出现这种问题的原因是什么呀?
2、数据库里设定的数据大小和SqlParameter中对应数据的大小有什么关系?设置SqlParameter中数据大小(长度)的依据是什么呢?
谢谢····
create table tb(v varchar(2))
insert tb select 'asdfas'
时就报这个错了.
v定义为2字节长,想存放asdfas6个字节数据表某个字段长度不够.
int64与bigint
int32与int
byte与tinyint
....
当然可以数据库都设为bigint,而类里面都写成int32。我的意思是它们不是必须一致。