visual studio sql error

Asked

Viewed 181 times

2

Good people, I’m new in c# and then I’m seeing some tutorials connecting to the database only that this error appears to me:

erro

The code I made is all still based on a basic tutorial and I leave it here for you to see

using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace teste
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection connection = new SqlConnection("Server=.\\SQLEXPRESS;Database=teste; Integrated Security = true");
            {
                connection.Open();
                //
                // The SqlCommand should be created inside a using statement.
                // ... It receives the SQL statement as the first argument.
                // ... It receives the connection object as the second argument.
                // ... The SQL text only works with a specific database.
                //
                using (SqlCommand command = new SqlCommand(
                "SELECT TOP 3 * FROM my_cab",
                connection))
                {
                    //
                    // Instance methods can be used on the SqlCommand instance.
                    // ... These read data from executing the command.
                    //
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            for (int i = 0; i < reader.FieldCount; i++)
                            {
                                Console.WriteLine(reader.GetValue(i));
                            }
                            Console.WriteLine();
                        }
                    }
                }
            }
        }
    }
}

What’s wrong with it? Thank you

2 answers

0


Maybe it’s an error in your connection string

"Server=.\\SQLEXPRESS;Database=teste; Integrated Security = true"

Ensure that SQL Server Express Edition is installed and the database test exist on your local machine.

  • The problem was even the connection string, I saw on the net another way to do it and already gave. Thanks!

  • @Hugo If this answer solved the problem consider marking it as the right answer. This helps other users who experience a similar problem to identify the solution.

  • I had scored, I don’t know what happened, but I already scored again. Thanks for the warning ;)

-1

A question: Is the name of the instance you passed? Try to pass the Nome_da_Máquina\Nome_da_Instância. Open the Sql Management or the VS2013 and copy as it is there and paste there in your code (avoid erring,rs). If not solve, check accesses and etc.

I made a quick method here to simulate a user login and it works. Make the proper arrangements and see if it works with you.

My method:

public bool login(string usu, string pwd)
        {
            bool retorno = false;

            string sql = "select idusuario,nm_usuario, tipo_usuario,senha_usuario from tbl_usuario where nm_usuario = '" + usu + "' and senha_usuario = '" + pwd + "'";

            SqlConnection conexao = new SqlConnection();
            conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conectDarf"].ConnectionString;

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;

            try
            {
                conexao.Open();
                cmd.Connection = conexao;

                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    retorno = true;
                }

            }
            catch (Exception e)
            {
                Erro = e.Message;
                retorno = false;
            }
            finally
            {
                conexao.Close();
                cmd.Dispose();
            }

            return retorno;
        }

And so it is in Web.config, but you can pass to your App.config

<add name="conectDarf" connectionString="Server=COMPUTADOR-PC\SQLEXPRESS;initial catalog=INETGLOBAL;User ID=USUARIO;Password=SENHA"/>
  • The strange thing is that if I do a project of type windows form application it works but in this case it is a project of type Console and no longer gives so should not be the name of the instance because if it would not go wrong in the two I think

  • Hugo, ignore the downvote answer. There’s no reason for that, but there are some guys here who think they are. I know because, it is because I did not put machine name and instance, but these ... should know that this is a generic way of answering, even because I do not know your machine and etc..

  • Thanks pnet but already found the answer! The error was in the string Connection, I tried to do otherwise that I saw on the net and already gave

Browser other questions tagged

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