大家好, 我在数据库编程实践中遇到了一些问题. 以下是我的目的: 1)我有会员编号和金额. 2)首先在C#中我运行了一个查询"SELECT * FROM Table1 WHERE ......" 3)然后,如果找到记录,我想更新记录 4)如果找不到记录,那么我想在表中插入记录. 请参考以下代码:
Hi all , I am stuck in some issue while my practice in database programming. Below is my purpose : 1) I have member id and amount with me. 2) First throught C# i ran one query "SELECT * FROM Table1 WHERE ......" 3) Then if record found i want to update the record 4) If record not found then i want to insert the record in the table. Please refer below code :
public static bool SaveMembersCollection(Int16 Id, double amt, DateTime dtofcollection) { OleDbCommand Com = new OleDbCommand(); string sql; sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID='", Id.ToString(), " AND CollectionDate='", dtofcollection.ToString(), "'"); Com.Connection = Conn; //OleDbParameter Param = new OleDbParameter("@titel", ""); //Com.Parameters.Add(Param); //Param = new OleDbParameter("@locatie", c); //Com.Parameters.Add(Param); Com.ExecuteNonQuery(); Conn.Close(); return false; }推荐答案
好吧,马上,我注意到您没有将sql或连接对象传递给cmd对象. > Well, right off, I noticed that you''re not passing your sql or the connection object to the cmd object. Com.ExecuteNonQuery(sql, Conn);
您在SELECT语句中AND之前缺少单引号'': Your missing a single quote mark '' just before the AND in your SELECT statement: sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID=''", Id.ToString(), " AND ...
应该是:
should be:
sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID=''", Id.ToString(), "'' AND ...
发布此内容时,我没有看到先前发布的答案....:doh:[\ Edit] 我立刻注意到的第一件事就是你在哪里写 I didn''t see the previously posted answer when I posted this.... :doh: [\Edit] The first thing I noticed right off is that where you write sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID=''", Id.ToString(), " AND CollectionDate=''", dtofcollection.ToString(), "''");
您忘记在Id.ToString()之后加上右引号.
you forgot to put in the closing single quote after Id.ToString().
sql=String.Concat("SELECT * FROM MemberCollections WHERE MemberID=''", Id.ToString(), "'' AND ...有关插入部分,请参见John Simmons的答案.
For the insert part see John Simmons answer.
更多推荐
[已解决] C#中的数据库编程
发布评论