Sql Server中ntext字段如何存取图象及.EXE类型的文件,高手帮个忙 现有在sql server中有表Accessory,字段为FID(int),FFileName(varchar), FFile(ntext),FSize(char)请问如何在表中存取图片、EXE文件、文本文件等。表中字段类型不能变了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 存取图片:private void buttonFromPhoto_Click(object sender, System.EventArgs e) { string bianhao=this.textBox1.Text; if(bianhao.Length<5) { MessageBox.Show("请先选择记录(点击每行左边的灰色部分)。"); return; } bianhao=bianhao.Substring(0,5); OpenFileDialog myfile=new OpenFileDialog(); if(myfile.ShowDialog() == DialogResult.OK) { try { //根据打开的图像文件创建原始图像大小的Bitmap对象 Bitmap bitmap=new Bitmap(myfile.OpenFile()); //缩放到112像素宽,144像素高 Bitmap image=new Bitmap(bitmap,112,144); //创建内存流 MemoryStream memStream=new MemoryStream(); //将图像以jpeg格式保存到内存流中 image.Save(memStream,System.Drawing.Imaging.ImageFormat.Jpeg); //将内存流数据写入字节数组 byte[] bytes=memStream.ToArray(); //关闭内存流 memStream.Close(); //求当前行号 int num=this.dataGrid1.CurrentRowIndex; //保存字节数组到当前行的照片字段中 dataset.Tables[0].Rows[num]["照片"]=bytes; } catch(Exception err) { MessageBox.Show(err.Message); } GetImage(this.textBox1.Text.Substring(0,5)); } }private void GetImage(string str) { int num=this.dataGrid1.CurrentRowIndex; if(num==-1) { return; } if(dataset.Tables[0].Rows[num]["照片"]!=Convert.DBNull) { byte[] bytes=(byte[])dataset.Tables[0].Rows[num]["照片"]; MemoryStream memStream=new MemoryStream(bytes); Bitmap bitmap = new Bitmap(memStream); memStream.Close(); this.pictureBox1.Image = bitmap; } else { this.pictureBox1.Image = null; } } 是啊,用image类型存图象我知道,但是我现在做的是二次开发,数据表中已经有数据,如果改变字段类型,有很多地方要出错且没有源码。所以一定要用ntext类型来存取,我看了一下,原来的功能能够存进去,也能下载下来。所以我觉得一定可以的,高手们帮我一块想想吧! 要存放这种非文本数据当然得使用image或者var binary这样的类型而ntext是用于存放文本的,如果非得用这个存放,建议先用Convert.ToBase64String转为字符串 用Convert.ToBase64String转为字符串会出现“Base-64字符数组的无效长度”原因应该是当Convert.FromBase64String方法的参数的长度小于 4 或不是 4 的偶数倍时,将会抛出FormatException,但是我是要从数据库中来读取完整数据,该怎样解决呢? 如何存取图象及.EXE类型的文件,看看这里就明白了:http://www.yesky.com/20030311/1656281.shtml 我说的是如何用ntext字段类型来存取 个人觉得象这些东西用二进制存取最合适最方便的,推荐你看看http://www.kjcm.com/2012.html有详细的代码和介绍. // GetValText函数 // 得到数据库nText字段的值 // 参数: // strText 用来接收返回值(字段值) // strFieldName 字段名,该字段数据类型必须是nText类型 BOOL GetValText(CString& strText,CString& strFieldName); //得到数据库nText字段的值 关于如何在asp中读写SQL Server的nTEXT类型字段兄弟可以参考一下http://xitoufang.com/asppost11/web228497.htm 关于异步的问题,没分了,帮忙看下 如何实现校内网写日志的功能? 关于socket编程的问题 怎样编程实现改变textbox中某个字符的颜色和判断textbox框能容纳的字符个数 C# 执行DOS命令后为什么不生成指定文件 c#winForm用gradeview自定义日历控件 郁闷,这样的代码为什么会报异常呢? 请教:C#单击鼠标弹出web上打印机窗口 请问,做个 点对点聊天系统 用什么语言好? C#中 自定义类与byte[] 如何相互转换 C#小问题 .NET中服务器管理器中找不到服务器
private void buttonFromPhoto_Click(object sender, System.EventArgs e)
{
string bianhao=this.textBox1.Text;
if(bianhao.Length<5)
{
MessageBox.Show("请先选择记录(点击每行左边的灰色部分)。");
return;
}
bianhao=bianhao.Substring(0,5);
OpenFileDialog myfile=new OpenFileDialog();
if(myfile.ShowDialog() == DialogResult.OK)
{
try
{
//根据打开的图像文件创建原始图像大小的Bitmap对象
Bitmap bitmap=new Bitmap(myfile.OpenFile());
//缩放到112像素宽,144像素高
Bitmap image=new Bitmap(bitmap,112,144);
//创建内存流
MemoryStream memStream=new MemoryStream();
//将图像以jpeg格式保存到内存流中
image.Save(memStream,System.Drawing.Imaging.ImageFormat.Jpeg);
//将内存流数据写入字节数组
byte[] bytes=memStream.ToArray();
//关闭内存流
memStream.Close();
//求当前行号
int num=this.dataGrid1.CurrentRowIndex;
//保存字节数组到当前行的照片字段中
dataset.Tables[0].Rows[num]["照片"]=bytes;
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
GetImage(this.textBox1.Text.Substring(0,5));
}
}
private void GetImage(string str)
{
int num=this.dataGrid1.CurrentRowIndex;
if(num==-1)
{
return;
}
if(dataset.Tables[0].Rows[num]["照片"]!=Convert.DBNull)
{
byte[] bytes=(byte[])dataset.Tables[0].Rows[num]["照片"];
MemoryStream memStream=new MemoryStream(bytes);
Bitmap bitmap = new Bitmap(memStream);
memStream.Close();
this.pictureBox1.Image = bitmap;
}
else
{
this.pictureBox1.Image = null;
}
}
而ntext是用于存放文本的,如果非得用这个存放,建议先用Convert.ToBase64String转为字符串
http://www.yesky.com/20030311/1656281.shtml
http://www.kjcm.com/2012.html
有详细的代码和介绍.
// 得到数据库nText字段的值
// 参数:
// strText 用来接收返回值(字段值)
// strFieldName 字段名,该字段数据类型必须是nText类型
BOOL GetValText(CString& strText,CString& strFieldName); //得到数据库nText字段的值 关于如何在asp中读写SQL Server的nTEXT类型字段兄弟可以参考一下
http://xitoufang.com/asppost11/web228497.htm