Azure - Connecting .NET web app with azure sql database - c#

I have a team project and I'm trying to publish the app at Azure, which I successfully did. I was able to get our database to Azure SQL server, but now I'm struggling with connecting the app with this database.
Originaly the app is working with a .mdf file locally, but now, when I'm publishing the app, I want to use the DB at azure server. I changed all the connection strings of .mdf file
(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename =" + Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + #"..\..\..\PATH"))
in the code to the azure database connection string
(#"Data Source = Server=tcp:nameofserver.database.windows.net,1433;Initial Catalog=sqlmusicdb;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;")
but after publishing, the app is not connected to database, as it is writing
AN ERROR OCCURRED WHILE PROCESSING YOUR REQUEST
Any help, please? Thanx.

Your connection string is formatted incorrectly. Remove the
Data Source =
at the very beginning of your connection string and it should work.

Related

Azure Web App wrong Connection string in Kudu Env

I'm trying to connect my Azure Web App to my AzureSQL database.
In the Kudu Env (https://{my-app-name}.scm.azurewebsites.net/env) there is some kind of default connection string:
LocalSqlServer
- ConnectionString = data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
- ProviderName = System.Data.SqlClient
I have set the connection string in the App configuration, it looks like this:
Data Source=tcp:myapp.database.windows.net,1433;Initial Catalog=databaseName;User ID=databaseSA;Password=password;
But no matter what I try, the connection string in the Kudu environment never changes.
This connection string is also set no where by me, neither in the code base nor in any Azure settings I found.
It's very similar to this SO, which never got resolved: Azure Web App Not Showing Connectionstring in Kudu Env
This is the error message that throws when the app tries to access the database, which makes sense since the connection string tries to connect to .\SQLEXPRESS which doesn't exist anywhere (locally I use .\SQL2019 as connection string):
I would appreciate any help or hint.
So after 3 days I finally managed to get it to work.
First I got it running locally, be sure to white list your IP in the Azure database firewall settings.
Then I got it running in Azure and checking all the names and settings again.
I used the first connection string from my Azure database (SQL Authentication) with the database SA user.
I set that connection string in the connection string of the Azure Web App.
Also make sure that the connection string in your code is the same as the one in the Azure Web app configuration.
Also during some point my App didn't run anymore due to errors in the .Net code, so beware of that.
I thought I had tried this already before posting here, but I guess there were some wrong settings in all these tests.

Can't connect to Azure database on Sever although the connection String is right. 1042 error

Im trying to connect to MySQL database on Azure through C#. I am using MySQLConnector package for my program.
I have admin account so I can easily access to the database through Sever Explorer in Visual Studio in SSH Sever Authentication . However I can not do the same with C#. I even tried to copy the exact connection string given by Sever Explorer but I still cant connect to my database through C# . I always get the error "1042: Unable to connect to any of the specified MySQL hosts." . The connection String from Sever Explorer provide necessary param : Data Source, Inital Catalog, Persist Security Info, User ID and Password.
As I have admin account. I have also tried to copy the connection String provided from Azure Portal for ADO.net. The connection String look a bit different . For example it provide extra parameter like
TrustServerCertificate, Encrypt, MultipleActiveResultSets. However if I used this connection string, the program does not compile. Visual Studio tell me the these parameters are not supported . For example :'Option 'MultipleActiveResultSets' not supported.' etc. If I deleted those paratemters, I have the same 1042 error. Please help me, I am new. Thanks
The Connection String from Sever Explorer in Visual Studio look like this:
Data Source= ******.database.windows.net;Initial Catalog=******;Persist Security Info=True;User ID=******;Password=******
The Connection String from Azure Portal look like this:
Server=tcp:******.database.windows.net,1433;Initial Catalog=******;Persist Security Info=False;User ID=******;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
The Connection String from Azure Portal look like this:
Server=tcp:******.database.windows.net,1433;Initial Catalog=******;Persist Security Info=False;User ID=******;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
This is a SQL Server connection string. You cannot use MySqlConnector with this connection string. Are you sure you're using Azure Database for MySQL and copying the connection string from the right database?

Connect to database in network drive

I am a newbie to SQL Server and .net. Please let me know if my question is not clear before down voting.
I am working on a Windows application with C#. I should give option to users to connect to a .mdf file on a network drive. On my machine, I have Windows and SQL Server authentication. Users have SQL authentication hence I should use userid and pwd. Myself and users work on that network drive, read/write/modify. We pretty much share documents, add and delete docs from network drive.
Here is the designer
I will choose the SQL Server database .mdf file which is located in network drive and then do test connection. For Test Connection this is the code
string sTemp = System.Configuration.ConfigurationManager.AppSettings["connectionStringShare"];
string connectionString = sTemp.Replace("{AppDir}", txtDB.Text.Trim());
using (SqlConnection objSqlConnection = new SqlConnection(connectionString))
{
try
{
objSqlConnection.Open();
objSqlConnection.Close();
MessageBox.Show("Connection is successfull");
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message.ToString());
}
}
This is the connection string
<add key="connectionStringShare"
value="Data Source=.\SQLEXPRESS;Initial Catalog=TableSQLExpress;AttachDBFilename={AppDir};Integrated Security=SSPI;user id=sa;password=pwd;" />
Here is the error message I got
Directory lookup for the file "S:\zrep\TableSQLExpress.mdf" failed with the operating system error 3(The system cannot find the path specified.).
Cannot attach the file 'S:\zrep\TableSQLExpress.mdf' as database 'TableSQLExpress'.
I changed connection string and tried also tired using windows authentication. No luck. Let me know if I need to provide any additional details. Since I am newbie to this field please give me detailed answer. I am glad to find this group. Thanks for everyone who looked into this.
When you use server-based SQL Server (i.e. Microsoft SQL Server Express) you are unable to share database file via network drive, it is by design. Even if you override default SQL Server behavior with a switch and enable UNC paths for databases, your data will be corrupted by multiple server instances trying to use single database MDF file. If you need to host database in serverless environment (using only a network drive), you may opt to Microsoft SQL Server Compact (SQL Server CE) edition. But be aware that in such case only one user will be able to access database file at the same time (exclusive locking -> low performance). Plus SQL Server CE does not have stored procs.

Connect to remote SQL Server 2008 on Visual Studio 2010

I created a local MSSQL database with my ASP.NET project.
Now I want to connect to my database hosted in www.abc.com
What should I put in the connectionString ?
Check with your hosting provider (abc.com). Some hosting providers don't allow remote connection to their databases, and require you to use their web application to access your database.
If they do allow access, they should provide you with SQL Authentication details of:
Server Name
Database Name
Username
Password
An example of a SQL connection string would be:
Data Source={server name/ip};Initial Catalog={database name};User ID={username};password={password}; MultipleActiveResultSets=True;
See http://www.connectionstrings.com/sql-server-2008
You may use Server Explorer tool of Visual studio to get the connection string. (Right click on Data Connections + Add Connection).
"Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;"
or
"Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;integrated security=true"

SQL connection string for database on network

I want to connect to a database on a host except localhost, my DBMS is SQL Server and I'm using ado.net, like this:
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("insert into st (ID,Name) values ('"+cnt.ToString()+"','havijuri');", con);
//some sql commands.
con.Close();
what should I use as the constr (connection string), and with these information:
host IP: 10.15.10.12
the file is database1.mdf,
in this directory(on the host): D:\Project1\DataBase
Tell me if any other information is needed
If the mdf file is not attached to an instance of sql server and you want to connect to the database while it does not exist on the same machine as your application, you need first to copy the database to the server with the mentioned IP and attach it to an instance of sql server installed on that server.
The connection string in this case -if you have a domain and will be authenticated to the database server by windows authentication- will be as follows:
"data source=10.15.10.12; integrated security=SSPI;initial catalog=database1"
Or you can create a sql server user on the database sever and connect using the following connection string:
"data source=10.15.10.12; initial catalog=database1;user id=<username>;password=<password>"
http://www.connectionstrings.com/sql-server-2008
create a file on your desktop called test.udl open it up and follow the steps to connect to your database then click test to make sure it works. then open the file in notepad, it will be 1 line and contain the connection string
and with these information:
•host IP: 10.15.10.12 •the file is
database1.mdf, •in this directory(on
the host): D:\Project1\DataBase
You can not. Database file attachment is only supported by express, not by the real server. ForSL Server, you need the database name (which can be different than the file name) and the database must e mounted first by the DBA. You also need acces to the server (as in: username, password). The security credentials are - again - determined by the DBA.
So, you miss the critical information (name of the database, username, password) to access a database server.

Categories

Resources