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

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