Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

inserting directory structure into SQL

$
0
0

Im trying to insert a directory and its subdirectories into SQL , its working great but i cant figure out how to insert the filenames of the files inside the subdirectory...

below is my complete code

private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo root = new DirectoryInfo(@"D:\website\AIO_GO");
            using (DirectoryTreeLoader loader = new DirectoryTreeLoader())
            {
                loader.Load(root);
            }

            return;
          
        }

Class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Data.SqlTypes;

namespace CCMM
{
    class DirectoryTreeLoader : IDisposable
    {
        const string connectionString = "Server=.;Database=WAM;Trusted_Connection=True;";
        private SqlConnection Connection;
        private SqlCommand Command;

        private SqlParameter ParentDirectoryId;
        private SqlParameter DirectoryName;

        public DirectoryTreeLoader()
        {
            Connection = new SqlConnection(connectionString);
            Command = Connection.CreateCommand();
            ParentDirectoryId = new SqlParameter("@parent_id", SqlDbType.Int, 4);
            DirectoryName = new SqlParameter("@name", SqlDbType.VarChar, 256);

            ParentDirectoryId.IsNullable = true;

            DirectoryName.IsNullable = false;

            Command.Parameters.Add(ParentDirectoryId);
            Command.Parameters.Add(DirectoryName);
            Command.CommandType = CommandType.Text;
            Command.CommandText = @"
      insert dbo.directory ( parent_id , name ) values ( @parent_id , @name ) ;
      select id = scope_identity() ;".Trim();

            return;
        }

        public void Load(DirectoryInfo root)
        {
            if (Connection.State == ConnectionState.Closed)
            {
                Connection.Open();
                Command.Prepare();
            }

            Visit(null, root);

            return;
        }

        private void Visit(int? parentId, DirectoryInfo dir)
        {
            // insert the current directory
            ParentDirectoryId.SqlValue = parentId.HasValue ? new SqlInt32(parentId.Value) : SqlInt32.Null;
            DirectoryName.SqlValue = new SqlString(dir.Name);

            object o = Command.ExecuteScalar();
            int id = (int)(decimal)o;

            // visit each subdirectory in turn
            foreach (DirectoryInfo subdir in dir.EnumerateDirectories())
            {
                Visit(id, subdir);
            }

            return;
        }

        public void Dispose()
        {
            if (Command != null)
            {
                Command.Cancel();
                Command.Dispose();
                Command = null;
            }
            if (Connection != null)
            {
                Connection.Dispose();
                Connection = null;
            }
            return;
        }
    }
}

any help ?


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>