|
|
|
Show ภาพจากฐานข้อมูล ASP.NET + Mysql ใช้ mysql เป็นฐานข้อมูล id = intname = vacharPhoto = bl |
|
|
|
|
|
|
|
ใช้ mysql เป็นฐานข้อมูล
id = int
name = vachar
Photo = blob
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
namespace testblob
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// connect database...........................................................
MySqlConnection conn = new MySqlConnection
("server =localhost;database =surat;uid=root;password=surin2518");
//.............................................................................
MySqlDataAdapter da = new MySqlDataAdapter();
DataSet ds = new DataSet();
private void button1_Click(object sender, EventArgs e)
{
string imagename;
FileDialog fldg = new OpenFileDialog();
fldg.InitialDirectory = @"c:\";
fldg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif";
if (fldg.ShowDialog() == DialogResult.OK)
{
imagename = fldg.FileName;
Bitmap newing = new Bitmap(imagename);
pctimg.SizeMode = PictureBoxSizeMode.StretchImage;
pctimg.Image = (Image)newing;
txtPath.Text = imagename;
}
}
private void button2_Click(object sender, EventArgs e)
{
string sqlselect = "SELECT * FROM emp";
MySqlDataAdapter da = new MySqlDataAdapter(sqlselect, conn);
DataSet ds = new DataSet();
da.Fill(ds, "testimage");
int id = int.Parse(txtId.Text);
Image imageSave = pctimg.Image;
string imagename = txtPath.Text;
FileStream fls;
fls = new FileStream(@imagename, FileMode.Open, FileAccess.Read);
byte[] blob = new byte[fls.Length];
fls.Read(blob, 0, System.Convert.ToInt32(fls.Length));
fls.Close();
string sql = "INSERT INTO emp(id,name,Photo)" + "VALUES(" + id + ",'" + txtName.Text + "','" + blob + "')";
da.InsertCommand = new MySqlCommand(sql);
da.InsertCommand.Connection = conn;
DataRow newemp = ds.Tables["testimage"].NewRow();
newemp["id"] = id;
newemp["name"] = txtName.Text;
newemp["Photo"] = blob;
ds.Tables["testimage"].Rows.Add(newemp);
da.Update(ds.Tables["testimage"]);
}
private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
string sqlselect = "SELECT * FROM emp";
MySqlDataAdapter da = new MySqlDataAdapter(sqlselect, conn);
DataSet ds = new DataSet();
da.Fill(ds, "testimage");
DataTable dtable;
dtable = ds.Tables[0];
cmbPict.Items.Clear();
foreach (DataRow drow in dtable.Rows)
{
cmbPict.Items.Add(drow[0].ToString());
cmbPict.SelectedIndex = 0;
}
}
private void btnShowPicture_Click(object sender, EventArgs e)
{
string sqlselect = "SELECT * FROM emp";
MySqlDataAdapter da = new MySqlDataAdapter(sqlselect, conn);
DataSet ds = new DataSet();
da.Fill(ds, "testimage");
DataTable mydatatable = ds.Tables[0];
if (pctShow.Image != null)
{
pctShow.Image.Dispose();
}
FileStream FS1 = new FileStream("image.jpg", FileMode.Create);
foreach (DataRow datarow in mydatatable.Rows)
{
if (datarow[0].ToString() == cmbPict.SelectedItem.ToString())
{
byte[] blob = (byte[])datarow[2];
System.IO.MemoryStream stream1 = new System.IO.MemoryStream(blob, true);
FS1.Write(blob, 0, blob.Length);
FS1.Close();
pctShow.Image = Image.FromFile("image.jpg"); //ERROR บรรทัดนี้คับ Out of memory
pctShow.SizeMode = PictureBoxSizeMode.StretchImage;
pctShow.Refresh();
}
}
}
}
}
Tag : - - - -
|
|
|
|
|
|
Date :
2009-08-31 16:52:22 |
By :
surat_sukman |
View :
2147 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="MySql.Data.MySqlClient"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
OleDbConnection objConn;
OleDbCommand objCmd;
void Page_Load(object sender,EventArgs e)
{
String strConnString;
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false";
objConn = new MySqlConnection(strConnString);
objConn.Open();
BindData();
}
void BindData()
{
String strSQL;
strSQL = "SELECT * FROM category";
MySqlDataReader dtReader;
objCmd = new MySqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DataList ***//
myDataList.DataSource = dtReader;
myDataList.DataBind();
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - DataList</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList id="myDataList" RepeatColumns="2" runat="server">
<HeaderTemplate>
<b>My Category</b>
</HeaderTemplate>
<ItemTemplate>
<div style="width:100px" align="center">
<img src="<%# DataBinder.Eval(Container.DataItem, "Picture") %>">
<br />
<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>
</div>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
</form>
</body>
</html>
ดาวน์โหลดฐานข้อมูลได้ที่ (C#) ASP.NET DataList Control - Repeat Multiple Columns
|
|
|
|
|
Date :
2009-11-05 17:45:22 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|