我用如下方式在Oracle数据库中创建的表
create table test(
col1 char(10),
col2 int,
primary key(col1))
insert into test values('aaa',1);
insert into test values('bbb',2);
insert into test values('ccc',3);
insert into test values('ddd',4);
commit然后用如下语句去执行删除
static void Delete()
{
OracleConnection conn = new OracleConnection("Server=fzjclk_192.168.51.12;Password=fzjc;User ID=fzjc");
OracleDataAdapter adp = new OracleDataAdapter();
DataSet ds = new DataSet(); try
{
OracleCommand cmd = new OracleCommand("select * from test", conn);
adp.SelectCommand = cmd; OracleCommand deleteCmd = new OracleCommand("delete from test where col1=?", conn);
deleteCmd.Parameters.Add("col1", OracleType.Char, 10, "col1");
adp.DeleteCommand = deleteCmd; adp.Fill(ds, "test"); DataTable table = ds.Tables["test"];
DataRow row = table.Rows[0];
row.Delete(); adp.Update(ds, "test"); // 此行报错 table.AcceptChanges();
ds.AcceptChanges();
Console.WriteLine("deleted");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (conn != null) conn.Close();
}
}执行后输出:System.Data.OracleClient.OracleException: ORA-01036: 非法的变量名/编号 在 System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, B
atchCommandInfo[] batchCommands, Int32 commandCount)
在 System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCo
mmandInfo[] batchCommands, Int32 commandCount)
在 System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
在 System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tab
leMapping)
在 System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)请教大家,变量名那块有错呢?
create table test(
col1 char(10),
col2 int,
primary key(col1))
insert into test values('aaa',1);
insert into test values('bbb',2);
insert into test values('ccc',3);
insert into test values('ddd',4);
commit然后用如下语句去执行删除
static void Delete()
{
OracleConnection conn = new OracleConnection("Server=fzjclk_192.168.51.12;Password=fzjc;User ID=fzjc");
OracleDataAdapter adp = new OracleDataAdapter();
DataSet ds = new DataSet(); try
{
OracleCommand cmd = new OracleCommand("select * from test", conn);
adp.SelectCommand = cmd; OracleCommand deleteCmd = new OracleCommand("delete from test where col1=?", conn);
deleteCmd.Parameters.Add("col1", OracleType.Char, 10, "col1");
adp.DeleteCommand = deleteCmd; adp.Fill(ds, "test"); DataTable table = ds.Tables["test"];
DataRow row = table.Rows[0];
row.Delete(); adp.Update(ds, "test"); // 此行报错 table.AcceptChanges();
ds.AcceptChanges();
Console.WriteLine("deleted");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (conn != null) conn.Close();
}
}执行后输出:System.Data.OracleClient.OracleException: ORA-01036: 非法的变量名/编号 在 System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, B
atchCommandInfo[] batchCommands, Int32 commandCount)
在 System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCo
mmandInfo[] batchCommands, Int32 commandCount)
在 System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
在 System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tab
leMapping)
在 System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)请教大家,变量名那块有错呢?
但是使用“?”是我从MSDN上查到的用法,真是奇怪?????如下是MSDN上的原文:下面的示例创建派生类 OleDbDataAdapter 并设置它的一些属性。Visual Basic 复制代码
Public Shared Function CreateCustomerAdapter( _
connection As OleDbConnection) As OleDbDataAdapter Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter()
Dim command As OleDbCommand
Dim parameter As OleDbParameter ' Create the SelectCommand.
command = New OleDbCommand("SELECT CustomerID FROM Customers " & _
"WHERE Country = ? AND City = ?", connection) command.Parameters.Add("Country", OleDbType.VarChar, 15)
command.Parameters.Add("City", OleDbType.VarChar, 15) dataAdapter.SelectCommand = command ' Create the DeleteCommand.
command = New OleDbCommand( _
"DELETE * FROM Customers WHERE CustomerID = ?", _
connection) parameter = command.Parameters.Add( _
"CustomerID", OleDbType.Char, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original dataAdapter.DeleteCommand = command Return dataAdapter
End Function
C# 复制代码
public static OleDbDataAdapter CreateCustomerAdapter(
OleDbConnection connection)
{
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
OleDbCommand command;
OleDbParameter parameter; // Create the SelectCommand.
command = new OleDbCommand("SELECT CustomerID FROM Customers " +
"WHERE Country = ? AND City = ?", connection); command.Parameters.Add("Country", OleDbType.VarChar, 15);
command.Parameters.Add("City", OleDbType.VarChar, 15); dataAdapter.SelectCommand = command; // Create the DeleteCommand.
command = new OleDbCommand(
"DELETE * FROM Customers WHERE CustomerID = ?",
connection); parameter = command.Parameters.Add(
"CustomerID", OleDbType.Char, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original; dataAdapter.DeleteCommand = command; return dataAdapter;
}