C# 从ASP网页插入一行数据以使用OleDB访问数据库
我正在尝试将一行数据插入MS Access数据库。当我通过VS调试时,它不会发现问题,但不会插入行。这个查询没有插入任何行。我的日期是字符串,格式正确。我在参数列表中传递了18个值,必须有一种更简单的方法。有没有其他方法可以使用OleDb的参数?我在下面贴了我的方法。你能看看我的语法是否正确吗?还是不行 这里是我传递信息的地方:C# 从ASP网页插入一行数据以使用OleDB访问数据库,c#,database,ms-access,parameters,oledb,C#,Database,Ms Access,Parameters,Oledb,我正在尝试将一行数据插入MS Access数据库。当我通过VS调试时,它不会发现问题,但不会插入行。这个查询没有插入任何行。我的日期是字符串,格式正确。我在参数列表中传递了18个值,必须有一种更简单的方法。有没有其他方法可以使用OleDb的参数?我在下面贴了我的方法。你能看看我的语法是否正确吗?还是不行 这里是我传递信息的地方: rowsAdded = ((DataAccessLayer)Application["dbAccess"]).insert("Employees", txtLname.
rowsAdded = ((DataAccessLayer)Application["dbAccess"]).insert("Employees", txtLname.Text, txtFname.Text, txtTitle.Text, txtCourt.Text,
txtAddress.Text, txtCity.Text, txtCountry.Text, txtHomePhone.Text, txtExtension.Text, int.Parse(txtReports.Text), txtPassword.Text,
txtPostalCode.Text, txtNotes.Text, txtRegion.Text, txtHireDate.Text, txtBday.Text, upPhoto.FileName.ToString());
这里是我用这个方法查询数据库的地方
public int insert(string tablename, string lname, string fname, string title, string toc, string address, string city,
string country, string phone, string ext, int report, string pass, string postal, string notes, string region,
string hire, string birth, string photo)
{
string tblName = tablename;
string last = lname;
string first = fname;
string tlt = title;
string tOfc = toc;
string addy = address;
string town = city;
string reg = country;
string phum = phone;
string exten = ext;
int rep = report;
string pas = pass;
string pc = postal;
string note = notes;
string regions = region;
string hD = hire;
string bD = birth;
string pho = photo;
int rows = 0;
int ID = 0;
string insertString = "INSERT INTO @tablename ([EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[Address],[City]," +
"[Country],[HomePhone],[Extension],[ReportsTo],[Password],[PostalCode],[Notes],[Region], [HireDate],[BirthDate],[Photo]) VALUES (" +
ID + ", @lname, @fname, @title, @toc, @addy, @city, @country," +
"@phone, @ext, @report, @pass, @postal, @notes,@region, @hire, @birth, @photo)";
string queryLast = "Select @@Identity";
try
{
conn.Open();
oleCommand = new OleDbCommand(insertString, conn);
oleCommand.CommandText = queryLast;
ID = (int)oleCommand.ExecuteScalar();
oleCommand.Parameters.AddWithValue("@tablename", tblName);
oleCommand.Parameters.AddWithValue("@lname", last);
oleCommand.Parameters.AddWithValue("@fname", first);
oleCommand.Parameters.AddWithValue("@title", tlt);
oleCommand.Parameters.AddWithValue("@toc", tOfc);
oleCommand.Parameters.AddWithValue("@addy", addy);
oleCommand.Parameters.AddWithValue("@city", town);
oleCommand.Parameters.AddWithValue("@country", reg);
oleCommand.Parameters.AddWithValue("@phone", phum);
oleCommand.Parameters.AddWithValue("@ext", exten);
oleCommand.Parameters.AddWithValue("@report", rep);
oleCommand.Parameters.AddWithValue("@pass", pas);
oleCommand.Parameters.AddWithValue("@region", regions);
oleCommand.Parameters.AddWithValue("@postal", pc);
oleCommand.Parameters.AddWithValue("@notes", note);
oleCommand.Parameters.AddWithValue("@hire", hD);
oleCommand.Parameters.AddWithValue("@birth", bD);
oleCommand.Parameters.AddWithValue("@photo", pho);
oleCommand.ExecuteNonQuery();
rows = (int)oleCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
dbError = "Add Employee command Error: " + ex.Message;
}
finally
{
conn.Close();
}
return rows;
}
在代码中,您不会使用
CommandText
作为insertString
执行OLECTOMAND
。所有三次执行都是在命令文本等于queryLast
的情况下完成的
您将insertString
传递给OleDbCommand
构造函数的事实并不意味着在分配后它会以某种方式被记住
oleCommand.CommandText = queryLast;
在这个赋值中,oleCommand
忘记了insertString
,并且只执行它的CommandText
中的内容,直到您分配了其他内容
另一件事:如果我没有弄错的话,执行insertString
可能会引发异常,因为sql参数化不支持指定为参数的表名
如果EmployeeID列是自动递增的,则不要将其包含在插入中。再说一次,你需要直走:
你确定你能说@tablename吗?我想你做不到。如果我“插入”+tblName+”怎么办(…?那不是,工作表名称不能参数化是的,+tablename+如果EmployeeID列是自动递增的-不要将其包含在插入中。同样,您需要明确步骤:1.使用insertString创建命令,2.指定参数,3.执行插入,4.将QueryList分配给CommandText,5.执行给get最后一个id。我刚刚尝试添加
oleCommand.CommandText=insertString
,但它仍然没有返回任何行。当我调用MyoleCommand=new-OleDbCommand(insertString,conn)时,我的insertString被实例化;
,而queryString
是OLECTOMAND的一个附加组件
我明白了,我可以将我的ExecuteOnQuery
放在CommandText和LOL之前:字段太小,无法接受您试图添加的数据量。尝试插入或粘贴较少的数据。可能是我包含ID的原因。如果我排除aut,会使用ID吗o递增数字?我不明白你在说什么。对于自动递增的字段,在INSERT sql语句中既不包含它们的名称,也不包含它们的值。