///<summary>Inserts one ToolButItem into the database. Provides option to use the existing priKey.</summary> internal static long Insert(ToolButItem toolButItem,bool useExistingPK) { if(!useExistingPK && PrefC.RandomKeys) { toolButItem.ToolButItemNum=ReplicationServers.GetKey("toolbutitem","ToolButItemNum"); } string command="INSERT INTO toolbutitem ("; if(useExistingPK || PrefC.RandomKeys) { command+="ToolButItemNum,"; } command+="ProgramNum,ToolBar,ButtonText) VALUES("; if(useExistingPK || PrefC.RandomKeys) { command+=POut.Long(toolButItem.ToolButItemNum)+","; } command+= POut.Long (toolButItem.ProgramNum)+"," + POut.Int ((int)toolButItem.ToolBar)+"," +"'"+POut.String(toolButItem.ButtonText)+"')"; if(useExistingPK || PrefC.RandomKeys) { Db.NonQ(command); } else { toolButItem.ToolButItemNum=Db.NonQ(command,true); } return toolButItem.ToolButItemNum; }
///<summary>Inserts one ToolButItem into the database. Returns the new priKey.</summary> internal static long Insert(ToolButItem toolButItem) { if(DataConnection.DBtype==DatabaseType.Oracle) { toolButItem.ToolButItemNum=DbHelper.GetNextOracleKey("toolbutitem","ToolButItemNum"); int loopcount=0; while(loopcount<100){ try { return Insert(toolButItem,true); } catch(Oracle.DataAccess.Client.OracleException ex){ if(ex.Number==1 && ex.Message.ToLower().Contains("unique constraint") && ex.Message.ToLower().Contains("violated")){ toolButItem.ToolButItemNum++; loopcount++; } else{ throw ex; } } } throw new ApplicationException("Insert failed. Could not generate primary key."); } else { return Insert(toolButItem,false); } }
///<summary>Converts a DataTable to a list of objects.</summary> public static List<ToolButItem> TableToList(DataTable table){ List<ToolButItem> retVal=new List<ToolButItem>(); ToolButItem toolButItem; for(int i=0;i<table.Rows.Count;i++) { toolButItem=new ToolButItem(); toolButItem.ToolButItemNum= PIn.Long (table.Rows[i]["ToolButItemNum"].ToString()); toolButItem.ProgramNum = PIn.Long (table.Rows[i]["ProgramNum"].ToString()); toolButItem.ToolBar = (ToolBarsAvail)PIn.Int(table.Rows[i]["ToolBar"].ToString()); toolButItem.ButtonText = PIn.String(table.Rows[i]["ButtonText"].ToString()); retVal.Add(toolButItem); } return retVal; }
///<summary>Updates one ToolButItem in the database. Uses an old object to compare to, and only alters changed fields. This prevents collisions and concurrency problems in heavily used tables.</summary> internal static void Update(ToolButItem toolButItem,ToolButItem oldToolButItem) { string command=""; if(toolButItem.ProgramNum != oldToolButItem.ProgramNum) { if(command!=""){ command+=",";} command+="ProgramNum = "+POut.Long(toolButItem.ProgramNum)+""; } if(toolButItem.ToolBar != oldToolButItem.ToolBar) { if(command!=""){ command+=",";} command+="ToolBar = "+POut.Int ((int)toolButItem.ToolBar)+""; } if(toolButItem.ButtonText != oldToolButItem.ButtonText) { if(command!=""){ command+=",";} command+="ButtonText = '"+POut.String(toolButItem.ButtonText)+"'"; } if(command==""){ return; } command="UPDATE toolbutitem SET "+command +" WHERE ToolButItemNum = "+POut.Long(toolButItem.ToolButItemNum); Db.NonQ(command); }
///<summary>Updates one ToolButItem in the database.</summary> internal static void Update(ToolButItem toolButItem) { string command="UPDATE toolbutitem SET " +"ProgramNum = "+POut.Long (toolButItem.ProgramNum)+", " +"ToolBar = "+POut.Int ((int)toolButItem.ToolBar)+", " +"ButtonText = '"+POut.String(toolButItem.ButtonText)+"' " +"WHERE ToolButItemNum = "+POut.Long(toolButItem.ToolButItemNum); Db.NonQ(command); }
private void butOK_Click(object sender, System.EventArgs e) { if(checkEnabled.Checked && textPluginDllName.Text!=""){ string dllPath=ODFileUtils.CombinePaths(Application.StartupPath,textPluginDllName.Text); if(dllPath.Contains("[VersionMajMin]")) { Version vers = new Version(Application.ProductVersion); dllPath = dllPath.Replace("[VersionMajMin]","");//now stripped clean } if(!File.Exists(dllPath)) { MessageBox.Show(Lan.g(this,"Dll file not found:")+" "+dllPath); return; } } if(textPluginDllName.Text!="" && textPath.Text!="") { if(!MsgBox.Show(this,MsgBoxButtons.OKCancel,"If both a path and a plug-in are specified, the path will be ignored. Continue anyway?")) { return; } } ProgramCur.ProgName=textProgName.Text; ProgramCur.ProgDesc=textProgDesc.Text; ProgramCur.Enabled=checkEnabled.Checked; ProgramCur.Path=textPath.Text; ProgramCur.CommandLine=textCommandLine.Text; ProgramCur.PluginDllName=textPluginDllName.Text; ProgramCur.Note=textNote.Text; if(IsNew){ Programs.Insert(ProgramCur); } else{ Programs.Update(ProgramCur); } ToolButItems.DeleteAllForProgram(ProgramCur.ProgramNum); //then add one toolButItem for each highlighted row in listbox ToolButItem ToolButItemCur; for(int i=0;i<listToolBars.SelectedIndices.Count;i++){ ToolButItemCur=new ToolButItem(); ToolButItemCur.ProgramNum=ProgramCur.ProgramNum; ToolButItemCur.ButtonText=textButtonText.Text; ToolButItemCur.ToolBar=(ToolBarsAvail)listToolBars.SelectedIndices[i]; ToolButItems.Insert(ToolButItemCur); } DialogResult=DialogResult.OK; }