问题
I have this table which contains a few's questions:
<table>
<tr><td><b>Utilizador:</b></td><td><asp:Label ID="username" runat="server"></asp:Label></td></tr>
<tr><td><b>Telefone da Empresa:</b></td><td><asp:TextBox ID="empresa" runat="server" MaxLength="13"></asp:TextBox> (Exemplo: +351234925xxx)</td></tr>
<tr><td><b>2º Telefone:</b></td><td><asp:TextBox ID="empresa2" runat="server" MaxLength="4"></asp:TextBox> (Exemplo: xxxx)</td></tr>
<tr><td><b>Telemóvel:</b></td><td><asp:TextBox ID="telemovel" runat="server" MaxLength="13"></asp:TextBox> (Exemplo: +3519xxxxxxxx)</td></tr>
<tr><td colspan="2"><asp:Label ID="lblInfo" runat="server" Font-Bold="True"></asp:Label></td></tr>
<tr><td><asp:Button ID="cmdSave" runat="server" Text="Guardar" onclick="cmdSave_Click" /></td><td></td></tr>
</table>
my goal is
when page loads verify if the username.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
i pretent do verify if already exist in access database if YES show the table with the information to update
if NO show the table to insert the information
how can I do such thing working?
my code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Security.Principal;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
username.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
}
protected void cmdSave_Click(object sender, EventArgs e)
{
string sFilePath = Server.MapPath("Database3.accdb");
OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
string insertCmd = "INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)";
using (Conn)
{
Conn.Open();
OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
myCommand.Parameters.AddWithValue("@Empresa", empresa.Text);
myCommand.Parameters.AddWithValue("@Empresa2", empresa2.Text);
myCommand.Parameters.AddWithValue("@Telemovel", telemovel.Text);
myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name);
Response.Write(" ");
myCommand.ExecuteNonQuery();
lblInfo.Text = "Dados guardados!";
lblInfo.ForeColor = System.Drawing.Color.Green;
}
}
}
回答1:
If I'm getting you correctly then you want to update record if already there with this username or insert. So to fix this I'm making some change in your query only but i would say to use a stored procedure instead of using query like this.
protected void cmdSave_Click(object sender, EventArgs e)
{
string sFilePath = Server.MapPath("Database3.accdb");
OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sFilePath + ";Persist Security Info=False;");
string insertCmd = "IF NOT EXISTS(Select * FROM colaborador where username=@username)"+
"INSERT INTO colaborador(Empresa,Empresa2,Telemovel,username) VALUES (@Empresa,@Empresa2,@Telemovel,@username)"+
"ELSE"+
"Update colaborador SET Empresa=@Empresa,Empresa2=@Empresa2,Telemovel=@Telemovel where username=@username";
using (Conn)
{
Conn.Open();
OleDbCommand myCommand = new OleDbCommand(insertCmd, Conn);
myCommand.Parameters.AddWithValue("@Empresa", empresa.Text);
myCommand.Parameters.AddWithValue("@Empresa2", empresa2.Text);
myCommand.Parameters.AddWithValue("@Telemovel", telemovel.Text);
myCommand.Parameters.AddWithValue("@username", HttpContext.Current.User.Identity.Name);
Response.Write(" ");
myCommand.ExecuteNonQuery();
lblInfo.Text = "Dados guardados!";
lblInfo.ForeColor = System.Drawing.Color.Green;
}
}
来源:https://stackoverflow.com/questions/31515456/verify-if-username-exist-to-show-the-database-or-sho-table-to-field