Wednesday, July 7, 2010

Insert Any File Type to the SQL Server table

Here I used table named doc with 2 columns and data types are varbinary(max), int.

Coding Part

            SqlConnection myconn = new SqlConnection(@"Data Source=192.168.2.4;Initial Catalog=pubs;Persist Security Info=True;User ID=vidu;Password=123");

            try
            {
                myconn.Open();

                byte[] bytearr = File.ReadAllBytes(textBox1.Text);

                int i = 1;
                //I have 2 columns in my table ->Column type varbinary(max),int
                string query = "INSERT INTO doc values(@file1,@no)";

                using(SqlCommand comm=new SqlCommand(query,myconn))
                {
                    comm.Parameters.Add(new SqlParameter("@file1", bytearr));

                    comm.Parameters.Add(new SqlParameter("@no", i));

                    comm.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Download Source Code
http://www.4shared.com/file/spPyvhwW/FormInterface.html

No comments: