Powered By Blogger

Thursday, December 24, 2009

upload image to database (image format) in MS-Sql server and MY-SQL

MS-SQL server
create table:


create table imgtab(idd int identity(1,1), imgsrc image)


aspx.vb


Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream
' intImageSize = PersonImage.PostedFile.ContentLength
intImageSize = fup1.PostedFile.ContentLength
strImageType = fup1.PostedFile.ContentType
ImageStream = fup1.PostedFile.InputStream
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
Dim myCommand As New SqlCommand("insert into
imgtab values ( @imgsamgetimage )'", Conn)
myCommand.Parameters.Add("@imgsamgetimage", SqlDbType.Image, ImageContent.Length).Value = ImageContent
myCommand.ExecuteNonQuery()
Conn.Close()


MY-SQL Server:

Create Table:

 CREATE TABLE `cust_file` (              
`id` int(11) NOT NULL auto_increment,
`customer_id` int(11) default NULL,
`filename` varchar(255) default NULL,
`filedata` blob,
`contenttype` varchar(255) default NULL,
`length` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1


C#:

 public void Mysql_File_Save(string sConnString, int nCustId, byte[] bData, string sName, string sContentType, int nContentLength)
{
using (MySql.Data.MySqlClient.MySqlConnection oConn =
new MySql.Data.MySqlClient.MySqlConnection(sConnString))
{
oConn.Open();

MySql.Data.MySqlClient.MySqlCommand oCommand = oConn.CreateCommand();
oCommand.Connection = oConn;


//Add new
oCommand.CommandText = "insert into cust_file(customer_id, filename, filedata, contenttype, length) " +
"values( ?in_customer_id, ?in_filename, ?in_filedata, ?in_contenttype, ?in_length)";
//oCommand.CommandType = CommandType.StoredProcedure;

MySql.Data.MySqlClient.MySqlParameter oParam =
oCommand.Parameters.Add("?in_customer_id",
MySql.Data.MySqlClient.MySqlDbType.Int64);
oParam.Value = nCustId;
oParam = oCommand.Parameters.Add("?in_filename",
MySql.Data.MySqlClient.MySqlDbType.VarChar, 255);
oParam.Value = sName;
oParam = oCommand.Parameters.Add("?in_filedata",
MySql.Data.MySqlClient.MySqlDbType.Blob);
oParam.Value = bData;
oParam = oCommand.Parameters.Add("?in_contenttype",
MySql.Data.MySqlClient.MySqlDbType.VarChar, 255);
oParam.Value = sContentType;
oParam = oCommand.Parameters.Add("?in_length",
MySql.Data.MySqlClient.MySqlDbType.Int64);
oParam.Value = nContentLength;

oCommand.ExecuteNonQuery();
oConn.Close();

}
}

Anyway it allows us to write code such as:
 byte[] bData = FileToArray("f:\\n1jpg.jpg");
Mysql_File_Save("Database=sakila;Data Source=192.168.10.4;User id=stefan;Password=pekka",
1, bData, "N1jpg", MimeType("f:\\n1jpg.jpg"), bData.Length);

Source:

http://www.programmingado.net/c-27/a-141/Insert-blob-into-MySQL.aspx

http://bytes.com/topic/asp-net/answers/342898-how-store-retrieve-array-bytes-mysql

http://www.dotnetspider.com/forum/194010-save-retrive-image-from-to-mysql-database-using-asp-net-c-language.aspx



Thumbnail photos in gridview using handler file

thumnail.aspx


<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" Visible="False" Width="50%">

<Columns>
<asp:TemplateField HeaderText="Absentees">
<ItemTemplate>
<asp:CheckBox ID="chk_absentees2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="20px"
ImageUrl='<%# "Handler_Student.ashx?id=" & Eval("stud_id") %>' Width="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="stud_name" HeaderText="Name"
SortExpression="stud_name" />
<asp:BoundField DataField="stud_id" HeaderText="Student ID"
SortExpression="stud_id" />
<asp:BoundField DataField="Course" HeaderText="Course"
SortExpression="Course" />
<asp:BoundField DataField="department" HeaderText="Department"
SortExpression="department" />
<asp:BoundField DataField="stud_year" HeaderText="Year"
SortExpression="stud_year" />
</Columns>
<HeaderStyle HorizontalAlign="Left" />
</asp:GridView>


thumnail.aspx.vb


Public Sub bind_year()

str = Convert.ToString(System.DateTime.Now.ToShortDateString())
If List_dept.Visible = False Then
If List_year.SelectedIndex = -1 Then
Else

lbl_status.Visible = True
btn_absent.Visible = True
lbl_status.Text = List_course.SelectedItem.Text & " " & List_year.SelectedItem.Text & " Year Student Details."

passquery = "select stud_id,stud_name,course,department,stud_year from student_details where course='" & List_course.SelectedValue.ToString() & "' and stud_year='" & List_year.SelectedItem.Text & "' and Joined_in='" & branchid & "' and stud_id not in(select studid from student_attendance where att_date = '" + str + "')"
Call DBConnect(Conn)
Obj.Pr_Bind_Grid(Conn, passquery, GridView1, "No Details available")
If GridView1.Rows.Count = 0 Then
btn_absent.Visible = False
lbl_grid.Visible = True
End If
End If
Else
If List_dept.Text = "" Then
lbl_status.Visible = False
GridView1.Visible = False
btn_absent.Visible = False
Response.Write("<script>alert ('Select Department')</script>")
Else
If List_year.Visible = False Then
Else
If List_year.SelectedIndex = -1 Then
Else


'List_year.SelectedIndex = 0
lbl_status.Visible = True
btn_absent.Visible = True
lbl_status.Text = List_course.SelectedItem.Text & " (" & List_dept.SelectedItem.Text & ") " & List_year.SelectedItem.Text & " Year Student Details."
passquery = "select stud_id,stud_name,course,department,stud_year from student_details where course='" & List_course.SelectedValue.ToString() & "' and department='" & List_dept.SelectedValue.ToString() & "' and stud_year='" & List_year.SelectedItem.Text & "' and Joined_in='" & branchid & "' and stud_id not in(select studid from student_attendance where att_date = '" + str + "')"
Call DBConnect(Conn)
Obj.Pr_Bind_Grid(Conn, passquery, GridView1, "No Details available")
If GridView1.Rows.Count = 0 Then
btn_absent.Visible = False
lbl_grid.Visible = True
End If
End If
End If

End If

End If
GridView1.DataBind()
Conn.Close()
End Sub


Handler_Student.ashx


<%@ WebHandler Language="VB" Class="Handler_Student" %>

Imports System
Imports System.Web
Imports System.Data.SqlClient
Imports System.Configuration

Public Class Handler_Student : Implements IHttpHandler
Dim con As New SqlConnection
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Try
Call DBConnect(con)
Dim sql As String = "Select photo from student_details where stud_id=@staff_id"
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.Add("@staff_id", System.Data.SqlDbType.VarChar, 50).Value = context.Request.QueryString("id")
cmd.Prepare()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
context.Response.BinaryWrite(DirectCast(dr("photo"), Byte()))
Catch ex As Exception

End Try
End Sub

Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property

End Class