How to simulate a data exception with localdb - sql-server

I'm using a LocalDB database with EntityFramework on an ASP.NET MVC project.
How can I simulate a connection failure to test the following try...catch block?
try
{
if (ModelState.IsValid)
{
db.Entry(course).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes.");
}
I've tried trying to take the database offline in SQL management studio, but it just hangs. I can't stop the SQL service as LocalDB doesn't run as a service.

Have you tried giving it a non-existing name of LocalDB instance? Just put this in your connection string for this test:
Server=(localdb)\missing_instance`
(if you embed this connection string in code don't forget to escape the \ character :-))
connectionString="Server=(localdb)\\missing_instance;..."

You can stop a LocalDB instance even though it doesn't run as a service, you can use the SQLLocalDB.exe tool to do this:
SqlLocalDB Utility

Related

Deployed package execution in SSISDB shows success but write action never happened

I simplified the issue to a simple write action in C# script task, writing current time stamp into a text file located on the same machine in C:\test\ granted full control to Everyone. The environment is SQL Server 2016, Visual Studio 2015, and Windows Server 2016.
If create a SQL Server Agent job from file system, it updates the text file as expected. If deploy the package into SSISDB and execute, the execution report shows all green and success while the write action never happened. And I was not able to find any clue in system logs.
I will be very grateful to inputs on:
1) What might be wrong in this simple-write test; and
2) How to get more useful log information about this error and possibly other issues.
FYI, Related issues resolved before this post:
SSIS deployed package fails to map drive tag to network shared folder
Code in the simple-write script task.
public void Main()
{
Dts.TaskResult = (int)ScriptResults.Failure;
try
{
// TODO: Add your code here
using (var writer = new StreamWriter("C:\\test\\simple-write.txt", true, Encoding.Unicode))
{
writer.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffK"));
writer.Flush();
}
Dts.TaskResult = (int)ScriptResults.Success;
}catch(Exception ex)
{
Dts.Events.FireError(0, "Simple-Write-Test", ex.ToString(), String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
I got the answer on the following link:
https://social.msdn.microsoft.com/Forums/en-US/6c34874c-03a2-4926-8989-b977c1ec1866/deployed-package-execution-in-ssisdb-shows-success-but-write-action-never-happened?forum=sqlintegrationservices
It seems to be a compatibility issue between the latest version of Management Studio (2017 suite) and SQL Server 2016 database. The script task in the deployed package was silently ignored.

SSIS package gives error after deployment SQL Server 2012

I created a package in Visual Studio 2015. It works fine.
Basically I am using a script task that generates Excel spreadsheet and sends it to different users.
After I deploy the package to SQL Server 2012 and then try to execute it from there - I get an error without any further details.
I also run select * from internal.packages from SSISDB to make sure package_format_version is 6, which is what should be for SQL Server 2012.
What could be the problem?
This necessarily isn't an answer on how to fix the issue, but it's an answer on how you can modify your script task to get a better error message then "Script Task Failure: Exception has been thrown..."
We'll always wrap our script tasks in a try-catch and then raise the exception message back out of the script task:
public void Main()
{
try
{
//Your code here
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.Events.FireError(-1, "", ex.Message, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
It's always a challenge, especially with a deployed SSIS package, when it errors on a scrip task you don't necessarily get a clear indication as to why it's failing and you get a cryptic error message. The above code will capture what threw the exception and bubble back out to integration services what that was.
You may want to make sure that the "Microsoft Access Database Engine 2010 Redistributable" driver is installed on the SSIS server. You can get it here.

add soap webservice to IIS

I made a web service that connects to a database hosted in SQL Server 2008 R2 on my local computer, and makes some operations on it.
This is the connection string in the web service:
conn = new SqlConnection("Data Source=amir-pc\\SQLEXPRESS;User Id=sa;Password=1234; Initial Catalog=Election;Integrated Security=True");
It works well and successfully accesses the database and runs right.
Now I want to add this web service to IIS.
I successfully added it on Windows 7 to the IIS and can run it from the browser.
localhost/election_service/service.asmx
but when I tried to call a function that checks the connection, it failed, and I don't know why.
This is the function:
[WebMethod]
public string Check_conn()
{
try
{
conn.Open();
conn.Close();
return "ok";
}
catch
{
return "failed";
}
}
Is there any modification I must to do to be able to access the database?
Hi can you give more information please try change you code for know what is exception is raising.
public string dbcheck()
{
try
{
conn.Open();
conn.Close();
return "ok";
}
catch (Exception ex)
{
return ex.Message;
}
}
Hi, then .. is a login issue:Open IIS Open Application Pool Select your current application pool are you current working (by default is DefaultAppPool) Go to Advanced Options, and find Identity and change to (NT AUTHORITY\Network Service) Then you has been changed your application user, now you need to add permision to network service to database Open SQlMananger, then go to Node Security, rigth click and properties go to NT AUTHORITY\Network Service go to User mapping and add the database you want to allow access.

How to ping from within an SSIS package?

I'm trying to verify a connection through a firewall, and we are very limited in the access allowed. Ultimately, data will be loaded from an Access database on another server to our SQL Server database using an SSIS package. I can ping that source server from my computer and get a reply -- I want to put that in our SSIS package and see if the destination SQL Server can get a ping reponse.
Right now, the OLE DB source connection fails from both my computer and the server, ping works directly from my computer, and I don't know if it will work from the server or not. SSIS is the only thing I can put and run on the server, and it also what we plan on using for getting data from this other source.
Is there a fairly simple way to use a SSIS package to ping a specific address? I probably don't even need to know anything more than whether it fails or works.
I cannot use sys.sp_comdshell because that is turned off as part of our security configuration for our sql server. I cannot remote to the server and use ping directly.
You could add a Script Task to your Control Flow with some code that does the ping for you. The System.Net.NetworkInformation namespace in .NET 2.0 and up contains a Ping class that can do the work for you. I suspect it might look something like this:
public void Main()
{
using (Ping ping = new Ping())
{
try
{
PingReply reply = ping.Send(url, 100);
if (reply.Status == IPStatus.Success)
{
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
catch
{
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}

No Process Is on the Other End of the Pipe

I receive this error when I try to connect to SQL Server 2005. I have enabled TCP/IP, Named Pipes, and restarted the server but that is not working.
For me the issue was that the SQL server was in Windows Authentication mode only, even though I set it to mixed during the install.
In the object explorer, right click on the server, properties and then the Security page and set Server authentication to SQL Server and Windows Authentication mode.
FYI, I've just had the same error.
I switched to Windows authentication, disconnected, then tried to login again with SQL authentication. This time I was told my password had expired. I changed the password and it all worked again.
I tried the troubleshooting steps in both microsoft tech articles, and oddly no luck.
I managed to fix the solution by changing my authentication from SQL Server Auth to Windows Auth. Though I am not sure the technical reason why this works?
It may help to make sure the database specified in the initial catalog exists.
I got this error when I (deliberately) reduced the configuration of maximum SQL Server memory to 16Mb and restarted.
So it might be a memory issue.
I encountered this problem when the password for the login that I was attempting to connect with had expired.
One another reason for this error message could be the case when you've deleted the database your application uses, and you didn't run the following commands from your visual studio:
Add-Migration MigrationNameHere
Update-Database
I assume you have seen this:
http://technet.microsoft.com/en-us/library/ms175496.aspx
how about this?
http://blogs.msdn.com/sql_protocols/archive/2006/07/26/678596.aspx
1st check the Window's Event Log for the following error:
Could not connect because the maximum number of ’1′ user connections
has already been reached. The system administrator can use
sp_configure to increase the maximum value. The connection has been
closed.
To solve the problem do the following:
Open Microsoft SQL Server Management Studio
Open a new query
Type the under given code and press the execute button
sp_configure ‘show advanced options’, 1;
GO
reconfigure
GO
sp_configure ‘user connections’, 0;
GO
reconfigure
GO
Source: http://www.windowstechupdates.com/microsoft-sql-server-error-233-no-process-is-on-the-other-end-of-the-pipe/
In my case make sure that your connection string has ;password=
A connection was successfully established with the server, but then an error occurred
during the login process. (provider: Shared Memory Provider, error: 0 - No process is
on the other end of the pipe.) (Microsoft SQL Server, Error: 233)
This error will occur when the login does not have an active "Default Database" assigned.
In my case this occurred after taking a DB Offline. The previous DBA had assigned a non-system DB as the Default DB for a login. After that DB was taken offline, the login failed threw this error 233.
To Check & Fix this...
Login to the SQL Server Instance via SSMS using a different login.
Go to... >> Security >> Logins >> {Login Name} >> General
Check the "Default Database" is set to an active DB (I reverted back to 'master').
Logout & then try logging in again using the login that was just updated.
in my case :
it was blocked by Symantec AV and firewall
just for trial I have to disable symantec n firewall
i think i'll have further checking
If you have created the migrations, you could execute them in the Startup.cs as follows.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
}
...
This will create the database and the tables using your added migrations.
If you're not using Entity Framework Migrations, and instead just need your DbContext model created exactly as it is in your context class at first run, then you can use:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.EnsureCreated();
}
...

Resources