Click here to Skip to main content
16,007,687 members
Home / Discussions / C#
   

C#

 
GeneralRe: Import old DLL to C# .Net Pin
Vimalsoft(Pty) Ltd1-Dec-09 3:48
professionalVimalsoft(Pty) Ltd1-Dec-09 3:48 
GeneralRe: Import old DLL to C# .Net Pin
Dave Kreskowiak1-Dec-09 4:33
mveDave Kreskowiak1-Dec-09 4:33 
AnswerRe: Import old DLL to C# .Net Pin
puromtec11-Dec-09 5:25
puromtec11-Dec-09 5:25 
QuestionCall Stored Procedure Using C# Pin
Phil Saville1-Dec-09 0:07
Phil Saville1-Dec-09 0:07 
AnswerRe: Call Stored Procedure Using C# Pin
Ashfield1-Dec-09 0:16
Ashfield1-Dec-09 0:16 
AnswerRe: Call Stored Procedure Using C# Pin
Vimalsoft(Pty) Ltd1-Dec-09 3:50
professionalVimalsoft(Pty) Ltd1-Dec-09 3:50 
AnswerRe: Call Stored Procedure Using C# Pin
PIEBALDconsult1-Dec-09 4:17
mvePIEBALDconsult1-Dec-09 4:17 
AnswerRe: Call Stored Procedure Using C# Pin
Mark Graham1-Dec-09 5:54
Mark Graham1-Dec-09 5:54 
Here's a helper class I use. It's lightweight but contains a few useful methods. Create a new class in your project and call it 'DbAssist' and copy this code in there. I haven't documented the methods so if you have trouble understanding anything just reply and I will assist. Cheers

using System;
using System.Configuration;
using System.Data;

namespace Common.Data {

    public static class DbAssist {

        private static void CheckConnectionClosed(IDbConnection connection) {
            if (connection.State != ConnectionState.Closed) {
                throw new InvalidOperationException("An error has occurred: connection is not in closed state.");
            }
        }

        // Connection help

        public static string GetConnectionString(string key) {
            try {
                return ConfigurationManager.ConnectionStrings[key].ConnectionString;
            } catch (NullReferenceException ex) {
                throw new ConfigurationErrorsException("An error has occurred: "
                    + key + " does not exist in config.", ex);
            }
        }

        // Scalar help

        public static object ExecScalar(string sqlText, IDbConnection connection) {
            return ExecScalar(sqlText, connection, CommandType.Text);
        }

        public static object ExecScalar(string sqlText, IDbConnection connection, CommandType cmdType) {
            return ExecScalar(sqlText, connection, cmdType, null);
        }

        public static object ExecScalar(string sqlText, IDbConnection connection,
                CommandType cmdType, IDataParameter[] parameters) {
            CheckConnectionClosed(connection);
            using (connection) {
                connection.Open();
                IDbCommand command = connection.CreateCommand();
                command.CommandText = sqlText;
                command.CommandType = CommandType.StoredProcedure;
                if (parameters != null) {
                    foreach (IDataParameter p in parameters) {
                        command.Parameters.Add(p);
                    }
                }
                return command.ExecuteScalar();
            }
        }

        // IDataReader help

        public static IDataReader ExecReader(string sqlText, IDbConnection connection) {
            return ExecReader(sqlText, connection, CommandBehavior.CloseConnection);
        }

        public static IDataReader ExecReader(string sqlText, IDbConnection connection,
            CommandBehavior behavior) {
            return ExecReader(sqlText, connection, CommandType.Text, behavior);
        }

        public static IDataReader ExecReader(string sqlText, IDbConnection connection,
                CommandType cmdType, CommandBehavior behavior) {
            CheckConnectionClosed(connection);
            connection.Open();
            IDbCommand command = connection.CreateCommand();
            command.CommandText = sqlText;
            command.CommandType = cmdType;
            return command.ExecuteReader(behavior);
        }

        public static IDataReader ExecReader(string sqlText, IDbConnection connection,
                CommandType cmdType, IDataParameter[] parameters, CommandBehavior behavior) {
            CheckConnectionClosed(connection);
            IDbCommand command = connection.CreateCommand();
            command.CommandText = sqlText;
            command.CommandType = cmdType;
            if (parameters != null) {
                foreach (IDataParameter p in parameters) {
                    command.Parameters.Add(p);
                }
            }
            return command.ExecuteReader(behavior);
        }

        public static T GetDataFromReader<T>(IDataReader reader, string fieldName, T fallbackValue) {
            int ordinal = reader.GetOrdinal(fieldName);
            if (!reader.IsDBNull(ordinal)) {
                return (T)reader.GetValue(ordinal);
            }
            return fallbackValue;
        }
    }
}



QuestionWrite DWORD Value in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer on Vista Pin
Sunil G 330-Nov-09 23:17
Sunil G 330-Nov-09 23:17 
AnswerRe: Write DWORD Value in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer on Vista Pin
Saksida Bojan30-Nov-09 23:25
Saksida Bojan30-Nov-09 23:25 
GeneralRe: Write DWORD Value in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer on Vista Pin
Sunil G 330-Nov-09 23:44
Sunil G 330-Nov-09 23:44 
GeneralRe: Write DWORD Value in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer on Vista Pin
Calla1-Dec-09 0:10
Calla1-Dec-09 0:10 
QuestionDateTimePicker Pin
Saksida Bojan30-Nov-09 22:11
Saksida Bojan30-Nov-09 22:11 
AnswerRe: DateTimePicker Pin
Blue_Boy30-Nov-09 22:17
Blue_Boy30-Nov-09 22:17 
GeneralRe: DateTimePicker Pin
Saksida Bojan30-Nov-09 22:27
Saksida Bojan30-Nov-09 22:27 
GeneralRe: DateTimePicker Pin
Blue_Boy30-Nov-09 22:36
Blue_Boy30-Nov-09 22:36 
GeneralRe: DateTimePicker Pin
Saksida Bojan30-Nov-09 22:48
Saksida Bojan30-Nov-09 22:48 
GeneralRe: DateTimePicker Pin
Blue_Boy30-Nov-09 23:48
Blue_Boy30-Nov-09 23:48 
GeneralRe: DateTimePicker Pin
Saksida Bojan1-Dec-09 1:04
Saksida Bojan1-Dec-09 1:04 
GeneralRe: DateTimePicker Pin
J$1-Dec-09 7:10
J$1-Dec-09 7:10 
GeneralRe: DateTimePicker Pin
Saksida Bojan1-Dec-09 10:04
Saksida Bojan1-Dec-09 10:04 
QuestionHow to merge and split cells in datagridview? Pin
sususan30-Nov-09 19:20
sususan30-Nov-09 19:20 
AnswerRe: How to merge and split cells in datagridview? Pin
Abhijit Jana30-Nov-09 20:17
professionalAbhijit Jana30-Nov-09 20:17 
AnswerRe: How to merge and split cells in datagridview? Pin
souidi abderrahman30-Nov-09 20:56
souidi abderrahman30-Nov-09 20:56 
GeneralRe: How to merge and split cells in datagridview? Pin
dan!sh 30-Nov-09 21:24
professional dan!sh 30-Nov-09 21:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.