C# doesn't connect to DataBase (SQL)











up vote
0
down vote

favorite












I have a database to manage a school (students and classes).
I have a class with the code to connect to the DataBase and then I call the functions in the main program.
When I try to interact with the DataBase, it warns me that it could not connect to the DataBase or it exceeded the connection time.
I tried to add an ssslmode but it didn't work. I also tried to add a port but it didn't work.



Code for the class:



public class ligacao
{
public MySqlConnection connection;
string server;
public string data_base;
string user_id;
string password;

public void inicializa()
{
server = "localhost";
data_base = "escola";
user_id = "root";
password = "usbw";
string connection_string;
string sslmode = "none";
connection_string = "SERVER=" + server + ";" + "DATABASE=" + data_base + ";" + "UID=" + user_id + "PASSWORD=" + password + ";" + "SslMode=" + sslmode + ";";
connection = new MySqlConnection(connection_string);
}

public bool open_connection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0: MessageBox.Show("Couldn't connect t DataBase."); break; // couldn't connect to database
case 1042: MessageBox.Show("Exceded the connection time"); break; // exceeded the connection time
case 1045: MessageBox.Show("Username/password are incorrect"); break;
}
return false;
}
}
public bool close_connection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}


Code for the Main Program:



public partial class consultas : Form
{
ligacao x = new ligacao();

public consultas()
{
InitializeComponent();
x.inicializa();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void consultas_Load(object sender, EventArgs e)
{
//define query
string query = "SELECT designacao FROM disciplinas";
//open connection
if (x.open_connection())
{
//create the comand and associates the query with the connection through the connector
MySqlCommand cmd = new MySqlCommand(query, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["designacao"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}

//define query
string queryBI = "SELECT bi FROM alunos";
//open connection
if (x.open_connection())
{
//create the commando and associate the query with the connection through the constructor
MySqlCommand cmd = new MySqlCommand(queryBI, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["bi"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}
}
}









share|improve this question
























  • Possible duplicate of How to form a correct MySQL connection string?
    – duDE
    Nov 7 at 12:44















up vote
0
down vote

favorite












I have a database to manage a school (students and classes).
I have a class with the code to connect to the DataBase and then I call the functions in the main program.
When I try to interact with the DataBase, it warns me that it could not connect to the DataBase or it exceeded the connection time.
I tried to add an ssslmode but it didn't work. I also tried to add a port but it didn't work.



Code for the class:



public class ligacao
{
public MySqlConnection connection;
string server;
public string data_base;
string user_id;
string password;

public void inicializa()
{
server = "localhost";
data_base = "escola";
user_id = "root";
password = "usbw";
string connection_string;
string sslmode = "none";
connection_string = "SERVER=" + server + ";" + "DATABASE=" + data_base + ";" + "UID=" + user_id + "PASSWORD=" + password + ";" + "SslMode=" + sslmode + ";";
connection = new MySqlConnection(connection_string);
}

public bool open_connection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0: MessageBox.Show("Couldn't connect t DataBase."); break; // couldn't connect to database
case 1042: MessageBox.Show("Exceded the connection time"); break; // exceeded the connection time
case 1045: MessageBox.Show("Username/password are incorrect"); break;
}
return false;
}
}
public bool close_connection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}


Code for the Main Program:



public partial class consultas : Form
{
ligacao x = new ligacao();

public consultas()
{
InitializeComponent();
x.inicializa();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void consultas_Load(object sender, EventArgs e)
{
//define query
string query = "SELECT designacao FROM disciplinas";
//open connection
if (x.open_connection())
{
//create the comand and associates the query with the connection through the connector
MySqlCommand cmd = new MySqlCommand(query, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["designacao"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}

//define query
string queryBI = "SELECT bi FROM alunos";
//open connection
if (x.open_connection())
{
//create the commando and associate the query with the connection through the constructor
MySqlCommand cmd = new MySqlCommand(queryBI, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["bi"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}
}
}









share|improve this question
























  • Possible duplicate of How to form a correct MySQL connection string?
    – duDE
    Nov 7 at 12:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a database to manage a school (students and classes).
I have a class with the code to connect to the DataBase and then I call the functions in the main program.
When I try to interact with the DataBase, it warns me that it could not connect to the DataBase or it exceeded the connection time.
I tried to add an ssslmode but it didn't work. I also tried to add a port but it didn't work.



Code for the class:



public class ligacao
{
public MySqlConnection connection;
string server;
public string data_base;
string user_id;
string password;

public void inicializa()
{
server = "localhost";
data_base = "escola";
user_id = "root";
password = "usbw";
string connection_string;
string sslmode = "none";
connection_string = "SERVER=" + server + ";" + "DATABASE=" + data_base + ";" + "UID=" + user_id + "PASSWORD=" + password + ";" + "SslMode=" + sslmode + ";";
connection = new MySqlConnection(connection_string);
}

public bool open_connection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0: MessageBox.Show("Couldn't connect t DataBase."); break; // couldn't connect to database
case 1042: MessageBox.Show("Exceded the connection time"); break; // exceeded the connection time
case 1045: MessageBox.Show("Username/password are incorrect"); break;
}
return false;
}
}
public bool close_connection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}


Code for the Main Program:



public partial class consultas : Form
{
ligacao x = new ligacao();

public consultas()
{
InitializeComponent();
x.inicializa();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void consultas_Load(object sender, EventArgs e)
{
//define query
string query = "SELECT designacao FROM disciplinas";
//open connection
if (x.open_connection())
{
//create the comand and associates the query with the connection through the connector
MySqlCommand cmd = new MySqlCommand(query, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["designacao"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}

//define query
string queryBI = "SELECT bi FROM alunos";
//open connection
if (x.open_connection())
{
//create the commando and associate the query with the connection through the constructor
MySqlCommand cmd = new MySqlCommand(queryBI, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["bi"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}
}
}









share|improve this question















I have a database to manage a school (students and classes).
I have a class with the code to connect to the DataBase and then I call the functions in the main program.
When I try to interact with the DataBase, it warns me that it could not connect to the DataBase or it exceeded the connection time.
I tried to add an ssslmode but it didn't work. I also tried to add a port but it didn't work.



Code for the class:



public class ligacao
{
public MySqlConnection connection;
string server;
public string data_base;
string user_id;
string password;

public void inicializa()
{
server = "localhost";
data_base = "escola";
user_id = "root";
password = "usbw";
string connection_string;
string sslmode = "none";
connection_string = "SERVER=" + server + ";" + "DATABASE=" + data_base + ";" + "UID=" + user_id + "PASSWORD=" + password + ";" + "SslMode=" + sslmode + ";";
connection = new MySqlConnection(connection_string);
}

public bool open_connection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0: MessageBox.Show("Couldn't connect t DataBase."); break; // couldn't connect to database
case 1042: MessageBox.Show("Exceded the connection time"); break; // exceeded the connection time
case 1045: MessageBox.Show("Username/password are incorrect"); break;
}
return false;
}
}
public bool close_connection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}


Code for the Main Program:



public partial class consultas : Form
{
ligacao x = new ligacao();

public consultas()
{
InitializeComponent();
x.inicializa();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void consultas_Load(object sender, EventArgs e)
{
//define query
string query = "SELECT designacao FROM disciplinas";
//open connection
if (x.open_connection())
{
//create the comand and associates the query with the connection through the connector
MySqlCommand cmd = new MySqlCommand(query, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["designacao"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}

//define query
string queryBI = "SELECT bi FROM alunos";
//open connection
if (x.open_connection())
{
//create the commando and associate the query with the connection through the constructor
MySqlCommand cmd = new MySqlCommand(queryBI, x.connection);
//create datareader and execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//show data in combobox1
if (dataReader.Read())
{
comboBox1.Items.Add(dataReader["bi"]);
}
//close dataReader
dataReader.Close();

//close connection
x.close_connection();
}
}
}






c# mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 12:39









mjwills

14.2k42439




14.2k42439










asked Nov 7 at 12:36









Joana Ferrão

105




105












  • Possible duplicate of How to form a correct MySQL connection string?
    – duDE
    Nov 7 at 12:44


















  • Possible duplicate of How to form a correct MySQL connection string?
    – duDE
    Nov 7 at 12:44
















Possible duplicate of How to form a correct MySQL connection string?
– duDE
Nov 7 at 12:44




Possible duplicate of How to form a correct MySQL connection string?
– duDE
Nov 7 at 12:44












2 Answers
2






active

oldest

votes

















up vote
0
down vote













I think there is something wrong with your connection string. Try using the MySqlConnectionStringBuilder:



MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);





share|improve this answer





















  • Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
    – Joana Ferrão
    Nov 10 at 16:56










  • @JoanaFerrão: You missed the culprit. Where do you get the error?
    – Palle Due
    Nov 12 at 9:40










  • it warns me that it could not connect to the DataBase or it exceeded the connection time
    – Joana Ferrão
    Nov 12 at 9:56


















up vote
-1
down vote













Try This :



   connection_string  = @"Data Source = " + server  + "; Initial Catalog = " + data_base  + "; Integrated Security=True;uid=myUser;password=myPass;";





share|improve this answer



















  • 1




    That won't help much if you want to connect with user name and password.
    – Palle Due
    Nov 7 at 12:48











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%2f53189623%2fc-sharp-doesnt-connect-to-database-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













I think there is something wrong with your connection string. Try using the MySqlConnectionStringBuilder:



MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);





share|improve this answer





















  • Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
    – Joana Ferrão
    Nov 10 at 16:56










  • @JoanaFerrão: You missed the culprit. Where do you get the error?
    – Palle Due
    Nov 12 at 9:40










  • it warns me that it could not connect to the DataBase or it exceeded the connection time
    – Joana Ferrão
    Nov 12 at 9:56















up vote
0
down vote













I think there is something wrong with your connection string. Try using the MySqlConnectionStringBuilder:



MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);





share|improve this answer





















  • Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
    – Joana Ferrão
    Nov 10 at 16:56










  • @JoanaFerrão: You missed the culprit. Where do you get the error?
    – Palle Due
    Nov 12 at 9:40










  • it warns me that it could not connect to the DataBase or it exceeded the connection time
    – Joana Ferrão
    Nov 12 at 9:56













up vote
0
down vote










up vote
0
down vote









I think there is something wrong with your connection string. Try using the MySqlConnectionStringBuilder:



MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);





share|improve this answer












I think there is something wrong with your connection string. Try using the MySqlConnectionStringBuilder:



MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
builder.Host = "localhost";
builder.UserId = "root";
builder.Database = "escola";
builder.Password = "usbw";
connection = new MySqlConnection(builder.ConnectionString);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 12:56









Palle Due

1,8551717




1,8551717












  • Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
    – Joana Ferrão
    Nov 10 at 16:56










  • @JoanaFerrão: You missed the culprit. Where do you get the error?
    – Palle Due
    Nov 12 at 9:40










  • it warns me that it could not connect to the DataBase or it exceeded the connection time
    – Joana Ferrão
    Nov 12 at 9:56


















  • Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
    – Joana Ferrão
    Nov 10 at 16:56










  • @JoanaFerrão: You missed the culprit. Where do you get the error?
    – Palle Due
    Nov 12 at 9:40










  • it warns me that it could not connect to the DataBase or it exceeded the connection time
    – Joana Ferrão
    Nov 12 at 9:56
















Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
– Joana Ferrão
Nov 10 at 16:56




Thank you for the help but now an error shows up saying "CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)". Do you know how to fix it?
– Joana Ferrão
Nov 10 at 16:56












@JoanaFerrão: You missed the culprit. Where do you get the error?
– Palle Due
Nov 12 at 9:40




@JoanaFerrão: You missed the culprit. Where do you get the error?
– Palle Due
Nov 12 at 9:40












it warns me that it could not connect to the DataBase or it exceeded the connection time
– Joana Ferrão
Nov 12 at 9:56




it warns me that it could not connect to the DataBase or it exceeded the connection time
– Joana Ferrão
Nov 12 at 9:56












up vote
-1
down vote













Try This :



   connection_string  = @"Data Source = " + server  + "; Initial Catalog = " + data_base  + "; Integrated Security=True;uid=myUser;password=myPass;";





share|improve this answer



















  • 1




    That won't help much if you want to connect with user name and password.
    – Palle Due
    Nov 7 at 12:48















up vote
-1
down vote













Try This :



   connection_string  = @"Data Source = " + server  + "; Initial Catalog = " + data_base  + "; Integrated Security=True;uid=myUser;password=myPass;";





share|improve this answer



















  • 1




    That won't help much if you want to connect with user name and password.
    – Palle Due
    Nov 7 at 12:48













up vote
-1
down vote










up vote
-1
down vote









Try This :



   connection_string  = @"Data Source = " + server  + "; Initial Catalog = " + data_base  + "; Integrated Security=True;uid=myUser;password=myPass;";





share|improve this answer














Try This :



   connection_string  = @"Data Source = " + server  + "; Initial Catalog = " + data_base  + "; Integrated Security=True;uid=myUser;password=myPass;";






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 12:52

























answered Nov 7 at 12:45









Rajendran S

1317




1317








  • 1




    That won't help much if you want to connect with user name and password.
    – Palle Due
    Nov 7 at 12:48














  • 1




    That won't help much if you want to connect with user name and password.
    – Palle Due
    Nov 7 at 12:48








1




1




That won't help much if you want to connect with user name and password.
– Palle Due
Nov 7 at 12:48




That won't help much if you want to connect with user name and password.
– Palle Due
Nov 7 at 12:48


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189623%2fc-sharp-doesnt-connect-to-database-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini