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:
After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
c# mysql sql asp.net asp.net-web-api
add a comment |
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:
After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
c# mysql sql asp.net asp.net-web-api
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 useMySqlConnection
to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
– Bradley Grainger
Nov 11 at 1:18
add a comment |
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:
After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
c# mysql sql asp.net asp.net-web-api
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:
After calling Get method I expect to create a table in my database but nothing happens. I want to know where is my problem.
c# mysql sql asp.net asp.net-web-api
c# mysql sql asp.net asp.net-web-api
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 useMySqlConnection
to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api
– Bradley Grainger
Nov 11 at 1:18
add a comment |
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 useMySqlConnection
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
add a comment |
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
It worked fine, but I had to add NUGET Mysql.Data
– Mohammad Farahi
8 hours ago
add a comment |
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
It worked fine, but I had to add NUGET Mysql.Data
– Mohammad Farahi
8 hours ago
add a comment |
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
It worked fine, but I had to add NUGET Mysql.Data
– Mohammad Farahi
8 hours ago
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 useMySqlConnection
to connect to it, e.g., mysql-net.github.io/MySqlConnector/tutorials/basic-api– Bradley Grainger
Nov 11 at 1:18