December
11
2008
corso c sharp realizzare un form da mandare al db per eseguire le queries
Tutorial->Programmazione-tutorial->tutorial C Sharp
File aspx.cs
- using System;
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void senddati_Click(object sender, EventArgs e)
- {
- string strConn = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString; ;
- Myc.Open();
- SqlCommand Mycommand = new SqlCommand(TxtSql.Text, Myc);
- try
- {
- int rows = Mycommand.ExecuteNonQuery();
- TxtSql.Text += "Righe affected" + rows.ToString();
- }
- catch (Exception error) {
- TxtSql.Text += error.Message ;
- }
- }
- }
File aspx
- <asp:content id="Content1" contentplaceholderid="head" runat="Server"> </asp:content>
- <asp:content id="Content2" contentplaceholderid="ContentPlaceHolder1" runat="Server"> <asp:textbox id="TxtSql" runat="server" height="93px" width="371px"> Testo </asp:textbox> <asp:button id="senddati" runat="server" text="Send Sql" onclick="senddati_Click"></asp:button> </asp:content>
In questo caso abbiamo usato una Master Page per il layout del sito, cosa che si invita a rispettare per evitare duplicati del codice.
Usiamo l'oggetto SqlCommand per mandare i dati attraverso il metodo ExecuteNonQuery.
Se la sintassi non viene ben scritta, riceviamo un errore che viene catturato per mezzo del catch.






















