Aug 20, 2014

Reading a tab delimited text file into DataTable

Reading a tab delimited text file into DataTable


DataTable records = new DataTable("records");
var textReader = new TextFieldParser(@"E:\CSV.txt");
using (textReader)
{
    textReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
    textReader.Delimiters = new String[] { "\t" };

    List currentRow = new List();

    bool IsFirst = true;
    int rowCount = 0;
    while (!textReader.EndOfData)
    {
        try
        {
            Console.WriteLine("reading line: " + (rowCount++));
            currentRow = textReader.ReadFields().ToList();
            if (IsFirst)
            {
                IsFirst = false;
                //add columns
                foreach (string col in currentRow)
                {
                    records.Columns.Add(col);
                }
            }
            else
            {
                DataRow row = records.NewRow();
                for (int i = 0; i < currentRow.Count; i++)
                {
                    row[i] = currentRow[i];
                }
                records.Rows.Add(row);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}


PS: You need to add reference to Microsoft.VisualBasic also import the namespace using Microsoft.VisualBasic.FileIO;

Set value for max user connections

Get value for max user connections per server:

SELECT * FROM sys.configurations
where name = 'user connections'


Set value for max user connections:

EXEC sys.sp_configure N'show advanced options', N'1'  RECONFIGURE WITH OVERRIDE
GO
EXEC sys.sp_configure N'user connections', N'0'
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sys.sp_configure N'show advanced options', N'0'  RECONFIGURE WITH OVERRIDE
GO
 

PS:  you have to connect your SSMS with admin privilages,
You can set the value of max user connections to 0 
when you get the error "provider: Shared Memory Provider, 
error: 0 - No process is on the other end of the pipe." 

Get Number of connections per database

Get Number of connections per database

SELECT DB_NAME(dbid) AS DBName,
       COUNT(dbid)   AS NumberOfConnections,
       loginame      AS LoginName,
       nt_domain     AS NT_Domain,
       nt_username   AS NT_UserName,
       hostname      AS HostName
FROM   sys.sysprocesses
WHERE  dbid > 0
GROUP  BY dbid,
          hostname,
          loginame,
          nt_domain,
          nt_username
ORDER  BY NumberOfConnections DESC;