Scenario:
I have programmatically created a Database on Master using an appropriate connection string and added tables to it. But when I tried to access it to create a stored procedure I cannot as it says, it does not have access to perform any action on DB.
I used this query with C# and tried to create the stored procedure.
//Connection String ("Server=localhost;Integrated security=SSPI;database=master") => For Createing Database
//Connection String ("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\MyAppDB.mdf;Integrated Security=True") => For trying to create stored procedure on already created Local DB
string AccountEmail = "CREATE PROCEDURE [MyAppDB].[dbo].[usp_AccountEmail]( #Email varchar(max)) AS SELECT Username FROM [dbo].[User] WHERE Email = #Email";
SqlCommand mySP = new SqlCommand(AccountEmail, sqlcon);
using [MyAppDB].[dbo].[usp_AccountEmail] resulted in Exception of specifying the database name. But when I tried the same like [dbo].[usp_AccountEmail] it executed and created a stored procedure on master but that's not what I want, I want it to be created on localDB.
The Problem:
I want to add a stored procedure to the already created database and I want to use the DB without installing any SQL Packages by the client. Just clean install of my app and run which connects to the local DB which is created programmatically by my app. I am struggling with attaching the DB with the installer and if I even do the connection string from the app cannot get the proper path of the attached DB which the client chooses (not definite).
Common Question:
is there any common approach to attach local DB with the set of files to the installer so that the client doesn't need to install any other packages?
The Goal:
All I want is to create an installer that has DB and AppData which does not require any additional packages to be installed and the app uses the DB to store and retrieve the data plus the database already has tables and stored procedures.
Please Help me with this Guys. Thanks in advance.
Related
I am working on a project which is in .net core and hosted on azure and being used as SAAS application by different clients all over the world having separate databases.
Now I got a requirement which says we should delete the databases of those clients which are no longer in master db and deleted by admin.
I am in doubt that should we really delete the database for the clients which would be getting deleted?
Also if yes is it feasible to delete the database through application or we should use some utility to do that?
Also I am afraid if server will allow application to delete the database?
Can any one please suggest me on this?
Following tech stack i am using:
.net core (backend) with EF core
MSSQL (database)
Theres a few different ways.. Essentially it works the same as any other database, so you can execute T-SQL to do it.. or use the Azure SDK.. Here is some examples of different approaches: https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-database-transact-sql?view=sql-server-2017
You can execute raw SQL statements in EF like so:
using(var context = new SampleContext())
{
var commandText = "INSERT Categories (CategoryName) VALUES (#CategoryName)";
var name = new SqlParameter("#CategoryName", "Test");
context.Database.ExecuteSqlCommand(commandText, name);
}
So, using that format, you could add your DROP Database command in? Taken from: https://www.learnentityframeworkcore.com/raw-sql - under the Database.ExecuteCommand section
Let's say I have an assembly with a method does_stuff() that I've installed in an SQL server database. I want this method/storedproc to refer to a specific known table in the database. How do I get does_stuff to access the contents of the table without ever knowing where itself is hosted?
Let's say does_stuff is supposed to access table info_config.
If it was hosted in database ALPHA, it would access ALPHA.info_config
If it was hosted in database BETA, it would access BETA.info_config
I know how to open DB connections etc with ADO.NET, but those require specific server and database strings. I need flexibility so that the assembly does the same thing no matter where it is hosted.
Google search is giving me nothing.
Thanks in advance
All you have to do is use a context connection.
At the moment it may not seem obvious, but CLR code always executes in some context. There is always something (somebody) that executed the code directly or in some parent scope (stored procedure calls CLR function, CLR trigger fires on insert etc). When program execution reaches context connection, it just takes connection parameters (server, database, SET options etc) of that scope.
I want this method/storedproc to refer to a specific known table in the database.
After context connection is opened, you are querying your database as usual.
using (SqlConnection conn = new SqlConnection("context connection=true"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM MyTable");
SqlContext.Pipe.ExecuteAndSend(cmd);
}
I have created a local db using SQLEXPRESS through Visual Basic.
I intend to use LINQ to connect to the database from the application. Here is my statement to initially connect to the database:
Dim db As New DataContext("Data Source=localhost\SQLEXPRESS; Initial Catalog=master; Integrated Security=True;")
Ideally, my database would be entered for Initial Catalog, but that was giving me authentication errors for some reason. Now that this statement executes, my next step is to connect to my specific database. However, when I try to connect with a statement like this:
Dim TestCommand = db.ExecuteCommand("Use MyDB.mdf")
I get an error that the database does not exist.
When I query my database with the following commands:
SELECT name FROM master.sys.databases
The returned values are master, tempdb, model, msdb, and C:USERS\MY NAME\DOCUMENTS\MyDB.mdf
I have tried the above "TestCommand" writing out the directory for the database, but I get an error at "C:".
So, my db exists, but can someone explain to me the syntax I should use to "USE" my database?
You should not use the use command this way! You must connect to the application's database directly by setting it as Initial Catalog. If you're not authorized to do so, a use command won't let you either, by the way. So you have to fix the authorization for the database: create a login for your windows account in Sql Server Management Studio and grant it read/write access to the application's database.
I am working on Asp.net MVC 3 project, I need change the database name for the project I tried changing the database name in connection string & What happens is that when I pull the data it pulls from new DB where as when I try to insert data in some table it tries to insert in the old db. I am sure from where it is getting reference to the old DB name. Please HELP.
There must be a different connection string for the inserts. You should make a search in the entier solution after the database name or parts of the connection string to find that connection string and modify it too.
i am a beginer and i am making a client server application in c# using sql database.
i am using just two computers, at one computer i want to store my database as well as the application will also run on the same computer it one computer is the server and the client both and the another computer will be a simple client that will access the database.
can any one help me how shoud i write the code for both systems to connect the database.
thank you.
To connect to SQL Server from C#.NET, you need to create a connection string such as below:
private SqlConnection connection;
private string connectionString =
#"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );
Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = #Cid", connection);
The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.
Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.
This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.