ilovejinglei 原文 C#中保存datagridview中的数据时报错“动态SQL生成失败。找不到关键信息”
问题描述
相关代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types; namespace 订单系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataSet ds;
private OracleDataAdapter oda;
private OracleConnection con;
private OracleCommand cmd; private void Form1_Load(object sender, EventArgs e)
{
try
{
string conString = "User Id=dingzi; password=dingzi;" + "Data Source=localhost:1521/orcl; Pooling=false;";
con = new OracleConnection();
con.ConnectionString = conString;
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = "select * from stock order by 订货日期 desc";
OracleDataReader reader = cmd.ExecuteReader();
ds = new DataSet();
oda = new OracleDataAdapter(cmd.CommandText, con);
oda.Fill(ds);
dataGridView1.DataSource = ds.Tables[];
dataGridView1.Columns["订货日期"].DefaultCellStyle.Format = "yyyy/MM/dd HH:mm:ss";//此句要放在ataGridView1.DataSource之后;
con.Close();
}
catch (Exception E)
{ MessageBox.Show(E.Message); }
} private void button1_Click(object sender, EventArgs e)
{
OracleCommandBuilder cx = new OracleCommandBuilder(oda);
oda.Update(ds); con.Open();
OracleCommand cmd = new OracleCommand("update stock set 订货日期=sysdate where 订货日期 is null", con);
cmd.ExecuteNonQuery();
MessageBox.Show("保存成功", "提示"); //更新数据后刷新数据
cmd = con.CreateCommand();
cmd.CommandText = "select * from stock order by 订货日期 desc";
OracleDataReader reader = cmd.ExecuteReader();
ds = new DataSet();
oda = new OracleDataAdapter(cmd.CommandText, con);
oda.Fill(ds);
dataGridView1.DataSource = ds.Tables[];
con.Close();
}
}
}
解决问题的提示:
这个错误是因为没有主键。