如果表中已存在数据(Ado.net)

编程入门 行业动态 更新时间:2024-10-26 10:29:19
本文介绍了如果表中已存在数据(Ado)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是使用Ado存储过程在表格中插入行的代码。

Here is the code for inserting rows in a table using Ado stored procedure.

<pre lang="c#"> IDbConnection conn = null; try { conn = this.GetConnection(); conn.Open(); IDbCommand cmd = conn.CreateCommand(); //string insertSQL = @"insert into Routes(FromCityId,ToCityId,DistanceInKms,Status) values(@fromcityID,@toCityID,@dis,@st);"; //cmd.CommandText = insertSQL; //cmd.CommandType = CommandType.Text; cmd.CommandText = "StoredProcedureName"; cmd.CommandType = CommandType.StoredProcedure; IDataParameter p1 = cmd.CreateParameter(); p1.ParameterName = "@FromCity"; p1.Value = RouteInfo.FromCity.CityId; cmd.Parameters.Add(p1); //........................ return cmd.ExecuteNonQuery(); } } catch (Common.ConnectToDatabaseException) { throw new RouteDAOException("Unable to add route"); } catch (Exception) { throw new RouteDAOException("Unable to add route"); } finally { conn.Close(); }

我需要提示写一个代码来检查行是否是已存在,如果是,我将不得不创建一个例外。 最好的问候,

I need a hint to write a code for checking if the row is already exists , if yes , I will have to create an exception. best regards,

推荐答案

在存储过程中使用以下IF语法 use following IF syntax in the stored procedure IF NOT EXISTS(SELECT * FROM TABLE NAME WHERE <duplicate condition="">) BEING --INSERT INTO TABLENAME (columnName1,..) VALUES (...) END </duplicate>

您需要在Sql中检查它。 在SQL中你做 You will want to check for it in Sql. In SQL you do IF EXISTS(SELECT * FROM table WHERE field = value) BEGIN -- do an update since it exists UPDATE table SET field1 = value. field2 = value2 WHERE field = value END ELSE BEGIN -- Since it does not exist, do an insert INSERT INTO ... END

更多推荐

如果表中已存在数据(Ado.net)

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

发布评论

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

>www.elefans.com

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