Windows Application tutorial using c#:Part 1:Uploading a File and store it in db

Akash VG
Akash VG
28.7 هزار بار بازدید - 7 سال پیش - Pls subscribe my channelHere i'm
Pls subscribe my channel




Here i'm gonna show you how to create a db and how to connect to it, how to use OpenFileDialog and copy the file to the project location
and store the file details to the database

source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace upload_image_and_retrive
{
   public partial class Form1 : Form
   {
       SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\akash\documents\visual studio 2012\Projects\upload image and retrive\upload image and retrive\Database1.mdf");
       SqlCommand cmd;
       public Form1()
       {
           InitializeComponent();
       }


       private void button1_Click_1(object sender, EventArgs e)
       {
           DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
           if (result == DialogResult.OK) // Test result.
           {
               string file = openFileDialog1.FileName;

               string[] f = file.Split('\\');
               // to get the only file name
               string fn = f[(f.Length) - 1];
               string dest = @"C:\Users\Akash\Documents\visual studio 2012\Projects\upload image and retrive\upload image and retrive\images\" + fn;

               //to copy the file to the destination folder
               File.Copy(file, dest, true);

               //to save to the database
               string q = "insert into [data_file] values('" + fn + "','" + dest + "')";
               cmd = new SqlCommand(q, con);
               con.Open();
               cmd.ExecuteNonQuery();
               MessageBox.Show("success");
           }

       }
   }
}
7 سال پیش در تاریخ 1396/02/08 منتشر شده است.
28,710 بـار بازدید شده
... بیشتر