"insert into" 来自 c# 的 accdb
"insert into" accdb from c#
我在 c# 中编写了这两段代码以在 access accdb 的一行中写一些东西,但它们都不起作用:
String tableName = "ARE";
String query = String.Format(
"select * from [{0}]", tableName);
DataSet ds = new DataSet();
using (OleDbConnection conn =
new OleDbConnection(connection_string))
{
try
{
//Open Database Connection
conn.Open();
OleDbDataAdapter da =
new OleDbDataAdapter(query, conn);
OleDbCommandBuilder cmdB =
new OleDbCommandBuilder(da);
da.MissingSchemaAction =
MissingSchemaAction.AddWithKey;
//Fill the DataSet
da.Fill(ds, tableName);
DataRow row = ds.Tables["ARE"].NewRow();
row["ID"] = "1";
row["Localizzazione"] = "1";
row["UO"] = "UO1";
row["Coordinate"] = "333;336";
ds.Tables["ARE"].Rows.Add(row);
da.Update(ds, "ARE");
}
catch (OleDbException exp)
{
MessageBox.Show("Database Error:" + exp.Message.ToString());
}
和
using (OleDbConnection oc = new OleDbConnection(connection_string))
{
string q = "INSERT INTO ARE "
+ "(ID, Localizzazione, UO, Coordinate) "
+ "VALUES (@ID, @Localizzazione, @UO, @Coordinate)";
OleDbCommand insertCommand = new OleDbCommand(q, oc);
insertCommand.Parameters.Add("@ID", OleDbType.Integer).Value = 1;
insertCommand.Parameters.Add("@Localizzazione", OleDbType.Integer).Value = 1;
insertCommand.Parameters.Add("@UO", OleDbType.Char).Value = (UO[selectedUO])[1].ToString();
insertCommand.Parameters.Add("@Coordinate", OleDbType.Char).Value = (e.X - 12) + ";" + (e.Y - 12);
oc.Open();
insertCommand.ExecuteNonQuery();
}
但总是告诉我"error sintax on isctruction INSERT INTO."
System.Data.OleDb.OleDbException
为什么??? :(
为什么先声明字符串 q 然后再声明字符串 q?看起来你的 sql 写得不正确.. 空格,大小写等
根据Microsoft,'ARE' 是Jet DB 引擎的保留关键字。你现在可以使用方括号来解决这个问题(比如 INSERT INTO [ARE]
,但你真的应该考虑更改 table 名称。
我在 c# 中编写了这两段代码以在 access accdb 的一行中写一些东西,但它们都不起作用:
String tableName = "ARE";
String query = String.Format(
"select * from [{0}]", tableName);
DataSet ds = new DataSet();
using (OleDbConnection conn =
new OleDbConnection(connection_string))
{
try
{
//Open Database Connection
conn.Open();
OleDbDataAdapter da =
new OleDbDataAdapter(query, conn);
OleDbCommandBuilder cmdB =
new OleDbCommandBuilder(da);
da.MissingSchemaAction =
MissingSchemaAction.AddWithKey;
//Fill the DataSet
da.Fill(ds, tableName);
DataRow row = ds.Tables["ARE"].NewRow();
row["ID"] = "1";
row["Localizzazione"] = "1";
row["UO"] = "UO1";
row["Coordinate"] = "333;336";
ds.Tables["ARE"].Rows.Add(row);
da.Update(ds, "ARE");
}
catch (OleDbException exp)
{
MessageBox.Show("Database Error:" + exp.Message.ToString());
}
和
using (OleDbConnection oc = new OleDbConnection(connection_string))
{
string q = "INSERT INTO ARE "
+ "(ID, Localizzazione, UO, Coordinate) "
+ "VALUES (@ID, @Localizzazione, @UO, @Coordinate)";
OleDbCommand insertCommand = new OleDbCommand(q, oc);
insertCommand.Parameters.Add("@ID", OleDbType.Integer).Value = 1;
insertCommand.Parameters.Add("@Localizzazione", OleDbType.Integer).Value = 1;
insertCommand.Parameters.Add("@UO", OleDbType.Char).Value = (UO[selectedUO])[1].ToString();
insertCommand.Parameters.Add("@Coordinate", OleDbType.Char).Value = (e.X - 12) + ";" + (e.Y - 12);
oc.Open();
insertCommand.ExecuteNonQuery();
}
但总是告诉我"error sintax on isctruction INSERT INTO." System.Data.OleDb.OleDbException
为什么??? :(
为什么先声明字符串 q 然后再声明字符串 q?看起来你的 sql 写得不正确.. 空格,大小写等
'ARE' 是Jet DB 引擎的保留关键字。你现在可以使用方括号来解决这个问题(比如 INSERT INTO [ARE]
,但你真的应该考虑更改 table 名称。