简体   繁体   English

SQL查询UPDATE不能正常工作

[英]SQL query UPDATE is not working asp.net

I have a simple asp.net website and a simple database. 我有一个简单的asp.net网站和一个简单的数据库。

The database has a table Users with this structure 数据库有一个表具有这种结构的Users

在此输入图像描述

Now I have been trying to execute this SQL command 现在我一直在尝试执行这个SQL命令

UPDATE [Users] 
SET [UserName] = 'Teazst22',
    [Password] = 'Tesst',
    [Email] = 'Test@gmail.com',
    [FirstName] = 'Test12',
    [LastName] = 'Work1',
    [BirthDate] = '30/1/1920' 
WHERE [Email] = 'Test@gmail.com'

Command is executed from my C# code: 命令从我的C#代码执行:

    public static void ChangeTable(string strSql, string FileName)
{
    try
    {
        OleDbConnection c = MakeConnection(FileName);
        OleDbCommand comm = new OleDbCommand();
        comm.CommandText = strSql;
        comm.Connection = c;
        comm.ExecuteNonQuery();
        c.Close();
        System.Diagnostics.Debug.WriteLine("SQL COMMAND Executed");
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine(e.ToString());
    }
}

} }

public static OleDbConnection MakeConnection(string dbFile)
{
    OleDbConnection c = new OleDbConnection();

    if (dbFile.ToLower().Contains(".accdb"))
        // MS Access >=2007
        c.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/" + dbFile);
    else
        // MS Access 2003
        c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/" + dbFile);

    c.Open();
    return c;

} }

 private String Email = "";
private String Username = "";
protected void Page_Load(object sender, EventArgs e)
{
    if ((Session["IsAdmin"] != null && !(bool)Session["IsAdmin"]) || Session["IsAdmin"] == null)
        Response.Redirect("Default.aspx");

   String StrSql = "Select * from [Users] where Email='{0}'";
    StrSql = String.Format(StrSql, Request.QueryString.Get("Em"));
   DataTable Dt=  MyDbase.SelectFromTable(StrSql, "Db.ACCDB");
    if (Dt.Rows.Count == 1)
    {
        USTXT.Text = Dt.Rows[0]["UserName"].ToString();
        PTXT.Text = Dt.Rows[0]["Password"].ToString();
        EmailTXT.Text = Dt.Rows[0]["Email"].ToString();
        FirstNTXT.Text = Dt.Rows[0]["FirstName"].ToString();
        LastNTXT.Text = Dt.Rows[0]["LastName"].ToString();
        BirthDTXT.Text = Dt.Rows[0]["BirthDate"].ToString();
        Email = EmailTXT.Text;
        Username = USTXT.Text;
    }

}
protected void SaveBTN_Click(object sender, EventArgs e)
{
    String StrSql = "Update [Users] Set [UserName]='{0}',[Password]='{1}',[Email]='{2}',[FirstName]='{3}',[LastName]='{4}',[BirthDate]='{5}' WHERE [Email]='{6}'";
    StrSql = String.Format(StrSql, USTXT.Text, PTXT.Text, EmailTXT.Text, FirstNTXT.Text, LastNTXT.Text, BirthDTXT.Text , Email);

    MyDbase.ChangeTable(StrSql,"Db.ACCDB");
    Response.Redirect("Admin.aspx");
}

When executing the code no errors are shown, but when I check the database, nothing has changed... quite simply, the command is not working. 执行代码时没有显示错误,但是当我检查数据库时,没有任何改变......很简单,命令不起作用。

PS: the database that I'm using is Microsoft Access 2007 PS:我使用的数据库是Microsoft Access 2007

Please make sure that the mdb file you are observing is in "Data Directory" folder. 请确保您观察的mdb文件位于“Data Directory”文件夹中。 you might be checking wrong file. 你可能正在检查错误的文件。 Also check the property of that mdb file, whether its last modified time is approx to the time you performed database operation. 还要检查该mdb文件的属性,其上次修改时间是否大约是您执行数据库操作的时间。

Simply this code put inside the try catch statement and try to catch your error .. as i seem definitely you can find the problem in this way 只需将此代码放入try catch语句中并尝试捕获您的错误..因为我似乎绝对可以通过这种方式找到问题

try 
{
    OleDbConnection c = MakeConnection(Database name);
    OleDbCommand comm = new OleDbCommand();
    comm.CommandText = SqlStr;
    comm.Connection = c;
    comm.ExecuteNonQuery();
    c.Close();
}
catch  (Exception e)
{
    Console.WriteLine(e.Message.ToString());
}

It might be that you have a db collation which is case sensitive and you are comparing uppercase letter to lowercase letter? 可能是您有一个区分大小写的db排序规则并且您要将大写字母与小写字母进行比较? https://msdn.microsoft.com/en-us/library/ms144250(v=sql.105).aspx https://msdn.microsoft.com/en-us/library/ms144250(v=sql.105).aspx

where test = Test would not gives any results if it's case sensitive test =如果区分大小写,则测试不会给出任何结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM