WCF and MVC : Call stored procedures from web mvc application - c#

I've done a wcf service that deals with a database. In that WCF service i've created some stored procedures. That service is now accessible from my network (http: //myIP/MysService.csv/) and my stored procedures are accessible that way : http: //myIP/MysService.csv/MyProcedure?myParam=XXX.
I've a MVC4 application that is working with some local data (by local I mean a local database and a local WCF service, called that way for requests by example :
public int getClientID(string login)
{
var context = new MyLocalService.MyLocalEntityEntities(new Uri(http://localhost:12345/MyWCF.svc/));
var persons = context.PERSON.ToList();
var cli = from person in persons
where person.LOGIN == login
select person.CLIENT_ID;
int cliID = (int) cli.First();
return cliID;
}
Now, I'd like to plug the 2 : delete all the part that works with local data (var context=...) and replace it by a call to the stored procedure of my WCF service. How to do it ?
I've try to add my service using right click-> add as a service reference -> http: //myIP/MysService.csv/ for address by I cannot access to stored procedures. Is it the right way to work and if not, how to do it ? Thanks !

If you're going to use Stored Procedures then look at the SqlCommand class that will allow you to execute stored procedures on the database.
The other idea would be to consider what kind of Object-Relational Mapping tool you are using that may allow for SQL commands to be executed directly since you already seem to be using some LINQ in your code.
You'd pair the SqlCommand class with a SqlConnection class that have a direct connection to the database, presuming the stored procedure is MS-SQL Server or SQL Express where you want to execute the procedure in the DB. Unless you have a very different configuration than what I've seen, most calls to stored procedures are done by wrapping some using statements for the connection and command to run the procedure. Calling stored procedure from C# code has an example if you need it.

Related

C# Import from bacpac to Azure and add database to Elastic pool

I need to create a database in SQL Azure importing schema and data from a BACPAC file.
I use the method ImportBacPac of the DacServices object.
I see that I can specify some options using DacAzureDatabaseSpecification (Edition, Maximum size...)
I wonder if there is a way to connect the database to elastic pool while importing it from bacpac.
Thank you
Importing to a database in elastic pool isn't supported. You can import data into a single database and then move the database to an elastic pool.
Once you have it in same Logical Server. Move the DB under Elastic Pool. You can do this in C#, the Portal, Using PowerShell, or using simple query as below:
ALTER DATABASE Your_DB_Name
MODIFY ( SERVICE_OBJECTIVE = ELASTIC_POOL ( name = Your_Pool_Name) ) ;
Note: You can create multiple pools on a server, but you can't add databases from different servers into the same pool.

Get name of "parent" database from assembly

I have a SQL CLR stored procedure written in c# (.NET4). Its purpose is to allow a trigger on a table in a SQL Server 2012 database to call a web service which then processes the data in that table.
However, there are several different databases which will all have triggers using this assembly. My web service needs to know which database is triggered the call to it in order to know where to get the data from.
I could simply add a parameter to my stored procedure but I want to keep things simple from the database side. Is there any way, in .NET, to obtain information about the database to which the assembly is attached?
Aah, found one:
This still opens a context connection to the database, but it's about the only way I can see.
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
string dbName = conn.Database
}
That's from an MSDN article. Also, the MSDN article on Context Connection.

Creating MySql Database on new install of c# application if it doesnt exist

I am about to deploy my application and have came into a bit of trouble.
I have the connection string for the database held in the application.settings and need a way to check if the database exists when the program first starts up, and if it doesn't, i need the program to create it before starting the program.
I am assuming it would be a mysql statement to check if db exists, if not create. However, I don't know where or how to do this, can I create a mysql dump of a blank database with tables etc already created and use that?
I have already stored the mysql dll files locally so there is no problem with that, its just creating the database that the string wants to connect to before the application runs so there are no connection errors straight away.
Thanks.
You can do this by running the following SQL statement:
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "my_db"
If it doesn't exist from the result set you get returned you can then create it.
This does pose questions regarding MySQL permissions and if your application should have user rights that enable such checking.
Edit in response of comments.
It isn’t clear if you create the connection string or not – I’ll assume the worst and that it is a part of the setup so your client can enter it (if you do know it the process below simplifies.
I would pass the connection string to the constructor of the MySqlConnectionStringBuilder class, this then makes it easy to connect to the database using the MySqlConnection class. I would use the properties from the new instance of the MySqlConnectionStringBuilder class (Server, Host, User etc) to setup the MySqlConnection class.
If the connection didn’t work I would return information to the user and they can update their connection string.
Once I’ve successfully connected to the database I would then use the database name from the Database property of my MySqlConnectionStringBuilder instance to build the query above.
If the command returns NULL the database doesn't exist and then needs creating, if the database does exist then the command will return the name of the database.
Now there are two paths:
It Doesn't exist – It needs creating, I would probably have an external SQL file with the create statements in (can be produced by MySQL dump by using the –nodata option). I would parse this file and execute the create statements
It does exist – I would now check the structure of the database to make sure it is compatible before continuing the installation.

INSERT statement doesn't work in WEB SERVER

IN localhost insert statement WORKS PERFECTLY ( insert data in database sql management server) but from web server it doesn't ( update, delete works but not insert).
I am using sql connection , string str = insert into dtbase.dbo.candidat values ().
command cmd = new command (sql, connection)
Can someone please tell me why it doesn;t work from wb server ( I am using web application.) do i need to add some permision in web.config?
To determine if this is a permissions issue (which I think it is) or not then temporarily (this is for the down voters out there) enable ASP.Net Impersonation by using an account that you know has access to your network and SQL Server instance: http://support.microsoft.com/kb/306158
Based on the other comments, I agree that it sounds like a permissions issue.
You may be getting the error using database.dbo.table because your table was created under a different schema (ie. database.user.table) and you're trying to access that schema from a user that doesn't have permissions to that schema.
Does your connection string change from localhost to your production server?

Stored Procedure doesn't exist, or does it?

I'm having a problem:
I have a db connection where I run stored procedures on. This same connection is used to create said stored procedures earlier on.
When I attempt to call a given stored procedure, later on, I get the following message:
Could not find stored procedure
'dbo.yaf_prov_upgrade'.
The problem is it actually does exist on the database. And there's also the fact that it shows up on the SQL Server Profiler.
RPC:Completed exec
[dbo].[yaf_prov_upgrade]
#PreviousVersion=46,#NewVersion=46 .Net
SqlClient Data
Provider Nico Matrix\Nico
I was wondering what could be the causes a particular query would throw such an exception even when it exists, it's called, and the call reaches the database.
It can't be a problem with the connection because it already executed other stored procedures. It can't be a problem with the procedure because it does exist, in fact the very same application, the very same web page, created it and put it there.
Update: forgot to mention I'm used integrated security, and I did run the SP on the database with the same user the application connects with, and I had no problem running it.
So what can it be?
Your RPC completed only means that the batch submitted to SQL Server was correct and completed. It doesn't mean the stored procedure ran and executed OK.
It will be (don't argue, check) one of:
wrong permissions
wrong database context
wrong server
stored proc is in a different database
To ensure that things are the same
SELECT
##SERVERNAME,
SUSER_SNAME(),
DB_NAME(),
USER_NAME(),
OBJECT_ID('dbo.yaf_prov_upgrade')
The OBJECT_ID will be NULL if the stored proc doesn't exist in that database or you don't have permissions.
I suspect it might be a permissions issue, check up if the user name your program is executing under has execute rights to the stored proc.
I'm no expert by far on ms-sql, but I do know it keeps SPs in a global cache. Is it possible the local connection only gets the global list of SPs upon connection? Maybe reinit the connection or re-select the cache?

Categories