I have a SQL Server 2008 database that uses a default schema called "Arxame". How do I specify or change the default schema from "dbo" to "Arxame" in my connection string? I'm using C# and ADO.NET.
You can't do that. You have to set the schema "Arxame" to the user you have specified on your connection string. You can do this using the SQL Server Management tool
If you need to change the default schema for an existing user you can do it like this
B. Changing the default schema of a user
The following example changes the default schema of the user Mary51 to Purchasing.
USE AdventureWorks2008R2;
ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;
GO
Source: MSDN
The InitialCatalog is indeed the database name. The schema that is used would depend on the user you specify, since schemas typically map to database users. Whatever user owns the Arxame schema is the one you should specify in the connection string.
I do not believe you can do this within a connection string, nor should you be able to. You use schemas, in much the same was as a namespace in C#, to further resolve securable objects within a database when there may be name collisions.
Schemas in SQL Server 2005 and up
The initial schema must be set in your connection string:
Data Source=localhost;Initial Catalog=Arxame;Integrated Security=True
Remember to use Integrated security only if you are using a Local Sql Server.
Related
As a part of the migration, I need to prefill an SQL Server table with the data where one of the fields should depend on the target database name or server name. At least it should not be the same for the Development and Production environments.
I wrote a code in OnModelCreating using modelBuilder.Entity<T>().HasData(...) but I still have no idea how to take the target database name here.
I think you can get database name from your connection string.
Put this code inside the Seed method:
var connection = context.Database.Connection.ConnectionString;
or something like that, which provide the connection string, then get the database name from that object.
I have code using EF with a connection string as in web.config (spaces and line breaks for clarity) :
connectionString=
metadata=res://*/Models.XXXDataContext.csdl|
res://*/Models.XXXDataContext.ssdl|
res://*/Models.XXXDataContext.msl;
provider=System.Data.SqlClient;
provider connection string="data source=xxxxxxxxxx.database.windows.net,1433;
initial catalog=db1;
user id=dba;
password=its-a-secret(bazinga!);
MultipleActiveResultSets=True;
App=EntityFramework"
I want to use the same connection string to execute a stored procedure to perform a merge insert/update to another table.
Can I user the connection string as is to create sqlConnection(cnns-tring)?
What is the purpose of the metadata=res: ... entry here? also MultipleActiveResultSets=True and App=EntityFramework? Would they have any impact here?
The metadata property specifically points to the location of the .SSDL (Storage Model,) .CSDL (Conceptual Model,) and .MSL (Mapping Model) files. These three files essentially are the Entity Data Model. The "res://" URI-style qualifier indicates that the files are embedded as resources in the compiled EDM assembly.
MultipleActiveResultSets - Multiple Active Result Sets (MARS) is a feature that works with SQL Server to allow the execution of multiple batches on a single connection. When MARS is enabled for use with SQL Server, each command object used adds a session to the connection.
App=EntityFramework: It's just the synonym of the Application Name.
All connectionstring keywords are explained here: https://msdn.microsoft.com/en-gb/library/system.data.sqlclient.sqlconnection.connectionstring.aspx
I have an ASP.NET MVC2 application running with SqlServer. I need to support multiple databases (SqlServer, Mysql and Oracle). The schema is the same for all databases.
The edmx is configured to run with SqlServer, so I added two new ssdl files: one for MYSQL and one for Oracle.
With SQLSERVER I can run the application, but when I try to set the connection string and metadata at runtime to be configured with MYSQL it generates exception:
How would I go about altering the ssdl to achieve that?
I don't know how to do it specifically for MySql but this is what I've done to achieve multiple database support for Sql Server and Oracle. I believe the same approach works for MySql also. I expect you know how to set up Oracle client configuration. Changing the active connection string in application configuration file is all I need to do to target a different db.
1) Generate edmx against Sql Server.
2) Generate .ssdl and .msl files for Oracle. The latter contains entity property mappings. Data types need to be changed in ssdl for Oracle. See this link for detailed information. Oracle ssdl file needs to have the correct provider configuration, in my case: Provider="Oracle.ManagedDataAccess.Client".
3) Create new connection string for Oracle db connection in your application configuration file. It is crucial that namespaces are typed correctly here. Below is a working example:
<add name="MyConnectionString"
connectionString="metadata=res://*/CustomDb.csdl|res://CustomDbProject/CustomDbNamespace.CustomDb.ssdl|res://CustomDbProject/CustomDbNamespace.CustomDb.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="DATA SOURCE=MYDS;USER ID=****;Password=****;Max Pool Size=**;Connection Timeout=120""
providerName="System.Data.EntityClient"/>
4) Create a default constructor for context:
public CustomDb()
: base("name=MyConnectionString")
{
Configuration.ProxyCreationEnabled = false;
}
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.
I can't find how to do this on google anywhere. How do you save to a SQL DB using just C# code? Is it possible? The save method that comes default when you create a DB using the wizard dosen't actually save the DB:
this.Validate();
this.studentsBindingSource.EndEdit();
this.studentsTableAdapter.Update(this.studentsDataSet.FirstClass);
It looks to me as though you are doing it correctly.
You should check your table adapter and verify that there is an update statement assigned. If you're using sprocs and only have the select sproc assigned then it'll be read only (and won't prompt you for the update/insert/delete sprocs).
Your connection string can be a few things depending on how you are configured to log into SQL.
Data Source=ServerName;Initial Catalog=DatabaseName;User Id=myUsername;Password=myPassword;
or
Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;
The latter is if you are using Windows Authentication; i.e. using the same user account you log into windows with.
DataSource is usually your machine's name or you can use (local) to get you over the hump should it be on the same machine you are working on.