我有这个表,其中包含一些问题:
<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>
我的目标是
页面加载时验证username.Text = "[" + HttpContext.Current.User.Identity.Name + "]";
i内容是否确认访问数据库中是否已存在是显示包含要更新的信息的表
如果否显示要插入信息的表
我该怎么办呢?
我的代码背后:
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;
}
}
}
答案 0 :(得分:0)
如果我找到了您正确的信息,那么您想要使用此用户名或插入信息更新记录。所以为了解决这个问题,我只对您的查询进行了一些更改,但我会说使用存储过程而不是像这样使用查询。
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;
}
}