[已解决] C#中的数据库编程

编程入门 行业动态 更新时间:2024-10-21 11:40:10
本文介绍了[已解决] C#中的数据库编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我在数据库编程实践中遇到了一些问题. 以下是我的目的: 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#中的数据库编程

本文发布于:2023-10-30 04:26:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1541760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!