求助大哥大姐,帮帮小妹好不好啦

rose2004 2003-12-26 09:38:06
小妹想在SQL2000里放個人相片... 我也已經把圖片轉換為了數組...可我如何把這些數組更新到數据庫中啊...
看過一個是"如何将一个二进制的byte数组存入string中" 的論壇...但我還是沒有看懂...而且它的數据庫中相片欄位到底怎么定義的也不清楚哦...
有誰能仔細告訴我,用你們精良代碼貼出來,好么?

....在線等待一天的時間....
...全文
21 点赞 收藏 30
写回复
30 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
earlsen 2003-12-26
用到picturebox控件。以下是完整的代码(函数)
txtorderid是一个文本框内容,就是更新数据表的条件ID。
app.sqldb.cn 用cn代替就可以了。
SAVePIc是我定义的函数名称,即:
'从数据库中加载图片到图片框
Private Function ShowPic() As Boolean
Dim dtpic As New DataTable()
Dim dapic As SqlDataAdapter
Try
dtpic.Clear()
Me.PicBox.Image = Nothing
Dim picsql As String = "select orderimage from productorder where productno=@bm"
Dim piccmd As New SqlCommand(picsql, Cn)
With piccmd
.Parameters.Add(New SqlParameter("@bm", SqlDbType.VarChar, 15)).Value() = Trim(txtOrderID.Text)
End With
If State = ConnectionState.Closed = True Then Open()
dapic = New SqlDataAdapter(piccmd)
dapic.Fill(dtpic)

If dtpic.Rows.Count = 0 Then Exit Function
'If dtpic.Rows(0)("orderimage") Is System.DBNull.Value And dtpic.Rows(0)("orderimage") = 0 Then Exit Function
'
If IIf(IsDBNull(dtpic.Rows(0)("orderimage")), "", dtpic.Rows(0)("orderimage")) Is System.DBNull.Value Then Exit Function
If IIf(IsDBNull(dtpic.Rows(0)("orderimage")), "0", dtpic.Rows(0)("orderimage")).length < 10 Then Exit Function
Dim arrPicture() As Byte = CType(dtpic.Rows(0)("orderimage"), Byte())
Dim ms As New MemoryStream(arrPicture)
With PicBox
.Image = Image.FromStream(ms)
End With

ShowPic = True
Catch e As SqlException
MessageBox.Show(e.Message, e.Source)
ShowPic = False
Finally
回复
earlsen 2003-12-26
提示一些什么?你直接列出来吗。
回复
sdewen2003 2003-12-26
hao ....help up!!!!
回复
rose2004 2003-12-26
幫我一把吧....路過的好心人.....

不然幫我UP也好啊....UP也有分哦....
回复
rose2004 2003-12-26
TO earlsen(earlsen) 大大:

我試了...少了許多...
但還是有几個說要我定義的...就是樓上的提示也...怎么辦呀....
回复
sdewen2003 2003-12-26
哦...那為什么我還會踫到這樣的提示呢???
形別"MemoryStream"未定義
名稱"txtOrderID"未宣告
" App." 未宣告
" SavePic" 未宣告
回复
czg1997 2003-12-26
'bytPic()为字节数组,cn为SqlConnection
Dim cmd as new sqlcommand
cmd.commandtext="Insert into Table Values(" & vbcrlf & _
"@FileName," & vbcrlf & _
"@Pic)"
cmd.connection=cn
cmd.parameters.add("@FileName", sqldbtype.binary).value = bytPic
cmd.parameters.add("", sqldbtype.text).value = ""
cmd.executenonquery()
回复
earlsen 2003-12-26
要引用下面:
Imports System.Data
Imports System.Data.SqlClient

没有用到任何的SQL2000触發器之类的东西。就是上面的代码。就是cn,是定义数据库连接的。
数据库图片字段为 image 类型 16位
回复
rose2004 2003-12-26
是啊...請您做答吧...
小妹要著急死了哦.......
回复
sdewen2003 2003-12-26
那您的后台是如何設定的呢??? 比如相片一欄位?? 定義成什么類型的呢???
回复
sdewen2003 2003-12-26
TO earlsen(earlsen) 大哥:
您的這些代碼中有很多提示說" 型別 SqlCommand 未定義" 是什么意思啊???
還有很多這個提示呢...

好像您的這段代碼要和SQL2000裏的触發器有關系是嗎?
回复
sdewen2003 2003-12-26
呵呵...同病相憐...
我也想知道這個問題啊......
幫妳UP吧...
回复
earlsen 2003-12-26
这是用VB.NET写的,保存图片到数据库的函数。已经用了很久了,没出现问题。
Try
Dim picsql As String = "update products set orderimage=@orderimage where productno=@productno"
Dim piccmd As New SqlCommand(picsql, Cn) 'Cn 数据库连接 SQL2000的数据库
With piccmd
.Parameters.Add(New SqlParameter("@productno", SqlDbType.VarChar, 15)).Value() = Trim(txtOrderID.Text)
If Me.PicBox.Image Is Nothing Then ‘判断图片是否为空
.Parameters.Add(New SqlParameter("@orderimage", SqlDbType.Image)).Value = System.DBNull.Value
Else
Dim ms As New MemoryStream()
PicBox.Image.Save(ms, PicBox.Image.RawFormat.Jpeg)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
.Parameters.Add(New SqlParameter("@orderimage", SqlDbType.Image)).Value = arrImage
End If
End With
If App.SQLDB.Cn.State = ConnectionState.Closed = True Then App.SQLDB.Cn.Open()
piccmd.ExecuteNonQuery()
MsgBox("图片操作成功!", MsgBoxStyle.Information)
Catch E As SqlException
MessageBox.Show(E.Message, E.Source, MessageBoxButtons.OK, MessageBoxIcon.Information)
SavePic = False
End Try
回复
rose2004 2003-12-26
等待各位黑俠客的指點....
小妹要急哭了....
回复
rose2004 2003-12-26
哇...不會吧...謝謝妳了,可我要用VB.NET 語法啊...而且妳的代碼是不是太長了哦...小妹看不懂啊....
回复
angxain 2003-12-26
C#的

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.SqlClient;

namespace WindowsApplication21
{
/// <summary>
/// Form1 篕璶?
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.IContainer components;
private string ConnectionString = "Integrated Security=SSPI;Initial Catalog=;Data Source=localhost;";
private SqlConnection conn = null;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.PictureBox pic1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;


public Form1()
{
//
// Windows 怠蔨??竟や┮ゲ惠
//
InitializeComponent();
conn = new SqlConnection(ConnectionString);

//
// TODO:  InitializeComponent ?ノ睰ヴ疼硑ㄧ??
//
}

/// <summary>
/// 睲瞶┮Τタㄏノ?方
/// </summary>
protected override void Dispose( bool disposing )
{
if (conn.State == ConnectionState.Open)
conn.Close();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );

}

#region Windows Form Designer generated code
/// <summary>
/// ??竟や┮惠よ猭 - ぃ璶ㄏノ???竟э
/// よ猭?甧
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.pic1 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.label2 = new System.Windows.Forms.Label();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(264, 48);
this.button1.TabIndex = 0;
this.button1.Text = "穝?";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pic1
//
this.pic1.Location = new System.Drawing.Point(280, 8);
this.pic1.Name = "pic1";
this.pic1.Size = new System.Drawing.Size(344, 264);
this.pic1.TabIndex = 3;
this.pic1.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(0, 104);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(264, 40);
this.button2.TabIndex = 4;
this.button2.Text = "??誹?い蝋?钩";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// openFileDialog1
//
this.openFileDialog1.Filter = "\"?钩ゅン(*.jpg,*.bmp,*.gif)|*.jpg|*.bmp|*.gif\"";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 152);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(264, 48);
this.label2.TabIndex = 5;
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 208);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(64, 21);
this.textBox1.TabIndex = 6;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(632, 445);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.label2,
this.button2,
this.pic1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// ?ノ祘?
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog ();

if (openFileDialog1.FileName.Trim()!="")
{
FileInfo fi = new FileInfo(openFileDialog1.FileName);
string imgtitle=openFileDialog1.FileName;
int imgdatalen=(int)fi.Length;
byte[] imgdata = new byte[imgdatalen];
Stream imgdatastream=fi.OpenRead();
int n=imgdatastream.Read(imgdata,0,imgdatalen);
if( conn.State == ConnectionState.Open)
conn.Close();
ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"Data Source=localhost;";
conn.ConnectionString = ConnectionString;

try
{
string mySelectQuery = "INSERT INTO ImageStore(imgdata) valueS (@imgdata )";

SqlCommand myCommand = new SqlCommand(mySelectQuery, conn);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.value = imgdata;
myCommand.Parameters.Add( paramData );

conn.Open();
int numRowsAffected = myCommand.ExecuteNonQuery();
conn.Close();

MessageBox.Show("Θ??誹?い");
}
catch(Exception err)
{
MessageBox.Show("眤???誹?い┪????琩!"+err.ToString() );
}
finally
{}


}

}



private void button2_Click(object sender, System.EventArgs e)
{
if( conn.State == ConnectionState.Open)
conn.Close();
ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"Data Source=localhost;";
conn.ConnectionString = ConnectionString;
string sql="SELECT * FROM ImageStore where id="+textBox1.Text.Trim() ;
SqlCommand command = new SqlCommand(sql, conn);
try
{
conn.Open();
}
catch
{
MessageBox.Show(" ぃゴ??誹?钡!") ;
}


SqlDataReader dr = command.ExecuteReader();

if(dr.Read())
{

byte[] mydata=((byte[])dr["imgdata"]);
MemoryStream myStream=new MemoryStream();
foreach(byte a in mydata)
{
myStream.WriteByte(a);
}
Image myImage=Image.FromStream(myStream);
myStream.Close();
pic1.Image=myImage;
pic1.Refresh();

}
else
{
MessageBox.Show("?ΤΘ??誹!") ;

}
conn.Close();
}
}
}
回复
啊维 2003-12-26
up
回复
rose2004 2003-12-26
小妹想在SQL2000里放個人相片... 我也已經把圖片轉換為了數組...可我如何把這些數組更新到數据庫中啊...
看過一個是"如何将一个二进制的byte数组存入string中" 的論壇...但我還是沒有看懂...而且它的數据庫中相片欄位到底怎么定義的也不清楚哦...
有誰能仔細告訴我,用你們精良代碼貼出來,好么?

....在線等待一天的時間....
回复
rose2004 2003-12-26
我的問題沒有完,已經再次發出貼子,如果有會的好心人,請進.......
http://expert.csdn.net/Expert/topic/2606/2606268.xml?temp=.4879877
回复
aidy 2003-12-26
把这两行代码加到代码编辑器的最顶上
Imports System.Data
Imports System.Data.SqlClient
带@的单词是SQL语句中的参数啊,查一下资料吧
回复
加载更多回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2003-12-26 09:38
社区公告
暂无公告