
Originariamente inviata da
oregon
Non tanto chiaro ... che file vuoi aggiungere ad un'anagrafica?
E comunque, i file non li puoi inserire nel DB ... userai un'area apposita e inserirai un link al file nel DB.
In realtà si che si possono inserire, ho trovato un codice online ma non so come collegarlo ai record già salvati nel mio db.
Ti posto il codice (che, come progetto singolo, funziona):
codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace mioprogetto
{
public partial class Form6 : Form
{
SqlConnection objConn = new SqlConnection();
string strSqlConn = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\miopercorso\Database1.mdf;Integrated Security=True";
string strQuery_AllAttachments = "SELECT ID,marchiocomm,ragsoc,sedelegale,indirizzo,cap,citta,prov,nazione,sedeoperativa,fileName,fileSize from localita order by marchiocomm";
string strQuery_GetAttachmentById = "select * from [localita] where [id] = @attachId";
string strQuery_AllAttachments_AllFields = "select * from [localita]";
DataTable localitaDT = new DataTable();
public Form6()
{
InitializeComponent();
//prevent resize at runtime
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "SQL file upload/download example";
}
private void Form6_Load(object sender, EventArgs e)
{
objConn.ConnectionString = strSqlConn; //set connection params
FillDataGrid(gridViewMain, strQuery_AllAttachments);
}
private void ConnectToDb()
{
//objConn.ConnectionString = strSqlConn; //set our connection params
//objConn.Open(); //open connection
}
private void Form6_FormClosing(object sender, FormClosingEventArgs e)
{
//objConn.Close(); //close connection
}
private void FillDataGrid(DataGridView objGrid, string strQuery)
{
//DataTable tbl1 = new DataTable();
SqlDataAdapter adapter1 = new SqlDataAdapter();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = objConn; // use connection object
cmd1.CommandText = strQuery; // set query to use
adapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey; //grab schema
adapter1.SelectCommand = cmd1; //
adapter1.Fill(localitaDT); // fill the data table as specified
objGrid.DataSource = localitaDT; // set the grid to display data
}
private void btnAddFile_Click(object sender, EventArgs e)
{
if (ofdMain.ShowDialog() != DialogResult.Cancel)
{
CreateAttachment(ofdMain.FileName); //upload the attachment
}
FillDataGrid(gridViewMain, strQuery_AllAttachments); // refresh grid
}
private void CreateAttachment(string strFile)
{
SqlDataAdapter objAdapter = new SqlDataAdapter(strQuery_AllAttachments_AllFields, objConn);
objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder objCmdBuilder = new SqlCommandBuilder(objAdapter);
DataTable objTable = new DataTable();
FileStream objFileStream = new FileStream(strFile, FileMode.Open, FileAccess.Read);
int intLength = Convert.ToInt32(objFileStream.Length);
byte[] objData;
objData = new byte[intLength];
DataRow objRow;
string[] strPath = strFile.Split(Convert.ToChar(@"\"));
objAdapter.Fill(objTable);
objFileStream.Read(objData, 0, intLength);
objFileStream.Close();
objRow = objTable.NewRow();
//objRow["marchiocomm"] = strPath[strPath.Length - 1];
objRow["fileName"] = strPath[strPath.Length - 1]; //clip the full path - we just want last part!
objRow["fileSize"] = intLength / 1024; // KB instead of bytes
objRow["attachment"] = objData; //our file
objTable.Rows.Add(objRow); //add our new record
objAdapter.Update(objTable);
}
private void btnDownloadFile_Click(object sender, EventArgs e)
{
SaveAttachment(sfdMain, gridViewMain);
FillDataGrid(gridViewMain, strQuery_AllAttachments); // refresh grid
}
private void SaveAttachment(SaveFileDialog objSfd, DataGridView objGrid)
{
string strId = objGrid.SelectedRows[0].Cells["id"].Value.ToString();
if (!string.IsNullOrEmpty(strId))
{
SqlCommand sqlCmd = new SqlCommand(strQuery_GetAttachmentById, objConn);
sqlCmd.Parameters.AddWithValue("@attachId", strId);
SqlDataAdapter objAdapter = new SqlDataAdapter(sqlCmd);
DataTable objTable = new DataTable();
DataRow objRow;
objAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(objAdapter);
objAdapter.Fill(objTable);
objRow = objTable.Rows[0];
byte[] objData;
objData = (byte[])objRow["attachment"];
if (objSfd.ShowDialog() != DialogResult.Cancel)
{
string strFileToSave = objSfd.FileName;
FileStream objFileStream = new FileStream(strFileToSave, FileMode.Create, FileAccess.Write);
objFileStream.Write(objData, 0, objData.Length);
objFileStream.Close();
}
}
}
}
}
Questo codice funziona e mi aggiunge una riga col file allegato, io vorrei poter aggiungere il file agli altri record esistenti.