Damon4hire Posted September 21, 2009 at 10:39 PM Report Share #288083 Posted September 21, 2009 at 10:39 PM Boas pessoal Estou a fazer um pequeno projecto para ser avaliado e estou a ter uns precalços a lidar com a interacção dos 2 programas. Já sei como executar stored procedures a partir do C#. A ideia é inserir um novo registo (sócio de um clube) e receber o campo identidade desse registo(@@identity, certo?), mas não sei como receber o valor retornado no C#. Uma ajuda por favor? ? Link to comment Share on other sites More sharing options...
bruno1234 Posted September 21, 2009 at 10:43 PM Report Share #288084 Posted September 21, 2009 at 10:43 PM Fazes uma stored procedure em q depois do insert fazes: select @@identity ou select scope_identity() No C# fazes um ExecuteScalar. Este método retorna um objecto q deves converter parar o valor q queres. Outra maneira é colocares um parametro de output na procedure q afectas com o valor da identidade. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Damon4hire Posted September 21, 2009 at 11:40 PM Author Report Share #288089 Posted September 21, 2009 at 11:40 PM epa não deu. Fiz var = sqlcommand.ExecuteScalar(); diz que está a sintaxe errada. Falta alguma coisa? Link to comment Share on other sites More sharing options...
bruno1234 Posted September 22, 2009 at 08:25 PM Report Share #288188 Posted September 22, 2009 at 08:25 PM Isso assim n é nada. Tens q ter uma ligação à base de dados, uma connection string,... Só com essa linha n é possivel ajudar-te. Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Saco Posted September 24, 2009 at 09:24 PM Report Share #288547 Posted September 24, 2009 at 09:24 PM epa não deu. Fiz var = sqlcommand.ExecuteScalar(); diz que está a sintaxe errada. Falta alguma coisa? Falta. Falta o nome da variável. var xpto = sqlcommand.ExecuteScalar(); Link to comment Share on other sites More sharing options...
bruno1234 Posted September 24, 2009 at 09:27 PM Report Share #288549 Posted September 24, 2009 at 09:27 PM Pois, isso sem o nome da variável nem sequer compila. A menos q seja uma nova feature do C# 😉 Matraquilhos para Android. Gratuito na Play Store. https://play.google.com/store/apps/details?id=pt.bca.matraquilhos Link to comment Share on other sites More sharing options...
Saco Posted September 24, 2009 at 09:27 PM Report Share #288550 Posted September 24, 2009 at 09:27 PM Ele disse logo que a sintaxe estava errada... Link to comment Share on other sites More sharing options...
EclipseX Posted December 11, 2009 at 04:29 PM Report Share #299861 Posted December 11, 2009 at 04:29 PM public partial class admin : System.Web.UI.Page { public SqlConnection SQLConn = new SqlConnection(@"Data Source=xxxxx;Initial Catalog=db_xxxxx;Integrated Security=True"); public void Page_Load(object sender, EventArgs e) { Session.Timeout = 5; SQLConn.Open(); xxx xxx xxx isto conta todos os registos public void contador(int registos) { SqlCommand cmd = new SqlCommand("SELECT * FROM xxx WHERE xxx = '" + xxx + "'", SQLConn); SqlDataReader contador = cmd.ExecuteReader(); while (contador.Read()) { registos += 1; } contador.Close(); conta = registos; } isto e para escrever string sqlIns = "INSERT INTO xxxxxx VALUES ('" + xxxxxx + "'" + ", '" +xxxxx+ "'" + ", '" + xxxxx + "'" + ", '" + xxxxx + "'" + ", '" +xxxx + "'" + ", '" + xxxxxxt + "'" + ", '" +xxxxxxx + "'" + ", '" + xxxxxxx + "'" + ", '" + xxxxxx+ "'" + ",'','" +xxxxxx + "')"; SqlCommand resultado = new SqlCommand(sqlIns, SQLConn); try { resultado.ExecuteNonQuery(); } catch (Exception ex) { Response.Write("Error: " + ex.Message); return; } este exemplo e para apagar um registo de uma tabela, que esta ligado a um outro registo de outras tabelas... public void ApagarSelecionado(object sender, EventArgs e) { conn.Open(); SqlCommand apagar1 = new SqlCommand("DELETE FROM xxxxxWHERExxxxx in(select xxxxx.xxxxx from ,xxxxxx where xxxxx = '" + xxxxx + "' and cxxxxxx.xxxxx.xxxxx)",conn); SqlCommand apagar2 = new SqlCommand("DELETE FROM xxxxx WHERE xxxxx= '" + xxxx + "'", conn); try { apagar1.ExecuteNonQuery(); apagar2.ExecuteNonQuery(); Items.Clear(); Logoff(); Abrir(); } catch (Exception ex) { Response.Write("Error: " + ex.Message); return; } } enfim, tens aki montes de exemplos do que podes fazer.....para auto incrementares, basta fazeres na base de dados, e a chave k estiver no sql e k tem de ser incrementada, para inserir o registo no c# basta inserires sem a chave...para veres, usas o exemplo que te deram Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now