Search This Blog

Friday, January 20, 2012

How to re-seed identity columns

DBCC CHECKIDENT (yourtable, reseed, 34)

Get identity column seed values

Writing a routine to fetch identity column seed values for tables in a SQL 2008 database
Looking to import data into a database and I need to allow room when I do insert with identity_insert on

                using (SqlConnection conn = new SqlConnection(mMRISConnConfig.ConnectionString))
                {
                    string mrisSQL = @"SELECT IDENT_SEED(TABLE_NAME) AS Seed,
                               IDENT_INCR(TABLE_NAME) AS Increment,
                               IDENT_CURRENT(TABLE_NAME) AS Current_Identity,
                               TABLE_NAME
                               FROM INFORMATION_SCHEMA.TABLES
                               WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1
                               AND TABLE_TYPE = 'BASE TABLE'
                               AND TABLE_NAME in ('W_PATIENT','W_REFERRING_PHYSICIAN','W_REFERRING_PHYSICIAN_LOCATION')";

                    conn.Open();
                    SqlCommand cmd = new SqlCommand(mrisSQL, conn);
                    SqlDataReader reader = cmd.ExecuteReader();
}

Wednesday, January 18, 2012

Using ShowHelp instead of HtmlHelp

My ongoing project is rewriting a database application I first wrote in MS Access 95 to VB6 and now into C#.
97% of the conversion is finally done except for the help file.

I am using HelpNDoc to author a chm help file and I was using the following VB6 code to open topics by topicID


Option Explicit

Public Declare Function HtmlHelp Lib "hhctrl.ocx" Alias "HtmlHelpA" _
        (ByVal hwndCaller As Long, ByVal pszFile As String, _
        ByVal uCommand As Long, ByVal dwData As Long) As Long

Global Const HH_DISPLAY_TOPIC = &H0
Global Const HH_SET_WIN_TYPE = &H4
Global Const HH_GET_WIN_TYPE = &H5
Global Const HH_GET_WIN_HANDLE = &H6

' Display string resource ID or text in a popupwin.
Global Const HH_DISPLAY_TEXT_POPUP = &HE
' Display mapped numeric value in dwdata
Global Const HH_HELP_CONTEXT = &HF
' Text pop-up help, similar to WinHelp's HELP_CONTEXTMENU
Global Const HH_TP_HELP_CONTEXTMENU = &H10
' Text pop-up help, similar to WinHelp's HELP_WM_HELP
Global Const HH_TP_HELP_WM_HELP = &H11

Calling HtmlHelp:

Private Sub cmdHelp_Click()
    Call HtmlHelp(0&, gHelpFile, HH_HELP_CONTEXT, HELP_BookWindow)
End Sub

So to keep my helpfile structure I needed to be able to open help topics by topicID in C#
Here is how to call ShowHelp using topicIDs.
(Needed to invoke ToString() on the int variable to get the value to pass as an object).


public static void OpenHelp(Form currentForm, Int32 helpTopicID)
{
            try           
            {
                System.Windows.Forms.Help.ShowHelp(currentForm, _helpFile, HelpNavigator.TopicId, helpTopicID.ToString());
            }
            catch(Exception ex)
            {
                DRCCommon.Common.ErrorLog(ex);
            }
}

Monday, January 9, 2012

How to drop linked Server from SQL Server 2008

Another task I can't seem to remember.

Two useful system stored procedures showing linked servers in SQL 2005/2008


exec sp_helpserver
exec sp_linkedservers

I had trouble removing a linked server because I forgot to remove all of the logins.
(seems like removing a linked server should cascade remove the associated logins)


exec sp_droplinkedsrvlogin 'FUSIONRIS','sa'
exec sp_droplinkedsrvlogin 'FUSIONRIS',NULL
exec sp_dropserver 'FUSIONRIS'