What's the best way to test SQL Server connection programmatically? - c#

I need to develop a single routine that will be fired each 5 minutes to check if a list of SQL Servers (10 to 12) are up and running.
Is there a way to simply "ping" a SQL Server from C# one with minimal code and sql operational requirements?

I have had a difficulty with the EF when the connection the server is stopped or paused, and I raised the same question. So for completeness to the above answers here is the code.
/// <summary>
/// Test that the server is connected
/// </summary>
/// <param name="connectionString">The connection string</param>
/// <returns>true if the connection is opened</returns>
private static bool IsServerConnected(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}

Execute SELECT 1 and check if ExecuteScalar returns 1.

See the following project on GitHub: https://github.com/ghuntley/csharp-mssql-connectivity-tester
try
{
Console.WriteLine("Connecting to: {0}", AppConfig.ConnectionString);
using (var connection = new SqlConnection(AppConfig.ConnectionString))
{
var query = "select 1";
Console.WriteLine("Executing: {0}", query);
var command = new SqlCommand(query, connection);
connection.Open();
Console.WriteLine("SQL Connection successful.");
command.ExecuteScalar();
Console.WriteLine("SQL Query execution successful.");
}
}
catch (Exception ex)
{
Console.WriteLine("Failure: {0}", ex.Message);
}

Wouldn't establishing a connection to the database do this for you? If the database isn't up you won't be able to establish a connection.

For what Joel Coehorn suggested, have you already tried the utility named tcping. I know this is something you are not doing programmatically. It is a standalone executable which allows you to ping every specified time interval. It is not in C# though. Also..I am not sure If this would work If the target machine has firewall..hmmm..
[I am kinda new to this site and mistakenly added this as a comment, now added this as an answer. Let me know If this can be done here as I have duplicate comments (as comment and as an answer) here. I can not delete comments here.]

Look for an open listener on port 1433 (the default port). If you get any response after creating a tcp connection there, the server's probably up.
You know, I first wrote this in 2010. Today, I'd just try to actually connect to the server.

public static class SqlConnectionExtension
{
#region Public Methods
public static bool ExIsOpen(
this SqlConnection connection, MessageString errorMsg = null)
{
if (connection == null) { return false; }
if (connection.State == ConnectionState.Open) { return true; }
try
{
connection.Open();
return true;
}
catch (Exception ex) { errorMsg?.Append(ex.ToString()); }
return false;
}
public static bool ExIsReady(
this SqlConnection connction, MessageString errorMsg = null)
{
if (connction.ExIsOpen(errorMsg) == false) { return false; }
try
{
using (var command = new SqlCommand("select 1", connction))
{ return ((int)command.ExecuteScalar()) == 1; }
}
catch (Exception ex) { errorMsg?.Append(ex.ToString()); }
return false;
}
#endregion Public Methods
}
public class MessageString : IDisposable
{
#region Protected Fields
protected StringBuilder _messageBuilder = new StringBuilder();
#endregion Protected Fields
#region Public Constructors
public MessageString()
{
}
public MessageString(int capacity)
{
_messageBuilder.Capacity = capacity;
}
public MessageString(string value)
{
_messageBuilder.Append(value);
}
#endregion Public Constructors
#region Public Properties
public int Length {
get { return _messageBuilder.Length; }
set { _messageBuilder.Length = value; }
}
public int MaxCapacity {
get { return _messageBuilder.MaxCapacity; }
}
#endregion Public Properties
#region Public Methods
public static implicit operator string(MessageString ms)
{
return ms.ToString();
}
public static MessageString operator +(MessageString ms1, MessageString ms2)
{
MessageString ms = new MessageString(ms1.Length + ms2.Length);
ms.Append(ms1.ToString());
ms.Append(ms2.ToString());
return ms;
}
public MessageString Append<T>(T value) where T : IConvertible
{
_messageBuilder.Append(value);
return this;
}
public MessageString Append(string value)
{
return Append<string>(value);
}
public MessageString Append(MessageString ms)
{
return Append(ms.ToString());
}
public MessageString AppendFormat(string format, params object[] args)
{
_messageBuilder.AppendFormat(CultureInfo.InvariantCulture, format, args);
return this;
}
public MessageString AppendLine()
{
_messageBuilder.AppendLine();
return this;
}
public MessageString AppendLine(string value)
{
_messageBuilder.AppendLine(value);
return this;
}
public MessageString AppendLine(MessageString ms)
{
_messageBuilder.AppendLine(ms.ToString());
return this;
}
public MessageString AppendLine<T>(T value) where T : IConvertible
{
Append<T>(value);
AppendLine();
return this;
}
public MessageString Clear()
{
_messageBuilder.Clear();
return this;
}
public void Dispose()
{
_messageBuilder.Clear();
_messageBuilder = null;
}
public int EnsureCapacity(int capacity)
{
return _messageBuilder.EnsureCapacity(capacity);
}
public bool Equals(MessageString ms)
{
return Equals(ms.ToString());
}
public bool Equals(StringBuilder sb)
{
return _messageBuilder.Equals(sb);
}
public bool Equals(string value)
{
return Equals(new StringBuilder(value));
}
public MessageString Insert<T>(int index, T value)
{
_messageBuilder.Insert(index, value);
return this;
}
public MessageString Remove(int startIndex, int length)
{
_messageBuilder.Remove(startIndex, length);
return this;
}
public MessageString Replace(char oldChar, char newChar)
{
_messageBuilder.Replace(oldChar, newChar);
return this;
}
public MessageString Replace(string oldValue, string newValue)
{
_messageBuilder.Replace(oldValue, newValue);
return this;
}
public MessageString Replace(char oldChar, char newChar, int startIndex, int count)
{
_messageBuilder.Replace(oldChar, newChar, startIndex, count);
return this;
}
public MessageString Replace(string oldValue, string newValue, int startIndex, int count)
{
_messageBuilder.Replace(oldValue, newValue, startIndex, count);
return this;
}
public override string ToString()
{
return _messageBuilder.ToString();
}
public string ToString(int startIndex, int length)
{
return _messageBuilder.ToString(startIndex, length);
}
#endregion Public Methods
}

Similar to the answer offered by Andrew, but I use:
Select GetDate() as CurrentDate
This allows me to see if the SQL Server and the client have any time zone difference issues, in the same action.

Here is my version based on the #peterincumbria answer:
using var scope = _serviceProvider.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
return await dbContext.Database.CanConnectAsync(cToken);
I'm using Observable for polling health checking by interval and handling return value of the function.
try-catch is not needed here because:

I normally do this by open a connection but I had some cases where a simple test via Open caused a AccessViolationException
using (SqlConnection db = new SqlConnection(conn))
{
db.Open(); // -- Access Violation caused by invalid Server in Connection String
}
So I did a TCP check before the open like recommanded by Joel Coehoorn. C# Code for this may be:
string targetAddress = "";
try
{
targetAddress = GetServerFromConnectionString();
IPAddress ipAddress = Dns.GetHostEntry(targetAddress).AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1433);
using (TcpClient tcpClient = new TcpClient())
{
tcpClient.Connect(ipEndPoint);
}
}
catch (Exception ex)
{
LogError($"TestViaTcp to server {targetAddress} failed '{ex.GetType().Name}': {ex.Message}");
}

Related

Singleton pattern and server connection using excel-DNA

I have an SQL database.
Then in one class I have an ExcelFunction:
[ExcelFunction(Description = "fonction de recherche")]
public static double id(string _isin)
{
double res;
res =DBSQL.Instance.getID(_isin);
return res;
}
Then in anoher class I have my connection and the creation of the singleton pattern (in order to be safe in case of multi-threading). The idea might not be clear, just ask me and I will try to explain. The point is to open a connection (using the singleton pattern), then do the request and then delete the singleton to close the connection.
Here is my code :
public class DBSQL : iAccesDB1
{
private SqlConnection MaConn = new SqlConnection("sefhgoefhouzeyf");
private static volatile DBSQL instance;
private static object syncRoot = new Object();
private DBSQL() {}
public static DBSQL Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new DBSQL();
}
}
return instance;
}
}
public void Connection()
{
MaConn.Open();
}
public void CloseConnection()
{
MaConn.Close();
}
public double getID(String _isin)
{
SqlDataReader rd;
double res = -9999;
SqlCommand cd = new SqlCommand("select cpn from tD where isin='" + _isin + "'", MaConn);
try
{
rd = cd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
res =double.Parse(rd["cpn"].ToString());
}
}
catch (Exception ex)
{
throw new Exception("1000: " + ex.Message);
}
return res;
}
}
The problem is that it does not work - in my excel cell I have the following: VALUE?
When your Excel-DNA function returns #VALUE to Excel, it probably means there was an unhandled exception.
I suggest you change your top-level function to return an 'object' which returns an error string if there is an exception, like this:
[ExcelFunction(Description = "fonction de recherche")]
public static object id(string _isin)
{
try
{
double res;
res = DBSQL.Instance.getID(_isin);
return res;
}
catch (Exception ex)
{
return "!!! ERROR: " + ex.ToString();
}
}

Sending data over a socket using parallels

I have an application that opens a socket and sends information over it. This functionality is encapsulated in a class.
I thought that it could be easy to work in parallel if I run this feature with Parallel.ForEach. In this way, each task would open/close a socket and send over it (to the same ip/port). This solution doesn't works.
Second way, I am implementing the socket operation in asynchronous way, and the behavior is that the server receives all data from first task and later the rest of data from other task. From the server side, there's no parallel behavior.
I am searching and reading information about this but I can't solve this problem.
Any ideas? Do you have a complete sample of using socket with Parallel library? Or any complete sample of using sockets in multithreading environment?
Complementary information:
I have a LINQ expression that returns a list of files. This list is processed as this:
Parallel.ForEach(AllFiles, new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism }, currentFile =>
{
ProcessFiles(currentFile);
});
...
ProcessFiles sumarized:
void ProcessFile (string currentFile)
{
MyTcpClient client = null;
try
{
var AllLinesInFiles = // LINQ Expression that returns a list of strings
int port = 23000;
client = new MyTcpServer(ip, port);
foreach (string data in AllLinesInFiles)
{
if (data.Length > 0)
{
if (!client.IsOk)
{
client.Connect(false);
if (!client.IsOk)
break;
}
client.SendMessage(tmpLine2);
}
}
}
catch (Exception ex)
{
...
}
finally
{
if ((client != null) && (client.IsOk))
client.Close();
}
}
MyTcpClient, synchronous version is:
public class MyTcpClient : IDisposable
{
private static object objLock = new object();
public int Port = ...
public IPAddress IPAddress = ...
private string _Host;
private TcpClient _client = null;
private Stream _stream = null;
static MyTcpClient()
{
}
public MyTcpClient(string PassedIPAddress, Int32 PassedPort)
{
try
{
this.IPAddress = PassedIPAddress;
this.Port = PassedPort;
}
catch (Exception ex)
{
...
}
}
public TcpClient TcpClient
{
get { return _client; }
set { _client = value; }
}
public X509Certificate Certificate
{
get { return _certificate; }
set { _certificate = value; }
}
public bool IsOk
{
get
{
return ((_client != null) && (_client.Connected));
}
}
public void Connect(bool isSecure)
{
if (IsOk == false)
{
_client = new TcpClient(this.IPAddress.ToString(), this.Port);
_stream = null;
try
{
NetworkStream networkStream = _client.GetStream();
_stream = (NetworkStream)networkStream;
}
catch (AuthenticationException ex)
{
...
}
catch (Exception ex)
{
...
}
finally
{
}
}
}
public void SendMessage(string message)
{
if (_client != null && IsOk)
{
byte[] dgram = EncodingHelper.GetEncoding().GetBytes(message);
lock (MyTcpClient.objLock)
{
_stream.Write(dgram, 0, dgram.Length);
}
}
}
public void SendMessage(byte[] dgram)
{
if (_client != null && IsOk)
{
lock (MyTcpClient.objLock)
{
_stream.Write(dgram, 0, dgram.Length);
}
}
}
public void Close()
{
this.Dispose();
}
public void Dispose()
{
if (_stream != null)
{
_stream.Close();
}
if (_client != null)
{
_client.Close();
}
}
}
And for the Asynchronous version I have used the example here http://msdn.microsoft.com/en-us/library/bew39x2a.aspx
When I use asynchronous socket client, I update ProcessFile method but, the idea is open/close the socket in this method.

What is the best way to implement a method with 2 returns?

Quite often I come across some code where I have to return a boolean to indicate whether the method finished successfully or not and a string with an error message in case of a problem.
I've implemented this in two ways.
The first consists in a class of response and all methods of all classes use this response class to communicate. Example:
public class ReturnValue {
public bool Ok;
public string Msg;
public object SomeData;
public ReturnValue() {
this.Ok = true;
this.Msg = null;
}
public ReturnValue(string Msg) {
this.Ok = true;
this.Msg = Msg;
}
public ReturnValue(object SomeData) {
this.Ok = true;
this.SomeData = SomeData;
}
public ReturnValue(bool Ok, string Msg) {
this.Ok = Ok;
this.Msg = Msg;
}
public ReturnValue(bool Ok, string Msg, object SomeData) {
this.Ok = Ok;
this.Msg = Msg;
this.SomeData = SomeData;
}
}
public class Test {
public ReturnValue DoSomething() {
if (true) {
return new ReturnValue();
} else {
return new ReturnValue(false, "Something went wrong.");
}
}
}
The second way is to have a method that stores the message in case of error and to see the message simply call this method. Example:
public class Test {
public string ReturnValue { get; protected set; }
public bool DoSomething() {
ReturnValue = "";
if (true) {
return true;
} else {
ReturnValue = "Something went wrong.";
return false;
}
}
}
There is a right way to do this?
Rather than relying on a return value to know if something succeeded, or what the error was if it didn't, C# generally relies on exceptions instead. If your program works, don't throw an exception. If it didn't work, throw an exception and include the error message in the exception thrown.
Personally, I prefer to use the ref keyword.
public bool DoSomething(ref string returnValue) {
returnValue = "something";
return true;
}
string returnValue = "";
DoSomething(ref returnValue);
// returnValue now has new value.

Entity Framework on different project - saving?

I have an established SQL Server database setup. I then generated an Entity Framework model in a Console Application to test some selecting, editing and adding to the database. All went well.
I am now moving more towards my final physical design of my WinForms and WebApp. So, I have decided to do the project in separate projects. I moved the Entity Framework to a Data project, I created a Services project, and I still have my console app as a test application (All in the same solution).
I have a ClassLib with data transfer objects to pass between my layers. So my GUI layer and Service layer don't know of the Entity Framework. I have helper methods in my EF project that convert the EF data into List etc...
Eg of a helper method:
using ClassLib;
namespace Data
{
public class PayeeDAL : EntityBase
{
public static List<PayeeDto> GetPayees()
{
var payees = (from payee in Db.payees
select payee).ToList();
List<PayeeDto> reply = new List<PayeeDto>();
foreach (var pa in payees)
{
PayeeDto p = new PayeeDto
{
PayeeId = pa.payee_id,
Name = pa.name,
Deleted = pa.deleted == null
};
reply.Add(p);
}
return reply;
}
}
}
And my data transfer object looks like this:
namespace ClassLib
{
public class PayeeDto
{
public int PayeeId { get; set; }
public string Name { get; set; }
public bool Deleted { get; set; }
}
}
So, my selection is working well with this design... but... have no idea how to handle saving.
In my console application, when the EF was available to me, I did this:
db.AddToaccount_transaction(ac);
account_transaction_line atl = new account_transaction_line
{
amount = amount,
cost_centre =
db.cost_centre.FirstOrDefault(
cc => cc.cost_centre_id == costcentreid),
sub_category =
db.sub_category.First(
sc => sc.sub_category_id == subcategoryId),
account_transaction = ac,
budget = db.budgets.FirstOrDefault(b => b.budget_id == budgetid)
};
db.AddToaccount_transaction_line(atl);
}
db.SaveChanges();
But now I don't have access to .AddTo.... and .SaveChanges... In my Console app, I'd create a parent object, and then add a few child objects... and then add the child objects to the parent object, and save.
But how would this be done in my new structure? I'm thinking I'd have a Save method in each of my helper classes... And then pass a List<> of the child objects, along with a single Parent class to the save method... and then transfor the Dtos to EF models, and then save it that way?
Is that an acceptable plan?
I only use DTO objects to transfer data from A to B. The updating, adding, removing etc., I always encapsulate in Commands (Command Pattern).
Retrieving data I do similarily with "Helper" classes.
Example of command pattern:
The base classes:
namespace Busker.Data.Commands
{
/// <summary>
/// The 'Command' abstract class
/// </summary>
public abstract class Command
{
private string message = "";
public string Message
{
get { return message; }
set { message = value; }
}
private bool success = false;
public bool Success
{
get { return success; }
set { success = value; }
}
private Validator validator = new Validator();
public Validator Validator
{
get { return validator; }
set { validator = value; }
}
private CommandStatusCode statusCode = CommandStatusCode.OK;
public CommandStatusCode StatusCode
{
get { return statusCode; }
set { statusCode = value; }
}
public LoggingLevel LoggingLevel = LoggingLevel.Debug;
//public BuskerContext BuskerContext;
public bool IsValid()
{
if (validator.Errors.Count > 0)
return false;
return true;
}
public abstract void Execute();
public void FailedSubCommand(Command cmd)
{
this.Success = cmd.Success;
this.Message = cmd.message;
}
}
}
namespace Busker.Data.Commands
{
public class Invoker
{
private Command command;
public Command Command
{
get { return command; }
set { command = value; }
}
public void SetCommand(Command command)
{
this.Command = command;
}
public virtual void ExecuteCommand()
{
if (command == null)
throw new Exception("You forgot to set the command!!");
try
{
log(this.command.GetType().Name + " starting execution ");
command.Execute();
if (!command.Success)
{
log(this.command.GetType().Name + " completed execution but failed. Message: " + command.Message + " " + command.StatusCode.ToString());
}
else
log(this.command.GetType().Name + " completed execution. Success!");
}
catch (Exception ex)
{
command.StatusCode = CommandStatusCode.Error;
Loggy.AddError("An unhandled error was caught in " + this.command.GetType().Name + ": " + ex.Message, ex);
command.Message = ex.ToString();
//throw;
}
}
private void log(string msg)
{
switch (command.LoggingLevel)
{
case Busker.Data.Constants.LoggingLevel.Debug:
Loggy.Debug(msg);
break;
case Busker.Data.Constants.LoggingLevel.Off:
break;
default:
Loggy.Add(msg);
break;
}
}
public virtual void ExecuteLinqCommand()
{
this.ExecuteCommand();
}
}
}
namespace Busker.Data.Commands
{
public static class Extensions
{
/// <summary>
/// Executes the command using the default invoker.
/// </summary>
/// <param name="aCommand"></param>
public static void Invoke(this Command aCommand)
{
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase m = stackTrace.GetFrame(1).GetMethod();
String strMethodName = m.DeclaringType.Name + "." + m.Name;
try
{
Invoker invoker = new Invoker();
invoker.SetCommand(aCommand);
invoker.ExecuteCommand();
}
catch (Exception ex)
{
Loggy.AddError("An error occured in Extensions.Invoke. + " + strMethodName ,ex);
throw ex;
}
}
}
Implementation Example:
namespace Busker.Data.Commands.Message
{
public class CreateMessageCommand :Command
{
public CreateMessageCommand (int from, int to, string title, string body)
{
// set private variable..
}
public override void Execute()
{
// Do your stuff here
be.SaveChanges();
this.Success = true;
}
}
}
Usage:
CreateMessageCommand cmd = new CreateMessageCommand (...);
//Don't use the execute method of the command
//the invoker, because implemented as an extension can be exchange in different
//environments
cmd.Invoke();

Casting Error when using serialization

I asked this question awhile ago, but didn't get a usuable answer. Basically, I can't get my method for duplicating objects to work due to invalid cast exceptions. But I can cast things fine outside of the duplication method.
Here's the duplication method
public static class ObjectDuplicator
{
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("the Type must be serializable.", "source");
}
if (Object.ReferenceEquals(source, null)) //dont try to serialize a null object
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
The problem is this: when I call this method using the code below
public void AddJob(Job job)
{
if (!Jobs.Contains(job))
{
Job newcopy = Utilities.ObjectDuplicator.Clone<Job>(job);
Jobs.Add(newcopy);
}
}
it throws this exception:
System.InvalidCastException was
unhandled Message=Unable to cast
object of type
'KH.CharacterClasses.Freelancer' to
type 'KH.CharacterClasses.Job'
Now, the type of job I'm adding is an inherited class from Job, (Freelancer) and the code for those two classes is below
[Serializable]
public class Job : Ability
{
protected JobCommand basecommand1;
protected JobCommand basecommand2;
protected JobCommand basecommand3;
protected JobCommand basecommand4;
protected JobCommand command1;
protected JobCommand command2;
protected JobCommand command3;
protected JobCommand command4;
bool mastered;
protected FFJob job;
protected string name;
int level;
public FFJob SetJob
{
get
{
return job;
}
}
public bool Mastered
{
get
{
return mastered;
}
}
public JobCommand Command1
{
get
{
return command1;
}
set
{
command1 = value;
}
}
public JobCommand DefaultCommand1
{
get
{
return basecommand1;
}
}
public JobCommand Command2
{
get
{
return command2;
}
set
{
command2 = value;
}
}
public JobCommand DefaultCommand2
{
get
{
return basecommand2;
}
}
public JobCommand Command3
{
get
{
return command3;
}
set
{
command3 = value;
}
}
public JobCommand DefaultCommand3
{
get
{
return basecommand3;
}
}
public JobCommand Command4
{
get
{
return command4;
}
set
{
command4 = value;
}
}
public JobCommand DefaultCommand4
{
get
{
return basecommand4;
}
}
public Job(string name, string description, int jobID)
: base(name, description, jobID, -1, -1, null, null, -1, -1)
{
}
public static bool operator ==(Job job1, Job job2)
{
if (System.Object.ReferenceEquals(job1, job2))
return true;
if (((object)job1 == null) || ((object)job2 == null))
return false;
return (job1.Name == job2.Name && job1.UID == job2.UID);
}
public static bool operator !=(Job job1, Job job2)
{
return !(job1 == job2);
}
// public abstract void CharacterModifier(BaseCharacter character);
// public abstract void CharacterDemodifier(BaseCharacter character);
}
[Serializable]
public class Freelancer : Job
{
public Freelancer()
: base("Freelancer", "A character not specializing in any class. Can combine the power of all mastered Jobs.", Globals.JobID.ID)
{
basecommand1 = JobCommand.Attack;
basecommand2 = JobCommand.Free;
basecommand3 = JobCommand.Free;
basecommand4 = JobCommand.Items;
command1 = basecommand1;
command2 = basecommand2;
command3 = basecommand3;
command4 = basecommand4;
job = FFJob.Freelancer;
}
}
I really don't know what the issue is. As I said, casting works fine outside of this method, and I know this code has worked before. Any ideas?
Thanks
I figured it out. At some point, I compiled it as a .dll to reference in another project. I forgot to delete the .dll from the bin directory, so the program was loading my classes from the dll, not from the new version of the code. I realized that after I tried to do a straight duplication of the same type of objct and saw it was referecing something from the .dll and from the .exe. Deleting the .dll fixed it. Silly me.
I you just want duplication, instead of serialization, why not just implement IClonable? Every type has a protected method called MemberwiseClone that eases the working involved considerably.

Categories