You have two servers when you perform a SQL Server database mirroring
You have 1 primary database and 1 mirror database
Do you need to make any changes to web application to tell it that your using database mirroring? If not how does your web application know which database to use when the primary database fails?
It can be set in the connection string. See the "database mirroring" example here
Do you need to make any changes to web application to tell it that your using database mirroring?
Yes, add the Failover Partner parameter to your connection string, using either the IP or instance name. IE: (line returns for readability)
Data Source = myServerAddress;
Failover Partner = myMirrorServerAddress;
Initial Catalog = myDataBase;
Integrated Security = True;
The mirror doesn't have to exist to test the failover - you can use a dummy address as the Data Source and watched our application use the valid config in the Failover Partner.
But mirroring does not cover logins or jobs - this link provides a script for cloning logins from SQL Server 2005 to other 2005+ instances. You will have to use either ALTER USER or sp_change_users_login to sync the logins if the SIDs are not identical on the principal and mirror.
Related
I am fairly beginner in here, so any help would be much appreciated :)
So, I created a SQL Database and I want to connect EasyTables to it. Apparently the automatic option has been removed and I have to do it manually.
I followed "How can I add a connection string manually" page but it lead me to nothing.
Where should i create the connection string, and what to put in the value field?
Or if there is any tutorials out there for the new way please tell me :)
Thank you a lot
You said you have followed tutorial: How can I add a connection string manually.
The Easy Table connection should like this:
SQL Database Connection String format
Data Source=tcp:{your_SQLServer},{port};Initial Catalog={your_catalogue};User ID={your_username};Password={your_password}
{your_SQLServer} Name of the server, this can be found in the
overview page for your database and is usually in the form of
“server_name.database.windows.net”.
{port} usually 1433.
{your_catalogue} Name of the database.
{your_username} User name to access your database.
{your_password} Password to access your database.
Add the connection string to your Web App
In App Service, you can manage connection strings for your application by using the Configuration option in the menu.
To add a connection string:
Click on the Application settings tab.
Click on [+] New connection string.
You will need to provide Name, Value and Type for your connection
string.
If your are adding a connection string to a SQL Azure database choose
SQLAzure under type.
If your are adding a connection to an Azure Storage account, chose
Custom under type.
NOTE If you are adding a connection string because you are planning on using the Easy API or Easy Table features, then the connection strings used by this features expect the following specific names:
Azure SQL database: MS_TableConnectionString
Azure Storage account: MS_AzureStorageAccountConnectionString
For example, this is my connection string:
When you have configured this, go to Easy table, click Add to add the table name in your Azure SQL database.
You can find that esay table has connected to my Azure SQL database now and we can see the data in the table.
Update:
You should first add the connection string in the configuration followed the format provided for you.
Then go to Easy Table, configure Easy table API:
Choose the 2:
When it done, you can add table in your SQL database, please follow my steps in above.
Note:
Your connection string name must be: MS_TableConnectionString.
Hope this helps.
I need to create a contained user for my Azure SQL database using C#. I have the following code already for creating the database:
// Login to Azure
var credentials = UserTokenProvider.LoginSilentAsync(clientId, domainName, username, password).Result;
// Create client
SqlManagementClient client = new SqlManagementClient(credentials)
{
SubscriptionId = subscriptionId
};
// Database parameters
var databaseParameters = new Microsoft.Azure.Management.Sql.Models.Database()
{
Location = "ukwest",
ElasticPoolId = elasticPoolId,
};
// Create database
var dbResponse = client.Databases.CreateOrUpdate(resourceGroupName, serverName, databaseName, databaseParameters);
I have found the following SQL script that can be used to create a contained user using SQL management studio but this requires that you manually connect to the database that you want to create the user for:
CREATE USER [databaseUser] WITH PASSWORD = 'xxxxxxxxxxx';
ALTER ROLE [db_datareader] ADD MEMBER [databaseUser]
ALTER ROLE [db_datawriter] ADD MEMBER [databaseUser]
So how can I use the SQL script within my C# code to connect the database after it has been created to then create the contained SQL user for that database, or is there some equivalent code available in Microsoft.Azure.Management.Sql.SqlManagementClient for doing this?
Conceptually, you should think about Azure SQL Database as having (at least) 2 different layers at which you can operate:
There is a REST API for control operations such as creating a database, changing its reservation size, restoring a copy, etc.
There is a T-SQL interface for operations within a database container, from creating tables to inserting rows, etc.
There can be some overlap across these two surfaces - in traditional SQL Server, all of it is exposed in the latter T-SQL interface. Some of those commands are enabled in Azure SQL Database and they are internally calling the REST API for you.
Note that operations to the REST API use the credentials associated with Azure's Portal infrastructure. Within the database, you can have mappings into SQL logins/users, but you can also have SQL logins/users that are not associated at all with the Azure identities.
Net-net: you should connect to the database using the C# SQLClient and run those commands using an authenticated user with enough permissions (in your case, likely the administrator account or sa) to get the new user and role information set up.
Here's a an example on how to set up C# SQLClient
I am a DBA and have decided as a project to help me learn c# / ef to build an app that monitors sql servers, the idea is to develop a windows service that runs to collect all the stats from various sql instances using scheduled jobs in .net quartz. i.e. connected users and details from various dynamic management views.
to store the configuration data i.e. what servers to monitor I am using an sql database so it just contains a table of servers to monitor you will be able to add move via a web ui.
now the issue I have is how to loop through the servers "table" in EF and obtain the connection strings column and use this to connect to the various sql instances to get stats. (these will later be written back to the database for analysis by a front end)
e.g.configuration table data:
servername: test server
connectionstring: testserver\inst1
hope that all makes sense, thanks for your time
You can create a list of all your connection string keys like this-
List<String> DataVaseKeys = new List<String>(); DataVaseKeys.Add("testserver\inst1");
DataVaseKeys.Add("testserver\inst2");
foreach (var key in DataVaseKeys) {
string currentConString=System.Configuration.ConfigurationManager.
ConnectionStrings[key].ConnectionString;
//access to the data base with your connection string here
}
I have a customer database that is kept on a SQL Server on our local network. I would like to create a customer portal that will be on our website that is hosted through another company. How would I connect to that SQL Server database?
Give the website host access rights to the sql server. Assuming Sql Server 2008; go to your management studio and right click the server (root) in the object explorer window and go to properties. You can manage permissions from there. Also, it will show you the "server" to use in your connection string (something like [server]\SQLEXPRESS, which can be used locally and remotely).
Create a proper connection string in the website, preferably in web.config, to use for all of your connections to the database. You can then get this connection string from, say, your data layer via
ConfigurationManager.ConnectionStrings["ConnString_Name"].ConnectionString;
Aside from the correct connection string, you will also need to ensure that the website can communicate with your SQL Server. If you have firewalls, you'll need to configure ports if they are blocked.
The alternative is to create a web service that is hosted on a DMZ zone that will communicate with your sql server internally. The website (hosted by the third party) would communicate via this web service to get the data (you can setup authentication so only those with rights can use this web service). By going this route, you're not exposing your internal sql server directly.
This answer is based on some assumptions because question does not provide all the required information.
For this you need to set ConnectionString property for your connection object.
For example
Data Source=yourIP;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Here is MSDN link connectionStrings
This is a example of SQLExpress connectionstring in Web.Config
<connectionStrings>
<add
name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
There is a Beginners guide on Code Project which is voted 5, it will give you all you need to get started.
But before you start working with the code, I suggest that you first test the connection with SQL Server management studio. make sure that you can connect and query some data, otherwise you may face some more confusion while trying to pull this off with code only at the first time.
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.
I write a win app,and i create my database on the server by codes.now every client on local network can't login to my database and this error occured
:"cannot open database "test" requested by the login.the login failed for user "farzane".
the connectionstring for to make my database is:
ConnectionString=#"Data Source=SERVER\SQLEXPRESS;Initial Catalog=master;Integrated security=SSPI;Persist Security Info=False";
and it's my connection string for open my database:
ConnectionString=#"Data Source=SERVER\SQLEXPRESS;Initial Catalog=test;Integrated security=SSPI;Persist Security Info=False";
how can give permission for logining to my database to any client with codes???
thanks in advance for any help.
I would check two things here:
Ensure that your SQL Express install allows remote connections. (Simple to check using SQL Server Studio Manager).
You are using trusted authentication in your connection string. You have to explicitly give users on your domain access on the database. You will have to this in SQL Server.
are you using a domain for the network ?
if yes then make sure that the user name has access to the SQL server
if you're using a workgroup then it won't work... just create a user on the sql server and use the sql server auth at the server and connection string
Points i concluded:
First of all the users who are going to create the database , must be authorized to use master database. So ask your admin to allow permission to farzanne.
If you(farzanne) are admin, set farzanne to create databases permission to true. Or the other users that might create dbs. Also, if you allow all users then it will be difficult to handle, your application, so be alert.
What is the need of the dynamically createing database from application. Is this a part of setup or deployment or you are creating an isolated space that is different user different database.