Jump to content

sql no C#


Recommended Posts

o que é que esta mal aqui


     SqlConnection conn = new SqlConnection();
            conn.ConnectionString =
                          "Data Source=(local);" +
                          "Initial Catalog=bd;" +
                          "Integrated Security=SSPI;";
            SqlCommand sqlComm = new SqlCommand("SELECT * FROM utilizador WHERE user=@user;", conn);
            sqlComm.Parameters.Add("@user", SqlDbType.VarChar);
            sqlComm.Parameters["@user"].Value = user.Text;

            SqlDataReader rdr = sqlComm.ExecuteReader();
            string valida = rdr.HasRows.ToString();
            label1.Text = valida.ToString();
Link to comment
Share on other sites

pq n utilizas SP(store procedures), sempre é mais fácil... esta é a minha opinião!!

codigo com SP,

SqlConnection dbLogin = new SqlConnection(ConfigurationSettings.AppSettings["Liga_BD"]);

SqlDataAdapter spLogin = new SqlDataAdapter ("spLogin", dbLogin);

spLogin.SelectCommand.CommandType = CommandType.StoredProcedure;

SqlParameter paramUser = new SqlParameter("@username", SqlDbType.NVarChar, 20);

paramUser.Value = txtUser.Text;


este código crias no ficheiro webconfig do teu projecto, assim sempre q kiseres ligar a BD, xamas 'Liga_BD',


<add key="Liga_BD"



user id=root;



este codigo é pra VS C# 2003, e SQL server 2003; como n indicaste kal estas a utilizar... 😁

espero ter ajudado, fika bem...

P.S.: aguardo resposta aki 😉

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.