我用SQLserver建了一个表,表名是News,表中的一个属性是Image,想用来存储图片。
请问 insert语句该怎么写呢?Image的值是图片的路径吗?
另外在页面中显示的时候,该用什么控件显示呢?是Image控件吗?
解决方案 »
- FileUpload的文本框如何保持不变
- 我的JVSCRIPT的$为什么不能用呢
- 简单问题请教:StreamWriter 可以将文件生成在服务器端,并提示客户端下载吗?
- 怎么在子窗体中将父窗体刷新?急急急!!!
- 讨论:csdn这个论坛是采用什么技术呢?UP有分
- 遍历页面控件时出错
- 数据库里面是数字型,取到Grivew里面要变成字符型,该怎么弄?
- (Lostinet)迷失网络-闪电论坛 - 0.0.4.2 测试版发布(只放两天)
- 比如我想把c:\my.jpg的文件上传到服务器中,但不通过文件选择框,应该怎么做
- 谁对ASP.NET中的Table控件的操作熟悉?
- 关于页面输出图片有缓存的问题.请见过的高手进来指点一下,在线急等..
- 頁面繼承後台代碼問題。
不过现在通常只存文件路径
Stream imgage = UploadFile.PostedFile.InputStream;
//文件大小
int l = UploadFile.PostedFile.ContentLength;
string type = UploadFile.PostedFile.ContentType;
byte[] imgdata = new byte[l];
int n = imgage.Read(imgdata,0,l);
//SqlConnection对象
SqlConnection conn = new SqlConnection("Server=.;Uid=sa;Pwd=;DataBase=pubs");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO ImageStore(IMGData,IMGType,IMGTitle) VALUES(@img,@type,@name)";
//参数FileData
cmd.Parameters.Add("@img",SqlDbType.Image);
cmd.Parameters["@img"].Value = imgdata;
cmd.Parameters["@img"].Direction = ParameterDirection.Input;
//参数FileType
cmd.Parameters.Add("@type",SqlDbType.NVarChar,50,"ImageType");
cmd.Parameters["@type"].Value = type;
cmd.Parameters["@type"].Direction = ParameterDirection.Input;
//参数FileName
cmd.Parameters.Add("@name",SqlDbType.NVarChar,50);
cmd.Parameters["@name"].Value = getFileName(UploadFile.Value);
cmd.Parameters["@name"].Direction = ParameterDirection.Input;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
conn.Dispose();
我这里再贴一个ORACLE的写法,参考一下.//上传
if(myFile.ContentLength != 0)
{
try
{
System.Web.HttpPostedFile myFile = this.Request.Files[0];
// string tmpFileName = myFile.FileName;
// string myFileName = tmpFileName.Substring(tmpFileName.LastIndexOf("."));
// string myFileMimeType = myFile.ContentType();
// myFile.SaveAs(this.Server.MapPath("../" + myFileName)); //读取到数组里面
System.IO.Stream mystream = myFile.InputStream;
byte[] Buffer = new byte[myFile.ContentLength];
mystream.Read(Buffer,0,myFile.ContentLength); //打开数据库
OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
cn.Open(); //用参数方式写入数据库
OracleCommand myComm = cn.CreateCommand();
string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)";
myComm.CommandText = sql;
myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer;
myComm.ExecuteNonQuery();
}
catch
{
//此处可加错误显示
}
finally
{
cn.Close();
}
}