Last active
September 23, 2018 17:58
-
-
Save pablohildo/9962368f60f8f92849e707b9856a6175 to your computer and use it in GitHub Desktop.
Database connection with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MySql.Data.MySqlClient; | |
static void Main() | |
{ | |
var conn = "Server=localhost;Database=SEU_BANCO_AQUI;Uid=SEU_USUARIO_AQUI;Pwd=SUA_SENHA_AQUI"; // Dados do banco | |
var connection = new MySqlConnection(conn); // Cria a conexão | |
var command = connection.CreateCommand(); // Cria um comando a ser executado | |
try | |
{ | |
connection.Open(); // Abre a conexão | |
connection.CommandText = "INSERT INTO BATATA('NOME') VALUES ('ARROZ')"; // Texto do comando a ser executado | |
command.ExecuteNonQuery(); // Executa um comando que não tem retorno (Insert, update, delete); se fosse select, seria ExecuteReader | |
} | |
finally | |
{ | |
if(connection.State == ConnectionState.Open) // Se a conexão estiver aberta | |
connection.Close(); // Fecha a conexão | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment