我有一个简单的asp.net网站和一个简单的数据库。
数据库有一个具有此结构的表Users
现在我一直在尝试执行这个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'
命令从我的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");
}
执行代码时没有显示错误,但是当我检查数据库时,没有任何改变......很简单,命令不起作用。
PS:我使用的数据库是Microsoft Access 2007
答案 0 :(得分:0)
请确保您观察的mdb文件位于“Data Directory”文件夹中。你可能正在检查错误的文件。还要检查 mdb 文件的属性,确定其上次修改时间是否与执行数据库操作的时间大致相同。
答案 1 :(得分:0)
只需将此代码放入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());
}
答案 2 :(得分:0)
您可能有一个区分大小写的db排序规则并且您将大写字母与小写字母进行比较? https://msdn.microsoft.com/en-us/library/ms144250(v=sql.105).aspx
其中test = Test如果区分大小写,则不会给出任何结果