Visualizzazione dei risultati da 1 a 10 su 11

Hybrid View

  1. #1
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    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.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    97
    Quote Originariamente inviata da oregon Visualizza il messaggio
    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.
    "perchè la vita è un brivido che vola via...è tutto un'equilibrio sopra la follia!"

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Quote Originariamente inviata da moncicci Visualizza il messaggio
    In realtà si che si possono inserire
    Non mi sono espresso completamente ... so che tecnicamente si può fare ma, come ti anticipava anche gibra, ti SCONSIGLIO vivamente dal farlo ... poi, se vuoi fare scoppiare il db dopo una settimana ... non venire qui a chiedere come rimediare ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    97
    Quote Originariamente inviata da oregon Visualizza il messaggio
    userai un'area apposita e inserirai un link al file nel DB.
    Immagino che sia questa la soluzione. Per "area apposita" intendi magari una cartella dell'hard disk? e poi il link lo devo inserire manualmente nel db (tramite magari una textbox?) ?
    Grazie
    "perchè la vita è un brivido che vola via...è tutto un'equilibrio sopra la follia!"

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,481
    Quote Originariamente inviata da moncicci Visualizza il messaggio
    Per "area apposita" intendi ..."
    Non conosco l'organizzazione e l'architettura del sistema in cui operi ma potrebbe essere una cartella di rete, una cartella locale del sistema in cui gira il programma ... insomma nel file system.

    Se conosci il path completo in cui memorizzerai il file (data una struttura a cartelle prefissata) non vedo perché doverla inserire a mano nel textbox.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.