اینم یه روش دیگه هستش که هر پسوندی رو تو دیتابیس ذخیره میکنه
namespace Attachment
{
public partial class Form1 : Form
{
private static string conected = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+Application.StartupPath+@"\DbTT.accdb";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
textBox1.Text = dlg.FileName;
dlg.Title = "انتخاب فایل";
}
private void button2_Click(object sender, EventArgs e)
{
FileStream fs = File.OpenRead(textBox1.Text);
byte[] fileBinData = new byte[fs.Length];
fs.Read(fileBinData, 0, fileBinData.Length);
string source = conected;
// string insert = "INSERT INTO tbluser (info) VALUES (@File)";
OleDbConnection sqlConn = new OleDbConnection(source);
OleDbCommand cmd = new OleDbCommand("Insert Into tbluser (info)Values(@File)", sqlConn);
OleDbParameter sqlParam = new OleDbParameter("@File", OleDbType.VarBinary);
sqlParam.Value = fileBinData;
cmd.Parameters.Add(sqlParam);
sqlConn.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
MessageBox.Show("Successfully Saved.");
sqlConn.Close();
}
}
}
توسط
Dany
(7,164 امتیاز)
●4 ●7 ●141