加急!倾分相送!ADO.NET中怎么更新sql server中的image型字段?

westaf 2001-10-13 03:58:43
其它类型的字段,如int、char等,我都能更新,就是image型的字段不能。

ado中用的是appendchunk,ado.net中没有这个东西了,而直接修改它的byte[]又不起作用。谁能帮帮我!

下面是我用的C#源代码,访问northwind数据库,其中的CategoryName字段可以通过这段程序更新,但Picture字段无论如何就是无法更新。

SqlConnection myConnection = new SqlConnection("server=(local);uid=sa;pwd=sa;database=northwind");
myConnection.Open();
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select * from Categories", myConnection);

DataSet myDataSet = new DataSet();

SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);

mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

mySqlDataAdapter.Fill(myDataSet, "Categories");

myDataSet.Tables["Categories"].Rows[0]["CategoryName"]="Peach";
byte[] buf = (byte[])myDataSet.Tables["Categories"].Rows[0]["Picture"];
buf[0] = 22;
myDataSet.Tables["Categories"].Rows[0]["Picture"] = buf;

mySqlDataAdapter.Update(myDataSet, "Categories");
myConnection.Close();
...全文
64 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccBoy 2001-10-18
  • 打赏
  • 举报
回复
但愿对你有帮助,网上资源,作者用的是一个Access数据库
---------------------------------------------------------
Imports System
Imports System.IO
Imports System.Data

Public Class SaveImage
Shared Sub main()
'Delaclare a file stream object
Dim o As System.IO.FileStream
'Declare a stream reader object
Dim r As StreamReader
Dim jpgFile As String
Console.Write("Enter a Valid .JPG file path")
jpgFile = Console.ReadLine
If Dir(jpgFile) = "" Then
Console.Write("Invalid File Path")
Exit Sub
End If
'Open the file
o = New FileStream(jpgFile, FileMode.Open, FileAccess.Read, FileShare.Read)
'Read the output in a stream reader
r = New StreamReader(o)
Try
'Declare an Byte array to save the content of the file to be saved
Dim FileByteArray(o.Length - 1) As Byte
o.Read(FileByteArray, 0, o.Length)
'Open the DataBase Connection, Please map the datasource name to match the 'Database path
Dim Con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=DbImages.mdb")
Dim Sql As String = "INSERT INTO DbImages (Pic,FileSize) VALUES (?,?)"
'Declare a OleDbCommand Object
Dim CmdObj As New System.Data.OleDb.OleDbCommand(Sql, Con)
'Add the parameters
CmdObj.Parameters.Add("@Pic", System.Data.OleDb.OleDbType.Binary, o.Length).Value = FileByteArray
CmdObj.Parameters.Add("@FileSize", System.Data.OleDb.OleDbType.VarChar, 100).Value = o.Length
Con.Open()
CmdObj.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
Console.Write(ex.ToString)
End Try
End Sub
End Class
westaf 2001-10-16
  • 打赏
  • 举报
回复
DataSet呢?我是要更新数据库呀。
你这个只是把文件读到内存来吧。
lg12net 2001-10-16
  • 打赏
  • 举报
回复
以下是我在做项目当中使用的方法,供你参考,别忘了分哟,呵呵。
try
{
string PicturePath;

PicturePath=Request.Files[0].FileName.Trim();
if (PicturePath!="")
{
if (File.Exists(PicturePath)==false)
{
ResultTxt.Text="图片不存在,请重新选择图片.";
return;
}
}
else
{
PicturePath=Server.MapPath("~/images/picture.bmp");
}
FileStream fs=File.Open(PicturePath,FileMode.Open);
byte[] buff=new Byte[fs.Length];
fs.Read(buff,0,buff.Length);
myInfo.Picture=buff;
}
catch(Exception Err01)
{
ResultTxt.Text="图片太大或者格式错误,请检查你的图片.";
return;
}
Jneu 2001-10-15
  • 打赏
  • 举报
回复
gz
westaf 2001-10-15
  • 打赏
  • 举报
回复
忘了说了,直接用DBCommand我是会的,但现在我要求用DataAdapter和DataSet来做到。

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧