How to prevent this exception network related instance from display?

  • Thread starter Thread starter engahmedbarbary
  • Start date Start date
E

engahmedbarbary

Guest
problem

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL

I need to prevent this echo from display and on same time when connection have problem then stop and when connection available then reconnect without close console app and reopen again

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using ConsoleApp1;

namespace ConsoleApplication1
{
class Program
{
public static string connection = @"Server=xxxxx;Database=xxxx;User Id=xxxx;Password=xxxx;";
static void Main(string[] args)
{

FileValidation();
}
public static void FileValidation()
{
SqlConnection con = new SqlConnection(connection);

while (true)
{
//---------------- Get FilePending
DataTable DTRowData = new DataTable();
DTRowData = GetRowData();
//-----------------------------------------------
foreach (DataRow Row in DTRowData.Rows)
{
Console.WriteLine("Start Working In File : " + Row[0].ToString());

con.Open();
using (SqlCommand cmd = new SqlCommand("SP_FilesRunValidationOnFile2", con))
{

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FileID", SqlDbType.VarChar).Value = Row[0];
cmd.ExecuteNonQuery();

Console.WriteLine("File Number : " + Convert.ToString(Row[0]) + " Validated Successfully");
con.Close();

}
}

System.Threading.Thread.Sleep(10000);
}
}

public static DataTable GetRowData()
{
DataTable dt;
SqlConnection con = new SqlConnection(connection);
con.Open();
using (SqlCommand cmd = new SqlCommand("sp_FilesGetPending", con))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
}

}
}
=====================End cs============================================
Sql
=======================================================================
Create proc [dbo].[sp_FilesGetPending]
as
select Top 10 FileID from Files With (Nolock) where FileStatusID=0
=======================================================================

Create proc [dbo].[SP_FilesRunValidationOnFile2]
@FileID int
as
declare @FileStartTime DateTime =getdate()

declare @ValidationDuration int =datediff (second , @FileStartTime , getdate() )


Update Files set FileStatusID = 1 , [FileDuration]=@ValidationDuration where FileID=@FileID


CREATE TABLE [dbo].[Files](
[FileID] [int] IDENTITY(1,1) NOT NULL,
[FileName] [nvarchar](500) NULL,
[FileStatusID] [int] NULL,
[FileDuration] [int] NULL,
CONSTRAINT [PK_TblFile] PRIMARY KEY CLUSTERED
(
[FileID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Continue reading...
 
Back
Top