C#.net用SqlServer2000可实现图片上传及显示,改用Oracle后,测试N次没有成功,救高手指教下面为SqlServer2000中图片上传代码:
SqlConnection cn=new SqlConnection("server=(local);database=test;uid=sa;");
Stream myStream=upLoadImg.PostedFile.InputStream;
int imgDataLen=upLoadImg.PostedFile.ContentLength;
string imgType=upLoadImg.PostedFile.ContentType;
string imgTitle=imgTitleTextBox.Text;
byte[] imgData=new byte[imgDataLen];
int n=myStream.Read(imgData,0,imgDataLen);SqlCommand cm=new SqlCommand("insert into image(imgtitle,imgtype,imgdata) values(@imgtitle,@imgtype,@imgdata)",cn);cm.Parameters.Add("@imgtitle",SqlDbType.VarChar,50).Value=imgTitle;
cm.Parameters.Add("@imgtype",SqlDbType.VarChar,50).Value=imgType;
cm.Parameters.Add("@imgdata",SqlDbType.Image).Value=imgData; cn.Open();
cm.ExecuteNonQuery();
cn.Close();最好能够写出用Oracle的完整实现代码
SqlConnection cn=new SqlConnection("server=(local);database=test;uid=sa;");
Stream myStream=upLoadImg.PostedFile.InputStream;
int imgDataLen=upLoadImg.PostedFile.ContentLength;
string imgType=upLoadImg.PostedFile.ContentType;
string imgTitle=imgTitleTextBox.Text;
byte[] imgData=new byte[imgDataLen];
int n=myStream.Read(imgData,0,imgDataLen);SqlCommand cm=new SqlCommand("insert into image(imgtitle,imgtype,imgdata) values(@imgtitle,@imgtype,@imgdata)",cn);cm.Parameters.Add("@imgtitle",SqlDbType.VarChar,50).Value=imgTitle;
cm.Parameters.Add("@imgtype",SqlDbType.VarChar,50).Value=imgType;
cm.Parameters.Add("@imgdata",SqlDbType.Image).Value=imgData; cn.Open();
cm.ExecuteNonQuery();
cn.Close();最好能够写出用Oracle的完整实现代码
存储过程例子:http://blog.csdn.net/chanet/archive/2005/01/19/259454.aspx
其中CREATE OR REPLACE DIRECTORY IMAGES AS 'C:\Oracle'; --图片目录
如果每次都要创建目录,会很麻烦,有没有别的方法可以替代啊,请指教。
{
OracleConnection cn=new OracleConnection("User id=ADMIN;Data Source=SGDC_ORA;password=123"); //oracle数据库 try
{
//上传文件
Stream myStream=upLoadImg.PostedFile.InputStream;
int imgDataLen=upLoadImg.PostedFile.ContentLength;
string imgType=upLoadImg.PostedFile.ContentType;
string imgTitle=imgTitleTextBox.Text;
byte[] imgData=new byte[imgDataLen];
int n=myStream.Read(imgData,0,imgDataLen); //操作数据库
string sqlstr="insert into image(imgtitle,imgtype,imgdata) values('"+imgTitle+"','"+imgType+"',empty_blob())";
OracleCommand cm=new OracleCommand(sqlstr,cn);
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
SavePhoto(imgTitle,imgData,imgType); //编号字段,图片字段,表名
}
catch(Exception ex)
{
throw ex;
}
}
{
OracleConnection cn=new OracleConnection("User id=ADMIN;Data Source=SGDC_ORA;password=123"); //oracle数据库
cn.Open();
try
{
OracleDataAdapter photoAdapter; //da
DataSet photoDataSet; //dataset
DataTable photoTable; //table
DataRow photoRow; //datarow photoAdapter = new OracleDataAdapter(
"SELECT imgtitle,imgdata FROM image WHERE imgtitle = '" + data_id+"'",cn);
photoDataSet= new DataSet("image");
photoAdapter.UpdateCommand = new OracleCommand
("UPDATE image SET imgdata = :vPHOTO WHERE imgtitle = :vID",cn);
photoAdapter.UpdateCommand.Parameters.Add(":vPHOTO",
OracleType.Blob, p_Blob.Length,"imgdata");
photoAdapter.UpdateCommand.Parameters.Add(":vID",
OracleType.VarChar,50,"imgtitle"); photoAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; // Configures the schema to match with Data Source
photoAdapter.FillSchema(photoDataSet, SchemaType.Source,"image"); // Fills the DataSet with 'drivers' table data
photoAdapter.Fill(photoDataSet,"image"); // Get the current driver ID row for updation
photoTable = photoDataSet.Tables["image"];
photoRow = photoTable.Rows.Find(data_id); // Start the edit operation on the current row in
// the 'drivvers' table within the dataset.
photoRow.BeginEdit();
// Assign the value of the Photo if not empty
if (p_Blob.Length != 0)
{
photoRow["imgdata"] = p_Blob;
}
// End the editing current row operation
photoRow.EndEdit(); // Update the database table 'drivers'
photoAdapter.Update(photoDataSet,"image"); }
catch(Exception e)
{
throw e;
}
}