how to connect database from different server in asp.net - asp.net

<configuration>
<connectionStrings>
<add name="MyCon" connectionString="Data Source=sys1-PC; Server=127.0.0.1; port=3306; Database=entmx001; Uid=root;Pwd=root_mysql; Convert Zero Datetime=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
this is my connection string in webconfig file.Now i want to connect my code to the database which is on other computer.what changes do i need to make here.or what else do i need to do..plzzhelp

<connectionStrings>
<add providerName="System.Data.SqlClient" name="MyCon" connectionString="Data Source=name_of_your_server;Initial Catalog=name_of_your_db; User Id=user_id;Password=password;" />
</connectionStrings>

Related

Web.Debug.config is NOT transforming the connectionstrings to the Web.config in MVC 5 projects

I have created a new VS 2015 web project MVC5. By default I can see the Web.config and Web.Debug.config
Reading several articles, I really don't see what I really need to do in order to have it take my values from Web.Debug.config and replace the current Web.config.
I've been looking at another project at work that does this and it works fine, but I'm been through a lot of properties and settings and I don't see what is different.
I can Right click on Web.Debug.config and Preview and it shows me that it will REPLACE "test" with "10.10.10.10" so it seems good to me (like it should work but running project it DOES NOT change the value)
Example
Project :
Debug / Any CPU , run google chrome , problem is that Data source is NOT getting changed
Web.Debug.config
<connectionStrings>
<add name="Envy" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Web.config
<connectionStrings>
<add name="Envy" connectionString="Data Source=test\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
<add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
<add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
<add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
Out of the box, transformations (debug/release) are applied on publishing (deploy). Not on build, on deploy.
To make this happen on build you may need to do some manual edits of the project file. Take a look here fore example: https://gist.github.com/EdCharbeneau/9135216
So close but BACKWARDS. For this case use Web.config and Web.Release.config only.
Put all your Debug settings in Web.config, put your Release settings in Web.Release.config, and do not use Web.Debug.config. Debugging will happen using the regular Web.config, and only released builds will get the Release settings.
from your example above:
Web.config
<!-- test/debug settings in regular web.config -->
<connectionStrings>
<add name="Envy" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
<add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
<add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
<add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>
Web.Release.config
<connectionStrings>
<add name="Envy" connectionString="Data Source=test\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="EnvyIdentity" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="DNNSmartstore" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=myDB;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="DNNPos" connectionString="Data Source=10.10.10.10\MSSQLSERVER2014;Initial Catalog=DevFood_POS;user id=myLoginID;password=password" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Web.Debug.config is a place to put settings that you want to apply when you deploy to a test server. The name is pretty misleading at first. The only place I ever use this feature is if I have a Test server which needs different settings from my Dev box. :-)

sql connection settings via web config file

I've defined my connection string as ::
<add name="ammyGroupConnectionString" connectionString="Data Source=ammyGroup_Server;Initial Catalog=ammyGroup_Staging;Persist Security Info=True;User ID=sa;Password=ammyGroup;" providerName="System.Data.SqlClient"/>
<add name="ammyGroupConnectionString" connectionString="Data Source=ammyGroup_Server;Initial Catalog=ammyGroup_Staging;Persist Security Info=True;User ID=sa;Password=ammyGroup;" providerName="System.Data.SqlClient"/>
Now in the file there are tags,
<location allowOverride="true" inheritInChildApplications="true">
<appSettings>
<add key="DBSchema" value=/>
<add key="DBUserId" value=/>
<add key="DBPassword" value=/>
<add key="DBServerName" value=/>
<add key="DBName" value=/>
What does these keys are? What should be the value of these keys? Please explain.
I know that the keys
add key="DBUserId" and add key="DBPassword"
are for MS SQL SERVER user name and password but what when there is windows authentication only? Please explain.
I know for windows authentication we have to do ::
Trusted_Connection=Yes;Integrated Security=SSPI;
but what about appsetting values

How to make a string connection on web.config for a SQL Server Connection?

I'm creating a Site for ASP.NET, actually I made a EF model to create my DataBase, but this is just creating a SQL Express .mdf DB, I would like to know how I should change the Connection String to connect to an SQL Server DB.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WingtipToys;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="WingtipToys" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\wingtiptoys.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />

Creating a string in a web.config and use it in a web.api

I am new in the web development world and I would like to create a variable in the web.config file so that I can use it in the .NET portion of the web.api
I found the following tutorials on how to do that :
Setting up connection string in ASP.NET to SQL SERVER
And
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
I have the following question , I don t have a database to connect the string to(I will only use it in the web config so that I can easily change the string without having to go through code . so assuming that I am using it in the following way :
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
What should I have in the connectionString and the providerName ?
If I understand what you want to do, it sounds like you don't want to use a connection string at all. Instead, use the Application Settings sections of your web.config file. For example
<configuration>
<system.web> ... </system.web>
<appSettings>
<add key="MyAppSetting" value="A test value." />
</appSettings>
</configuration>
This can then be used in your code by getting the value of
System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]
(C#) or
System.Configuration.ConfigurationManager.AppSettings("MyAppSetting")
(VB)
See MSDN for more information, or just search online for "asp.net AppSettings".
If you don't have a database to connect to (which is what I understood from your question), then you don't even need to have the <connectionStrings> section in your Web.config. That section is only needed if you are going to connect to a database.
If you do use a database, then the connectionString varies depending on several factors such as type of authentication, database product (MS SQL Server, MySQL), type of driver (ODBC, .NET), etc.
The "Provider Name" will depend on the database product that you are using. For example for SQL Server is "System.Data.SqlClient"
You can look at this site for a comprehensive list of database products and connection strings appropriate for each product for different authentication types, drivers used, etc.
For ASP.NET 4.5 Application I'm using appSettings for email configuration. I'm also using connectionStrings
appSettings needs to be included before connectionStrings not before configSections
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="ContactEmail0" value="service#davincispainting.com" />
<add key="ContactEmail1" value="estimate#davincispainting.com" />
</appSettings>
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
</connectionStrings>
...

DB Connection string in Web.config to use attached .mdf database won't work

The file neodb.mdf is in my App_Data folder and I can browse the database in the server explorer in visual studio, using built in SQLEXPRESS:
Currently trying to no avail:
<connectionStrings>
<add name="EFDbContext" connectionString=".\SQLExpress;AttachDbFilename=|DataDirectory|neodb.mdf; Database=neodb;Trusted_Connection=Yes;" providerName="System.Data.SqlClient"/>
</connectionStrings>
and
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source=.SQLEXPRESS;Database=neodb.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Also as I understand the *.mdf is an SQL server database file type and .dbo is owner of file when it's included in the initial catalog ? What's the initial catalog anywhere ?
If you have the *.mdf placed in App_Data folder, using this format works:
<connectionStrings>
<add name="ConnectionName"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
There is a simple way to retrieve the connection string to each database.
Double click on "DataBase.mdf" file in Solution Explorer > right click on the "DataBase.mdf" file in Server Explorer > click "Properties" > Now you can see the connection string (under the "Connection" header) in the properties menu!
If you wanted the database to be created in your App_Data folder. You can use the following ConnectionString:
<connectionStrings>
<add name="ConnectionName" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />
Alternatively you can use:
<add name="ConnectionName" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
Also you will see MyDatabase.mdf and .ldf database files are created in the C:\Users\YourUserName Folder.

Resources