ASP.Net web application connect to host SQL DataBase











up vote
0
down vote

favorite












I have a simple Web API Application and I want to connect it to a DataBase presents in my host. This is in my web.config:



  <connectionStrings>
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>


And I'm going to test it with following codes:



public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
command.ExecuteNonQuery();

}
catch (Exception ex)
{}
}
return strings[id];
}


After publishing project in my host what happens is The Connection String will be add in ASP.NET Configuration page in Plesk panel:plesk panel



After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
phpMyAdmin










share|improve this question






















  • You can't get to localhost from your hosting provider. You need a database server accessible to your host. You could also consider an embedded database like SQLite if it meets your needs.
    – Crowcoder
    Nov 10 at 14:13










  • How do I connect database server using name id and password?
    – Mohammad Farahi
    Nov 10 at 14:37










  • While I'm not familiar with Plesk, I see a "databases" tab in your screenshot. There are dozens of options but I would start there.
    – Crowcoder
    Nov 10 at 14:42






  • 1




    Your code (SqlConnection) and connection string are for Microsoft SQL Server, but your screenshot is of a MySQL database. You will need to use MySqlConnection to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
    – Bradley Grainger
    Nov 11 at 1:18















up vote
0
down vote

favorite












I have a simple Web API Application and I want to connect it to a DataBase presents in my host. This is in my web.config:



  <connectionStrings>
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>


And I'm going to test it with following codes:



public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
command.ExecuteNonQuery();

}
catch (Exception ex)
{}
}
return strings[id];
}


After publishing project in my host what happens is The Connection String will be add in ASP.NET Configuration page in Plesk panel:plesk panel



After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
phpMyAdmin










share|improve this question






















  • You can't get to localhost from your hosting provider. You need a database server accessible to your host. You could also consider an embedded database like SQLite if it meets your needs.
    – Crowcoder
    Nov 10 at 14:13










  • How do I connect database server using name id and password?
    – Mohammad Farahi
    Nov 10 at 14:37










  • While I'm not familiar with Plesk, I see a "databases" tab in your screenshot. There are dozens of options but I would start there.
    – Crowcoder
    Nov 10 at 14:42






  • 1




    Your code (SqlConnection) and connection string are for Microsoft SQL Server, but your screenshot is of a MySQL database. You will need to use MySqlConnection to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
    – Bradley Grainger
    Nov 11 at 1:18













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a simple Web API Application and I want to connect it to a DataBase presents in my host. This is in my web.config:



  <connectionStrings>
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>


And I'm going to test it with following codes:



public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
command.ExecuteNonQuery();

}
catch (Exception ex)
{}
}
return strings[id];
}


After publishing project in my host what happens is The Connection String will be add in ASP.NET Configuration page in Plesk panel:plesk panel



After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
phpMyAdmin










share|improve this question













I have a simple Web API Application and I want to connect it to a DataBase presents in my host. This is in my web.config:



  <connectionStrings>
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>


And I'm going to test it with following codes:



public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
command.ExecuteNonQuery();

}
catch (Exception ex)
{}
}
return strings[id];
}


After publishing project in my host what happens is The Connection String will be add in ASP.NET Configuration page in Plesk panel:plesk panel



After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
phpMyAdmin







c# mysql sql asp.net asp.net-web-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 13:41









Mohammad Farahi

4803723




4803723












  • You can't get to localhost from your hosting provider. You need a database server accessible to your host. You could also consider an embedded database like SQLite if it meets your needs.
    – Crowcoder
    Nov 10 at 14:13










  • How do I connect database server using name id and password?
    – Mohammad Farahi
    Nov 10 at 14:37










  • While I'm not familiar with Plesk, I see a "databases" tab in your screenshot. There are dozens of options but I would start there.
    – Crowcoder
    Nov 10 at 14:42






  • 1




    Your code (SqlConnection) and connection string are for Microsoft SQL Server, but your screenshot is of a MySQL database. You will need to use MySqlConnection to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
    – Bradley Grainger
    Nov 11 at 1:18


















  • You can't get to localhost from your hosting provider. You need a database server accessible to your host. You could also consider an embedded database like SQLite if it meets your needs.
    – Crowcoder
    Nov 10 at 14:13










  • How do I connect database server using name id and password?
    – Mohammad Farahi
    Nov 10 at 14:37










  • While I'm not familiar with Plesk, I see a "databases" tab in your screenshot. There are dozens of options but I would start there.
    – Crowcoder
    Nov 10 at 14:42






  • 1




    Your code (SqlConnection) and connection string are for Microsoft SQL Server, but your screenshot is of a MySQL database. You will need to use MySqlConnection to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
    – Bradley Grainger
    Nov 11 at 1:18
















You can't get to localhost from your hosting provider. You need a database server accessible to your host. You could also consider an embedded database like SQLite if it meets your needs.
– Crowcoder
Nov 10 at 14:13




You can't get to localhost from your hosting provider. You need a database server accessible to your host. You could also consider an embedded database like SQLite if it meets your needs.
– Crowcoder
Nov 10 at 14:13












How do I connect database server using name id and password?
– Mohammad Farahi
Nov 10 at 14:37




How do I connect database server using name id and password?
– Mohammad Farahi
Nov 10 at 14:37












While I'm not familiar with Plesk, I see a "databases" tab in your screenshot. There are dozens of options but I would start there.
– Crowcoder
Nov 10 at 14:42




While I'm not familiar with Plesk, I see a "databases" tab in your screenshot. There are dozens of options but I would start there.
– Crowcoder
Nov 10 at 14:42




1




1




Your code (SqlConnection) and connection string are for Microsoft SQL Server, but your screenshot is of a MySQL database. You will need to use MySqlConnection to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
– Bradley Grainger
Nov 11 at 1:18




Your code (SqlConnection) and connection string are for Microsoft SQL Server, but your screenshot is of a MySQL database. You will need to use MySqlConnection to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
– Bradley Grainger
Nov 11 at 1:18












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The problem occurred because you're using SQL Server connection string to connect against MySQL database, which doesn't work as expected. Ensure that MySQL Connector .NET is referenced in your project (i.e. include MySql.Data.dll and all related assemblies), then replace your connection string from this one:



<!-- Wrong (SQL Server connection string) -->
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />


to this example:



<!-- Correct -->
<add name="JarasDB" connectionString="Server=localhost;Port=3306;Database=jarasdb;Uid=UserID;Pwd=XXXXX" providerName="MySql.Data.MySqlClient"/>


Additionally, it is necessary to use MySqlConnection and MySqlCommand from MySql.Data.MySqlClient namespace to execute DDL query:



// add this line on top of 'using' lines
using MySql.Data.MySqlClient;

public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connectionString))
{
try
{
con.Open();
using (MySqlCommand command = new MySqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// throw exception here
}
}
return strings[id];
}


Reference: MySQL Connector .NET Connection Strings






share|improve this answer





















  • It worked fine, but I had to add NUGET Mysql.Data
    – Mohammad Farahi
    8 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239567%2fasp-net-web-application-connect-to-host-sql-database%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The problem occurred because you're using SQL Server connection string to connect against MySQL database, which doesn't work as expected. Ensure that MySQL Connector .NET is referenced in your project (i.e. include MySql.Data.dll and all related assemblies), then replace your connection string from this one:



<!-- Wrong (SQL Server connection string) -->
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />


to this example:



<!-- Correct -->
<add name="JarasDB" connectionString="Server=localhost;Port=3306;Database=jarasdb;Uid=UserID;Pwd=XXXXX" providerName="MySql.Data.MySqlClient"/>


Additionally, it is necessary to use MySqlConnection and MySqlCommand from MySql.Data.MySqlClient namespace to execute DDL query:



// add this line on top of 'using' lines
using MySql.Data.MySqlClient;

public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connectionString))
{
try
{
con.Open();
using (MySqlCommand command = new MySqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// throw exception here
}
}
return strings[id];
}


Reference: MySQL Connector .NET Connection Strings






share|improve this answer





















  • It worked fine, but I had to add NUGET Mysql.Data
    – Mohammad Farahi
    8 hours ago















up vote
1
down vote



accepted










The problem occurred because you're using SQL Server connection string to connect against MySQL database, which doesn't work as expected. Ensure that MySQL Connector .NET is referenced in your project (i.e. include MySql.Data.dll and all related assemblies), then replace your connection string from this one:



<!-- Wrong (SQL Server connection string) -->
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />


to this example:



<!-- Correct -->
<add name="JarasDB" connectionString="Server=localhost;Port=3306;Database=jarasdb;Uid=UserID;Pwd=XXXXX" providerName="MySql.Data.MySqlClient"/>


Additionally, it is necessary to use MySqlConnection and MySqlCommand from MySql.Data.MySqlClient namespace to execute DDL query:



// add this line on top of 'using' lines
using MySql.Data.MySqlClient;

public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connectionString))
{
try
{
con.Open();
using (MySqlCommand command = new MySqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// throw exception here
}
}
return strings[id];
}


Reference: MySQL Connector .NET Connection Strings






share|improve this answer





















  • It worked fine, but I had to add NUGET Mysql.Data
    – Mohammad Farahi
    8 hours ago













up vote
1
down vote



accepted







up vote
1
down vote



accepted






The problem occurred because you're using SQL Server connection string to connect against MySQL database, which doesn't work as expected. Ensure that MySQL Connector .NET is referenced in your project (i.e. include MySql.Data.dll and all related assemblies), then replace your connection string from this one:



<!-- Wrong (SQL Server connection string) -->
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />


to this example:



<!-- Correct -->
<add name="JarasDB" connectionString="Server=localhost;Port=3306;Database=jarasdb;Uid=UserID;Pwd=XXXXX" providerName="MySql.Data.MySqlClient"/>


Additionally, it is necessary to use MySqlConnection and MySqlCommand from MySql.Data.MySqlClient namespace to execute DDL query:



// add this line on top of 'using' lines
using MySql.Data.MySqlClient;

public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connectionString))
{
try
{
con.Open();
using (MySqlCommand command = new MySqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// throw exception here
}
}
return strings[id];
}


Reference: MySQL Connector .NET Connection Strings






share|improve this answer












The problem occurred because you're using SQL Server connection string to connect against MySQL database, which doesn't work as expected. Ensure that MySQL Connector .NET is referenced in your project (i.e. include MySql.Data.dll and all related assemblies), then replace your connection string from this one:



<!-- Wrong (SQL Server connection string) -->
<add name="JarasDB" connectionString="Data Source=localhost;Initial Catalog=JarasDB;Integrated Security=True;" providerName="System.Data.SqlClient" />


to this example:



<!-- Correct -->
<add name="JarasDB" connectionString="Server=localhost;Port=3306;Database=jarasdb;Uid=UserID;Pwd=XXXXX" providerName="MySql.Data.MySqlClient"/>


Additionally, it is necessary to use MySqlConnection and MySqlCommand from MySql.Data.MySqlClient namespace to execute DDL query:



// add this line on top of 'using' lines
using MySql.Data.MySqlClient;

public string Get(int id)
{
string connectionString = ConfigurationManager.ConnectionStrings["JarasDB"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(connectionString))
{
try
{
con.Open();
using (MySqlCommand command = new MySqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
{
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// throw exception here
}
}
return strings[id];
}


Reference: MySQL Connector .NET Connection Strings







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 2:02









Tetsuya Yamamoto

13.1k41939




13.1k41939












  • It worked fine, but I had to add NUGET Mysql.Data
    – Mohammad Farahi
    8 hours ago


















  • It worked fine, but I had to add NUGET Mysql.Data
    – Mohammad Farahi
    8 hours ago
















It worked fine, but I had to add NUGET Mysql.Data
– Mohammad Farahi
8 hours ago




It worked fine, but I had to add NUGET Mysql.Data
– Mohammad Farahi
8 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239567%2fasp-net-web-application-connect-to-host-sql-database%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues