Saturday 2 June 2012

Retrieve an Image from SQL Server in Picture Box Using VB.Net

Hi, There
Retrieving an image from sql server database is so simple just do following steps
  • Make a project and database which i mention in my Blog Take a look 
  • Add a picture box on your form, and a button for show image
  • On Show Button Click event paste following code

Friday 1 June 2012

Save an Image in SQL Server Database With VB.Net

Hi
Saving Image is an important part of any software,They're many ways to handling image.One easy and secure way is save your image in sql server database.If you want to save image in sql server then follow these steps
  • Make a database and a table with following description my database name is bsw and table name is stu
Database Table Structure

VB Project Controls Which you have to use
  •  You have to import 2 name space
    Imports System.IO
    Imports System.Data.SqlClient
  • Add OpenFileDialog control
  • And make a global string variable named str
    • On Browse picture button click paste following code
        OpenFileDialog1.ShowDialog()
         str = OpenFileDialog1.FileName
On Click event of save button paste following code


Dim con As New SqlConnection("initial catalog=bsw;data source=.;integrated security=true") Dim com As New SqlCommand()
Try
            Dim fs As New        FileStream(str,FileMode.Open)
            Dim data() As Byte = New [Byte](fs.Length) {}
            fs.Read(data, 0, fs.Length)
            fs.Close()
            'readed image
            Dim roll As New SqlParameter("roll", SqlDbType.Int)
            roll.Value = Val(TextBox1.Text)

            Dim name As New SqlParameter("name", SqlDbType.Char, 10)
            name.Value = TextBox2.Text

            Dim city As New SqlParameter("city", SqlDbType.Char, 10)
            city.Value = TextBox3.Text

            Dim img As New SqlParameter("img", SqlDbType.Image)
            img.Value = data
            'Adding parameters
            com.Parameters.Add(roll)
            com.Parameters.Add(name)
            com.Parameters.Add(city)
            com.Parameters.Add(img)
            con.Open()
            com.Connection = con
            com.CommandText = "insert into stu values(@roll,@name,@city,@img)"
            com.ExecuteNonQuery()
            MsgBox("Saved")
            Me.StuTableAdapter.Fill(Me.BswDataSet1.stu)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Tuesday 21 February 2012

Complete menu based java database program for read,write,delete,update


hi folks
this is complete menu based program  java database read write.You'll have to set odbc driver which i have mention in java database connection blog
paste following code in your editor compile and run.....

import java.sql.*;
import java.io.*;
class dataoperation
{
        String nm,ct,db,cls;
        int rl;
        Connection con=null;      
        DataInputStream da=new DataInputStream(System.in);
        Statement stmt=null;
        dataoperation()
        {
                cls="sun.jdbc.odbc.JdbcOdbcDriver";
                db="jdbc:odbc:mydata";
                try
                {
                        Class.forName(cls);
                        con=DriverManager.getConnection(db,"","");
                        stmt=con.createStatement();
                }
                catch(Exception ex)
                {
                        System.out.println("Error : "+ex.getMessage());
                }
               
        }
        void write()
        {
                try
                {
                     
                        System.out.print("Enter Roll ");
                        rl=Integer.parseInt(da.readLine());
                        System.out.print("Enter name  ");
                        nm=da.readLine();
                        System.out.print("Enter city ");
                        ct=da.readLine();
                      
                        stmt.executeUpdate("insert into stu values("+rl+",'"+nm+"','"+ct+"')");
                        System.out.println("\nInserted");
                      
                }
                catch(Exception ex)
                {
                    System.out.println("Error : "+ex.getMessage());
                }
               
        }
        void delete()
        {
            System.out.println("Enter Roll ");
           
            try
            {
                rl=Integer.parseInt(da.readLine());
                stmt.executeUpdate("delete from stu where roll="+rl);
            }
            catch(Exception ex)
            {
                System.out.println("Error : "+ex.getMessage());
            }
        }
        void update()
        {
            try
            {
                System.out.print("Enter roll ");
                rl=Integer.parseInt(da.readLine());
                System.out.print("Enter Name ");
                nm=da.readLine();
                System.out.print("Enter City ");
                ct=da.readLine();
                stmt.executeUpdate("update stu set name='"+nm+"', city='"+ct+"' where roll="+rl);
               // con.close();
                System.out.print("\nRecord Updated");
            }
            catch(Exception ex)
            {
                System.out.print("\nError"+ex.getMessage());
            }
        }
        void print()
        {
            System.out.println("\n\n********* OUTPUT *************");
            try
            {
                ResultSet rlt=stmt.executeQuery("select * from stu");
                while(rlt.next())
                {
                    System.out.print("\t"+rlt.getString(1)+" "+rlt.getString(2)+" "+rlt.getString(3)+"\n");
                }
            }
            catch(Exception ex)
            {
                System.out.print("\nError : "+ex.getMessage());
            }
            System.out.println("\n****************************\n\n");
        }
}


class dbcomplete
{
   
   
    public static void main(String[] args) throws Exception
    {
       int opt;
       DataInputStream da=new DataInputStream(System.in);
       dataoperation ob=new dataoperation();
       do
       {
           System.out.println("****************************************");
           System.out.print("\n1. Add\n2. Delete\n3. Update\n4. Print\n0. Exit\n****************************************");
           System.out.print("\nEnter Your choice ");
           opt=Integer.parseInt(da.readLine());
           switch(opt)
           {
               case 1:
                    ob.write();
                    break;
               case 2:
                   ob.delete();
                   break;
               case 3:
                   ob.update();
                   break;
               case 4:
                   ob.print();
                   break;
               case 0:
                   break;
               default:
                   System.out.print("\nInvalid choice");
           }
          
       }while(opt!=0);
    }
   
}

Thursday 16 February 2012

How to delete record from Ms Access in Java








rajkumar9795@gmail.com
for delete a record in access follow instruction as mention in file connection blog