|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
完全不一样的。.net其实我也说不太清,.net可以把他理解为跟J2EE相对的工具。c++主要做系统相关的开发你要学.net的话就应该学C#。(其实微软在.NET平台上也考虑了给C++留一个地位。ado时我们必要把一些年夜的数据工具如图片、可实行文件、视频和文档等数据存进数据库。在MSSQLServer中,这要用到Image数据范例,能够保留多达2G的数据。以下给出一个经由过程ADO.NET和MSSQLServer完成的小小的例子。
先创立一个测试数据表。
在查询剖析器中输出并实行以下语句:
Createtable[imgtable](
[imgid][int]IDENTITY(1,1)NOTNULL,
[imgname][varchar](100)COLLATEChinese_PRC_CI_ASNULL,
[imgData][image]NULL,
PRIMARYKEYCLUSTERED
(
[imgid]
)ON[PRIMARY]
)ON[PRIMARY]TEXTIMAGE_ON[PRIMARY]
这要在你所选的数据库中就多了一个名叫imgtable的表。
VS中的代码以下:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.IO;
namespaceADO_Demo
{
///<summary>
///Form1的择要申明。
///</summary>
publicclassADO_Demo:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.Buttonbutton2;
privateSystem.Windows.Forms.PictureBoxpictureBox1;
privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;
privateSystem.Windows.Forms.Buttonbutton3;
///<summary>
///必须的计划器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
publicADO_Demo()
{
//
//Windows窗体计划器撑持所必须的
//
InitializeComponent();
//
//TODO:在InitializeComponent挪用后增加任何机关函数代码
//
}
///<summary>
///清算一切正在利用的资本。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindows窗体计划器天生的代码
///<summary>
///计划器撑持所需的办法-不要利用代码编纂器修正
///此办法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.button1=newSystem.Windows.Forms.Button();
this.button2=newSystem.Windows.Forms.Button();
this.pictureBox1=newSystem.Windows.Forms.PictureBox();
this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();
this.button3=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//button1
//
this.button1.Location=newSystem.Drawing.Point(368,48);
this.button1.Name="button1";
this.button1.Size=newSystem.Drawing.Size(104,23);
this.button1.TabIndex=0;
this.button1.Text="保留图片";
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//
//button2
//
this.button2.Location=newSystem.Drawing.Point(368,120);
this.button2.Name="button2";
this.button2.Size=newSystem.Drawing.Size(104,23);
this.button2.TabIndex=1;
this.button2.Text="显现图片";
this.button2.Click+=newSystem.EventHandler(this.button2_Click);
//
//pictureBox1
//
this.pictureBox1.Location=newSystem.Drawing.Point(8,16);
this.pictureBox1.Name="pictureBox1";
this.pictureBox1.Size=newSystem.Drawing.Size(312,288);
this.pictureBox1.TabIndex=2;
this.pictureBox1.TabStop=false;
//
//openFileDialog1
//
this.openFileDialog1.FileOk+=newSystem.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
//button3
//
this.button3.Location=newSystem.Drawing.Point(368,200);
this.button3.Name="button3";
this.button3.Size=newSystem.Drawing.Size(104,23);
this.button3.TabIndex=1;
this.button3.Text="读取文件并翻开";
this.button3.Click+=newSystem.EventHandler(this.button3_Click);
//
//ADO_Demo
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(496,317);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.button3);
this.Name="ADO_Demo";
this.Text="ADO_Demo";
this.ResumeLayout(false);
}
#endregion
///<summary>
///使用程序的主出口点。
///</summary>
[STAThread]
staticvoidMain()
{
Application.Run(newADO_Demo());
}
///<summary>
///点击翻开文件对话框断定按钮,将文件保留到数据库中
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidopenFileDialog1_FileOk(objectsender,System.ComponentModel.CancelEventArgse)
{
stringfilename=this.openFileDialog1.FileName;
SqlConnectionconn=newSqlConnection("server=192.168.2.200;integratedsecurity=sspi;database=northwind");
SqlCommandcmd=newSqlCommand("insertimgtablevalues(@imgname,@imgData)",conn);
SqlParameterpm=newSqlParameter("@imgname",SqlDbType.VarChar,100);
pm.Value=filename;
SqlParameterpm1=newSqlParameter("@imgData",SqlDbType.Image);
FileStreamfs=newFileStream(filename,FileMode.Open);
intlen=(int)fs.Length;
byte[]fileData=newbyte[len];
fs.Read(fileData,0,len);
fs.Close();
pm1.Value=fileData;
cmd.Parameters.Add(pm);
cmd.Parameters.Add(pm1);
conn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
this.openFileDialog1.ShowDialog();
}
///<summary>
///从数据库中读取bitmap图片并显现
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidbutton2_Click(objectsender,System.EventArgse)
{
SqlConnectionconn=newSqlConnection("server=192.168.2.200;integratedsecurity=sspi;database=northwind");
SqlCommandcmd=newSqlCommand("select*fromimgtablewhereimgnamelike%bmp%",conn);
conn.Open();
SqlDataReaderdr;
try
{
dr=cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinarysb=dr.GetSqlBinary(2);
//或byte[]imageData=(byte[])dr[2];
MemoryStreamms=newMemoryStream(sb.Value);//在内存中操纵图片数据
Bitmapbmp=newBitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image=bmp;
dr.Close();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
///<summary>
///读取文件并保留到硬盘,然后翻开文件
///</summary>
///<paramname="sender"></param>
///<paramname="e"></param>
privatevoidbutton3_Click(objectsender,System.EventArgse)
{
SqlConnectionconn=newSqlConnection("server=192.168.2.200;integratedsecurity=sspi;database=northwind");
SqlCommandcmd=newSqlCommand("select*fromimgtablewhereimgnamelike%doc",conn);
conn.Open();
SqlDataReaderdr;
try
{
dr=cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinarysb=dr.GetSqlBinary(2);
//或byte[]imageData=(byte[])dr[2];
//FileStreamfs=newFileStream(@"C: emp.bmp",FileMode.Create);
stringfilename=@"C:"+System.IO.Path.GetFileName(dr.GetString(1));
FileStreamfs=newFileStream(filename,FileMode.Create);
fs.Write(sb.Value,0,sb.Value.Length);
fs.Close();
//this.pictureBox1.Image=Image.FromFile(@"C: emp.bmp");
System.Diagnostics.Process.Start(filename);
dr.Close();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
}
}
间接把全部文件读取到内存中的数组里关于小文件来讲是没成绩的,但假如是年夜文件,出格是巨细都凌驾了物理内存的文件,大概会招致严峻的内存成绩,必要分段读取,并分段写到数据库。
如果英语好,口才好,加上女孩子的优势说不定有机会进去做做别的工具) |
|