SQL Server提供了一個特別的數據類型
在SQL SERVER中建立這樣結構的一個表
列名
類型
目的
ID
Integer
主鍵ID
IMGTITLE
Varchar(
IMGTYPE Varchar(
圖片類型
為了能存儲到表中
Stream imgdatastream = File
int imgdatalen = File
string imgtype = File
string imgtitle = TextBox
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream
string connstr=((NameValueCollection)Context
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand
(
VALUES ( @imgtitle
SqlParameter paramTitle = new SqlParameter
(
paramTitle
command
SqlParameter paramData = new SqlParameter(
paramData
command
SqlParameter paramType = new SqlParameter(
paramType
command
connection
int numRowsAffected = command
connection
現在讓我們來從SQL Server中讀取我們放入的數據吧!我們將要輸出圖片到你的浏覽器上
private void Page_Load(object sender
{
string imgid =Request
string connstr=((NameValueCollection)
Context
string sql=
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql
connection
SqlDataReader dr = command
if(dr
{
Response
Response
}
connection
}
要注意的是Response
下面給大家一個用於C# Winform的存入
using System;
using System
using System
using System
using System
using System
using System
using System
namespace WindowsApplication
{
/// <summary>
/// Form
/// </summary>
public class Form
{
private System
/// <summary>
/// 必需的設計器變量
/// </summary>
private System
private string ConnectionString =
private SqlConnection conn = null;
private SqlCommand cmd = null;
private System
private System
private System
private string sql = null;
private System
private string nowId=null;
public Form
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
conn = new SqlConnection(ConnectionString);
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源
/// </summary>
protected override void Dispose( bool disposing )
{
if (conn
conn
if( disposing )
{
if (components != null)
{
components
}
}
base
}
#region Windows Form Designer generated code
/// <summary>
/// 設計器支持所需的方法
/// 此方法的內容
/// </summary>
private void InitializeComponent()
{
this
this
this
this
this
this
//
// button
//
this
this
this
this
this
this
//
// pic
//
this
this
this
this
this
//
// button
//
this
this
this
this
this
this
//
// openFileDialog
//
this
//
// label
//
this
this
this
this
//
// Form
//
this
this
this
this
this
this
this
this
this
this
this
}
#endregion
/// <summary>
/// 應用程序的主入口點
/// </summary>
[STAThread]
static void Main()
{
Application
}
private void button
{
openFileDialog
if (openFileDialog
{
FileInfo fi = new FileInfo(openFileDialog
string imgtitle=openFileDialog
int imgdatalen=(int)fi
byte[] imgdata = new byte[imgdatalen];
Stream imgdatastream=fi
int n=imgdatastream
if( conn
conn
ConnectionString =
conn
try
{
string mySelectQuery =
//string mySelectQuery=
SqlCommand myCommand = new SqlCommand(mySelectQuery
SqlParameter paramTitle = new SqlParameter(
paramTitle
myCommand
SqlParameter paramData = new SqlParameter(
paramData
myCommand
conn
int numRowsAffected = myCommand
conn
}
catch(Exception err)
{
MessageBox
}
finally
{}
}
}
private void Form
{
}
private void button
{
//打開數據庫連接
if( conn
conn
ConnectionString =
conn
// 創建數據適配器
string sql=
SqlCommand command = new SqlCommand(sql
try
{conn
catch(Exception newerr)
{
MessageBox
}
finally
{}
SqlDataReader dr = command
if(dr
{
FileInfo fi = new FileInfo(
FileStream myStream=fi
byte[] mydata=((byte[])dr[
//label
foreach(byte a in mydata)
{
myStream
}
myStream
Image myImage=Image
pic
pic
dr
}
else
{
MessageBox
}
conn
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11512.html