我在WinForms项目中使用C#和Oracle。我收到错误:
使用" InsertCommand.Parameters.Add"时,ORA-01400无法将null插入(字符串)。
问题是参数(":ID", ":NOME", ":OBSERVACAO", ":NUMERO", ":ENDERECO")
没有从" srcColumn"中接收到值。 ("ID", "NOME", "OBSERVACAO", "NUMERO", "ENDERECO")
方法的参数" Add"。我做错了什么?
public Form1()
{
InitializeComponent();
ConsultaGRID();
}
OracleConnection conn = new OracleConnection("Data Source=xxx; User ID=xxx; Password=xxx;");
DataTable tabela = new DataTable();
OracleDataAdapter da = new OracleDataAdapter();
private void ConsultaGRID()
{
while (conn.State != ConnectionState.Open) { conn.Open(); }
OracleCommand cmd = new OracleCommand("select ID, NOME, OBSERVACAO, NUMERO, ENDERECO from CADASTRO_FONES order by ID", conn);
da = new OracleDataAdapter(cmd);
da.Fill(tabela);
dataGridView1.DataSource = tabela;
}
private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.F6)//Inserir
{
GetInsert();
}
else if (e.KeyCode == Keys.F10)//Salvar
{
GetSave();
}
}
private void GetInsert()
{
tabela.Rows.Add();
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0];
}
private void GetSave()
{
try
{
while (conn.State != ConnectionState.Open) { conn.Open(); }
da.InsertCommand = new OracleCommand("insert into CADASTRO_FONES (NOME, ID, OBSERVACAO, NUMERO, ENDERECO) values (:NOME ,:ID ,:OBSERVACAO ,:NUMERO ,:ENDERECO)", conn);
da.InsertCommand.Parameters.Add(":ID", OracleType.Number, 22, "ID");
da.InsertCommand.Parameters.Add(":NOME", OracleType.VarChar, 70, "NOME");
da.InsertCommand.Parameters.Add(":OBSERVACAO", OracleType.VarChar, 70, "OBSERVACAO");
da.InsertCommand.Parameters.Add(":NUMERO", OracleType.VarChar, 20, "NUMERO");
da.InsertCommand.Parameters.Add(":ENDERECO", OracleType.VarChar, 70, "ENDERECO");
da.Update(tabela);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
finally
{
conn.Close();
}
}
我改变了#34; GetSave()"函数代码如下,仍然不起作用:
private void GetSave()
{
try
{
while (conn.State != ConnectionState.Open) { conn.Open(); }
OracleCommand cmd = new OracleCommand("insert into CADASTRO_FONES (NOME, ID, OBSERVACAO, NUMERO, ENDERECO) values (:NOME ,:ID ,:OBSERVACAO ,:NUMERO ,:ENDERECO)", conn);
da.InsertCommand.Parameters.Add("ID", OracleType.Number, 22, "ID");
da.InsertCommand.Parameters.Add("NOME", OracleType.VarChar, 70, "NOME");
da.InsertCommand.Parameters.Add("OBSERVACAO", OracleType.VarChar, 70, "OBSERVACAO");
da.InsertCommand.Parameters.Add("NUMERO", OracleType.VarChar, 20, "NUMERO");
da.InsertCommand.Parameters.Add("ENDERECO", OracleType.VarChar, 70, "ENDERECO");
da.InsertCommand = cmd;
da.Update(tabela);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
finally
{
conn.Close();
}
}
答案 0 :(得分:0)
您需要摆脱Parameters.Add:
中的冒号 da.InsertCommand.Parameters.Add(":ID", OracleType.Number, 22, "ID");
应该是
da.InsertCommand.Parameters.Add("ID", OracleType.Number, 22, "ID");
冒号在实际的SQL字符串中用于表示绑定参数。创建参数时,只需使用不带冒号的名称。
参见示例here