you can create a Class for this:
Only before that we’ll add one ConnectionStrings
at the App.config
of your project. Once located and opened the App.config
before the closing tag <\configuration>
, add:
<connectionStrings>
<add name="CaminhoSqlSever"
providerName="System.Data.SqlClient"
connectionString="Server=Nome do seu Server;
Database=Nome do seu Banco;
User Id=seu usuário;
Password=Seu password;"/>
</connectionStrings>
Done this add the necessary references to the project:
using System.Configuration; //-> Com ele podemos acessar a ConnectionString criada no `App.config`
using System.Data.SqlClient;
There are several ways to create this class, below follows a simple example.
class ConexaoDB
{
//-->Variavel que armazena a conexão.
SqlConnection Con;
//Propriedade que encapsula e retorna a conexão.
public SqlConnection Conexao => Con;
//Propriedade que retorna a string da conexão armazenada no arquivo de configurações.
public string StrConexao => ConfigurationManager.ConnectionStrings["CaminhoSqlSever"].ConnectionString;
public ConexaoDB()
{
try
{
Con = new SqlConnection(StrConexao);
}
catch (SqlException erro)
{
MessageBox.Show($"Ocorreu um erro: {erro.message}");
}
}
}
which database? You have to say what you are using or which one you want to use.
– Paulo Ricardo
The database I defined yesterday is sql Server. But where I worked they had a specific webserver to make the Connections, so the doubt, if you can help me I will be very grateful, this part is catching me a lot. Thank you!
– Jonathan Igor Bockorny Pereira
you’ve heard of connection string?
– Leonardo
Connection string already, but what happens, I work as a developer as a whole has 5 months, where I worked for 3 months their webservice was sqld.Executenonquery("code of the bank"); However I went to see and it is different and the explanations so far are a little confused, did not give me a north you know?
– Jonathan Igor Bockorny Pereira