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

preview_player
Показать описание
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
{
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");
}

}
}
}
Рекомендации по теме
Комментарии
Автор

Thanks for sharing, this was very useful for me.

MuneerAlwan
Автор

sir, , , it works, , where was the part 2 or 3?

johnjeffreymacalde
Автор

But all you do is giving path according to our machine. Let say i did a program with access database and gave somebody as executable. will it work in these people's computers bcs each person has different paths ??? Access database i put its own debug directory so i have no problem with it to run every computer but I need default path for every PC to copy files to my debug directory. How we can solve it ?

seckinseckin
welcome to shbcf.ru