visual studio sql error

2

Well, I'm new to C # and then I'm seeing a few database connection tutorials that appear to me this error:

ThecodeI'vedoneisallstillbasedonabasictutorialandI'llleaveithereforyoutosee

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? Thanks

    
asked by anonymous 13.07.2015 / 12:17

2 answers

0

Maybe this could be an error in your connection string

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

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

    
13.07.2015 / 18:13
-1

One question: Is the instance name you passed? Try to pass Nome_da_Máquina\Nome_da_Instância . Open Sql Management or VS2013 and copy as it's there and paste it into your code (avoid mistakes, rs). If it does not resolve, check access and etc.

I did a quick method here to simulate a user login and it works. Make the right arrangements and see if it works for 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 move to your App.config

<add name="conectDarf" connectionString="Server=COMPUTADOR-PC\SQLEXPRESS;initial catalog=INETGLOBAL;User ID=USUARIO;Password=SENHA"/>
    
13.07.2015 / 13:26