2006-07-13

通过ADO.NET存取文件

来源: 本站收集整理 作者:佚名 评论 0 条
 
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 23);
this.button2.TabIndex = 1;
this.button2.Text = "显示图片";
this.button2.Click = new System.EventHandler(this.button2_Click);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(8, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(312, 288);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
//
// openFileDialog1
//
this.openFileDialog1.FileOk = new System.ComponentModel.CancelEventHandler(this.openFileDialog1_FileOk);
//
// button3
//
this.button3.Location = new System.Drawing.Point(368, 200);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 23);
this.button3.TabIndex = 1;
this.button3.Text = "读取文件并打开";
this.button3.Click = new System.EventHandler(this.button3_Click);
//
// ADO_Demo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.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]
static void Main()
{
Application.Run(new ADO_Demo());
}

/// <summary>
/// 点击打开文件对话框确定按钮,将文件保存到数据库中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string filename = this.openFileDialog1.FileName;
SqlConnection conn = new SqlConnection("server=192.168.2.200;integrated security = sspi;database = northwind");
SqlCommand cmd = new SqlCommand("insert imgtable values(@imgname,@imgData)",conn);
SqlParameter pm = new SqlParameter("@imgname",SqlDbType.VarChar,100);
pm.Value = filename;
SqlParameter pm1 = new SqlParameter("@imgData",SqlDbType.Image);
FileStream fs = new FileStream(filename,FileMode.Open);
int len = (int)fs.Length;
byte[] fileData = new byte[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(Exception ex)
{
MessageBox.Show(ex.Message);
共3页: 上一页 [1] 2 [3] 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:网页设计配色精彩十例  
下一篇:网页设计技巧—布局
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号