Connection to SQL database in C#

Asked

Viewed 1,958 times

0

Good morning friends,

I am doubtful about database connection in particular for use in a project that captures database information for a Datagridview, my first problem is that table information is not coming, my doubt, I can connect the database only with the connection string, or I need beyond the connection string add the database by the Configuration Wizard data base of the visual studio ?

  • When defining the connection string, you also define the database you want to access. For example: connectionString="Data Source=. sqlexpress;Initial Catalog=MEUBD;Integrated Security=True"

  • hum got it, I’m using objects to access the database : public class Daoutils { public Static Dbconnection Getconexao() { string cnx = @"Server=. /Sqlexpress14;Database=Silo;User Id=sa;Password=avila1334;"; Connected Dbconnection = new Sqlconnection(cnx); connection.Open(); Connected Return;

  • That may be my mistake ?

  • Returns any errors? Because you are only connecting to the database, but do not execute any query.

  • That, it returns nothing

1 answer

1


See if this example helps you:

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;

cmd.CommandText = "SELECT * FROM Customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

reader = cmd.ExecuteReader();
// Aqui os dados são acessados através do objeto dataReader
sqlConnection1.Close();
  • 1

    Thank you very much Vinicios, I discovered what was happening, I was not putting the DGV to load correct in the form , so the data did not come , ended up thinking that the problem was the bank, thanks friend

Browser other questions tagged

You are not signed in. Login or sign up in order to post.