【发布时间】:2014-11-08 19:59:35
【问题描述】:
我正在尝试使用 sql 查询添加到表访问和数据网格行。购买没有成功。有任何想法吗 ?谢谢 我的 sql 查询:
DataBaseIkuns.Instance.InsertToDB(string.Format(DictionaryUtilsDB.dictioneary[DictionaryUtilsDB.CommendTypes.AddObserver], o.ID_Observer, o.Lat, o.Long, o.azimuth));
public static Dictionary<CommendTypes, string> dictioneary = new Dictionary<CommendTypes, string>
{
{CommendTypes.AddObserver,"Insert into ShowTableObserver(ID_Ob,Lat,Long,Azimuth)"
+"values('{0}','{1}','{2}','{3}')"},
{CommendTypes.AzimuthLongLatFromOB,"SELECT ID_Observer,Longitude,Latitude,Azimuth FROM Observer Where ID_Observer = {0}"}
};
public void InsertToDB(string sql) // It get the right values - 1,2,3,4
{
int insert = 0;
try
{
if (con.State.ToString()== "Open")
{
cmd = new OleDbCommand();
oledbAdapter = new OleDbDataAdapter();
dt = new DataTable();
cmd.Connection = con;
cmd.CommandText = sql;
insert = cmd.ExecuteNonQuery(); // Here it jump's to the catch. why ?
if (insert > 0)
{
MessageBox.Show("Your Insert successed");
}
else
{
MessageBox.Show("Your Insert failed");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
在程序所在的代码中添加注释。 sql下坠时的值:
Insert into ShowTableObserver(ID_Ob,Lat,Long,Azimuth)values('3','31.4','34','150')
【问题讨论】:
-
在引发异常时,您能告诉我们
sql的值吗? -
是的,我编辑问题看看
-
最重要的是知道在 catch 块中显示的确切错误消息。
-
您是否使用 Access 数据库来存储您的数据?
-
是的,我使用 access 作为我的数据库,因为我应该这样做。错误是查询中的语法错误。
标签: c# mysql sql-server wpf datagrid