Browse Source

Added a little more code for the db control - working on the db object tree

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2861 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Dickon Field 18 years ago
parent
commit
ccdf9de860
  1. 45
      src/AddIns/DisplayBindings/ServerTools/DbControlController.cs
  2. 48
      src/AddIns/DisplayBindings/ServerTools/DbControlNode.cs
  3. 2
      src/AddIns/DisplayBindings/ServerTools/ICSharpCode.ServerTools.csproj
  4. 26
      src/AddIns/DisplayBindings/ServerTools/ServerControl.xaml.cs
  5. 86
      src/Libraries/ICSharpCode.DataTools/OleDbUtils.cs

45
src/AddIns/DisplayBindings/ServerTools/DbControlController.cs

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
/*
* Created by SharpDevelop.
* User: dickon
* Date: 15/01/2008
* Time: 16:47
*
*/
using System;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Data.OleDb;
using dbtool = ICSharpCode.DataTools.OleDbConnectionUtil;
namespace ICSharpCode.ServerTools
{
/// <summary>
/// Description of DbToolController.
/// </summary>
public static class DbControlController
{
private static Dictionary<string, OleDbConnection> connections =
new Dictionary<string, OleDbConnection>();
/// <summary>
/// Lookup the connection for the named db node,
/// </summary>
/// <param name="dbNode"></param>
public static void BuildDbNode(TreeViewItem dbNode, string connectionName)
{
}
public static bool TryGetConnection(string connectionName, out OleDbConnection connection)
{
if (connections.TryGetValue(connectionName, out connection)) {
return true;
} else {
// TODO: use OleDbUtils to get a connection if possible.
return false;
}
}
}
}

48
src/AddIns/DisplayBindings/ServerTools/DbControlNode.cs

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
/*
* Created by SharpDevelop.
* User: dickon
* Date: 15/01/2008
* Time: 18:15
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Controls;
namespace ICSharpCode.ServerTools
{
/// <summary>
/// Description of DbNode.
/// </summary>
public class DbControlNode : TreeViewItem
{
private DbControlNodeState state;
public DbControlNode(string name)
{
this.Name = name;
}
public DbControlNode(string name, DbControlNodeState state)
{
this.Name = name;
this.state = state;
}
public DbControlNodeState State {
get {
return this.state;
}
set {
this.state = value;
}
}
}
public enum DbControlNodeState
{
Open,
Closed
}
}

2
src/AddIns/DisplayBindings/ServerTools/ICSharpCode.ServerTools.csproj

@ -80,6 +80,8 @@ @@ -80,6 +80,8 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="DbControlNode.cs" />
<Compile Include="DbControlController.cs" />
<Compile Include="ServerControl.xaml.cs">
<DependentUpon>ServerControl.xaml</DependentUpon>
<SubType>Code</SubType>

26
src/AddIns/DisplayBindings/ServerTools/ServerControl.xaml.cs

@ -17,6 +17,7 @@ using System.Configuration; @@ -17,6 +17,7 @@ using System.Configuration;
using System.Collections.Generic;
using ICSharpCode.DataTools;
using log = ICSharpCode.Core.LoggingService;
namespace ICSharpCode.ServerTools
{
@ -33,13 +34,14 @@ namespace ICSharpCode.ServerTools @@ -33,13 +34,14 @@ namespace ICSharpCode.ServerTools
public void Refresh()
{
log.Debug("refreshing ServerControl");
log.Debug("loading oledb connection strings");
ConnectionStringSettingsCollection c
= OleDbConnectionUtil.GetConnectionSettingsCollection();
this.dbTree.Items.Clear();
foreach (ConnectionStringSettings s in c)
{
TreeViewItem n = new TreeViewItem();
//n.Name = s.Name;
n.Header = s.Name;
this.dbTree.Items.Add(n);
}
@ -59,15 +61,31 @@ namespace ICSharpCode.ServerTools @@ -59,15 +61,31 @@ namespace ICSharpCode.ServerTools
//Cast the generic object that PromptNew returns to an ADODB._Connection.
aDOcon = (ADODB._Connection)mydlg.PromptNew();
if (aDOcon == null)
{
return;
}
oleCon.ConnectionString = aDOcon.ConnectionString;
oleCon.Open();
if (oleCon.State.ToString() == "Open")
{
// TODO: add the oledb connection to the local config and
// refresh
OleDbConnectionUtil.Put("any name", oleCon.ConnectionString);
// If we get to here, we have a valid oledb
// connection string, at least on the basis of the current
// state of the platform that it refers to.
// Now construct a name for the connection string settings based on
// the attributes of the connection string and save it.
// VS08 assumes the following naming scheme:
// connection name ::= <provider name>.<host name>\<server name>.<catalog name>
string provider = oleCon.Provider;
string source = oleCon.DataSource;
string catalogue = oleCon.Database;
string dbServerName = @provider + ":" + @source + "." + @catalogue;
OleDbConnectionUtil.Put(dbServerName, oleCon.ConnectionString);
OleDbConnectionUtil.Save();
this.Refresh();
oleCon.Close();
}

86
src/Libraries/ICSharpCode.DataTools/OleDbUtils.cs

@ -25,9 +25,9 @@ namespace ICSharpCode.DataTools @@ -25,9 +25,9 @@ namespace ICSharpCode.DataTools
/// <exception cref="ArgumentException">thrown if the name of the connection is found,
/// but is not a valid oledb connection string</exception>
///
public const string CONNECTION_STRINGS_FILE = "Connections.conf";
public const string CONNECTION_STRINGS_FILE = "DatabaseConnections.xml";
private static ConnectionStringSettingsCollection cssc;
private static ConnectionStringSettingsCollection cssc = null;
public static bool TryGet(string connectionName, out OleDbConnection conn)
{
@ -46,11 +46,22 @@ namespace ICSharpCode.DataTools @@ -46,11 +46,22 @@ namespace ICSharpCode.DataTools
return false;
}
}
/// <summary>
/// Validates and adds an oledb connection string to the internal cache.
/// Note that this does not persist it - call Save() immediately after this
/// you want the connection string to be available between sessions.
/// </summary>
/// <param name="name">an arbitrary display for the connection which can be used to
/// display the connection. There can therefore be multiple instances of the same
/// connection in use.</param>
/// <param name="connectionString"></param>
public static void Put(string name, string connectionString)
{
// check that it is an oledb connection string
OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder(connectionString);
// if an exception wasn't thrown then its a valid connection string, so add it to
// the cache
ConnectionStringSettings st = new ConnectionStringSettings(name, connectionString);
cssc.Add(st);
}
@ -92,7 +103,7 @@ namespace ICSharpCode.DataTools @@ -92,7 +103,7 @@ namespace ICSharpCode.DataTools
cssc = new ConnectionStringSettingsCollection();
// Does the connections file exist - if not return an emptry collection
// Does the connections file exist - if not return an empty collection
string configPath = PropertyService.ConfigDirectory;
string filePath = Path.Combine(configPath, CONNECTION_STRINGS_FILE);
@ -106,8 +117,10 @@ namespace ICSharpCode.DataTools @@ -106,8 +117,10 @@ namespace ICSharpCode.DataTools
{
while (sr.Length > 0)
{
XmlSerializer x = new XmlSerializer(typeof(ConnectionStringSettings));
ConnectionStringSettings cs = (ConnectionStringSettings)x.Deserialize(sr);
XmlSerializer x = new XmlSerializer(typeof(ConnectionStringSettingsXMLSerializerWrapper));
ConnectionStringSettingsXMLSerializerWrapper csw
= (ConnectionStringSettingsXMLSerializerWrapper)x.Deserialize(sr);
ConnectionStringSettings cs = csw.ConnectionStringSettings;
cssc.Add(cs);
}
}
@ -128,33 +141,84 @@ namespace ICSharpCode.DataTools @@ -128,33 +141,84 @@ namespace ICSharpCode.DataTools
/// </summary>
public static void Save()
{
log.Debug("persisting oledb connection strings");
string configPath = PropertyService.ConfigDirectory;
string filePath = Path.Combine(configPath, CONNECTION_STRINGS_FILE);
try
{
log.Debug("existing file found, so deleting it to be replaced");
File.Delete(filePath);
}
catch (Exception)
{
// this should just indicate that the file does not exist
log.Info("no connection settings file found while saving - will create a new one");
log.Debug("no connection settings file found while saving - will create a new one");
}
using (Stream sw = new FileStream(filePath, FileMode.CreateNew))
{
XmlSerializer xs = new XmlSerializer(typeof(ConnectionStringSettings));
XmlSerializer xs = new XmlSerializer(typeof(ConnectionStringSettingsXMLSerializerWrapper));
foreach (ConnectionStringSettings cs in cssc)
{
try
{
xs.Serialize(sw, cs);
log.Debug("serialising as xml and storing: " + cs.ToString());
ConnectionStringSettingsXMLSerializerWrapper csw
= new ConnectionStringSettingsXMLSerializerWrapper(cs);
xs.Serialize(sw, csw);
}
catch
catch(Exception e)
{
log.Debug("failed to write ConnectionStringSettings: " + cs.ToString());
throw (e);
// TODO: do something sensible with the exception if it occurs
//log.Debug("failed to write ConnectionStringSettings: " + cs.ToString());
//log.Debug(e.StackTrace);
}
}
}
}
}
public class ConnectionStringSettingsXMLSerializerWrapper
{
private ConnectionStringSettings settings;
public ConnectionStringSettingsXMLSerializerWrapper()
{
}
public ConnectionStringSettingsXMLSerializerWrapper(ConnectionStringSettings settings)
{
this.settings = settings;
}
public string Name {
get {
return settings.Name;
}
set {
ConnectionStringSettings.Name = value;
}
}
public string ConnectionString {
get {
return settings.ConnectionString;
}
set {
ConnectionStringSettings.ConnectionString = value;
}
}
[XmlIgnore]
public ConnectionStringSettings ConnectionStringSettings {
get {
if (settings == null) {
settings = new ConnectionStringSettings();
}
return settings;
}
}
}
}

Loading…
Cancel
Save