我有一个代码向数据库发送一些我在将代码运行到数据库时编写的信息,现在我需要检查我插入数据库的数据是否已存在,如果是,我必须更新它。我将保留下面的代码,以便您更方便地提供帮助。如果您需要更多代码,请随时向我提供。
try
{
conn.Open();
string sqlQuerry = @"INSERT INTO UXMenu(TransDocument, TransSerial, TransDocNumber, PartyName, PartyLocalityID, TotalAmount, ShipToPostalCode, Estado , IDZona ,Bonus,NumFac) VALUES (@transdocument, @transserial, @transdocnumber, @partyname, @partylocal, @totalamount, @shiptocode, @estado ,@IDZona ,@Bonus,@NumFac)";
SqlCommand SQLcm = new SqlCommand();
SQLcm.Connection = conn;
SQLcm.CommandText = sqlQuerry;
SQLcm.CommandType = CommandType.Text;
SQLcm.Parameters.AddWithValue("@transdocument", transdocument);
SQLcm.Parameters.AddWithValue("@transserial", transserial);
SQLcm.Parameters.AddWithValue("@transdocnumber", transaction.TransDocNumber);
SQLcm.Parameters.AddWithValue("@partyname", nomecli);
SQLcm.Parameters.AddWithValue("@partylocal", locality);
SQLcm.Parameters.AddWithValue("@totalamount", transaction.TotalAmount);
SQLcm.Parameters.AddWithValue("@shiptocode", shipto);
SQLcm.Parameters.AddWithValue("@estado", estado);
SQLcm.Parameters.AddWithValue("@IDZona", "0");
SQLcm.Parameters.AddWithValue("@Bonus", "0");
SQLcm.Parameters.AddWithValue("@NumFac", "0");
SQLcm.ExecuteNonQuery();
MessageBox.Show("inseriu dados");
conn.Close();
}
catch (Exception ex)
{
throw ex;
}
}
更新1:
这是我创建表格的方式:
using (SqlConnection conn = new SqlConnection(ConecçãoDB))
{
conn.Open();
SqlCommand command = new SqlCommand("IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(TransDocument varchar(5), TransSerial varchar(5), TransDocNumber float PRIMARY KEY, PartyName varchar(60), PartyLocalityID varchar(5), TotalAmount varchar(25), ShipToPostalCode varchar(35), Estado int, IDZona varchar(15),Bonus varchar(15),NumFac varchar(15));", conn);
command.ExecuteNonQuery();
MessageBox.Show("tabela criada menu");
conn.Close();
答案 0 :(得分:0)
您尚未发布数据库架构&示例数据,所以我不能给你一个执行查询。
但是,您可以通过MERGE
语句在SQL Server中实现您想要做的事。
这是在SQL Server 2008中添加的,以容纳UPSERT
声明,这些声明也非常常见,但没有语言支持&必须分两步完成。
MERGE
可让您根据与源表的联接结果在目标表中获得UPDATE
,INSERT
或DELETE
。
可以在https://docs.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql
找到该文档以下是您的表架构的基本示例。
运行两次。首次运行将执行INSERT
,第二次运行将执行UPDATE
IF OBJECT_ID('UXMenu', 'U') IS NULL CREATE TABLE UXMenu(TransDocument varchar(5), TransSerial varchar(5), TransDocNumber float PRIMARY KEY, PartyName varchar(60), PartyLocalityID varchar(5), TotalAmount varchar(25), ShipToPostalCode varchar(35), Estado int, IDZona varchar(15),Bonus varchar(15),NumFac varchar(15));
DECLARE @TargetTransDocNumber float=1;
SELECT * FROM UXMenu;
MERGE UXMenu AS target
USING
(
SELECT @TargetTransDocNumber as TransDocNumber
) AS source
ON (target.TransDocNumber = source.TransDocNumber)
WHEN NOT MATCHED BY TARGET THEN
INSERT(TransDocument, TransSerial, TransDocNumber, PartyName, PartyLocalityID, TotalAmount, ShipToPostalCode, Estado, IDZona, Bonus, NumFac)
VALUES('doc1', 'ser1', @TargetTransDocNumber, 'par1', 'ploc1', 'totamt1', 'postal1', 1, 'zon1', 'bon1', 'fac1')
WHEN MATCHED THEN
UPDATE SET
target.TransDocument = 'doc2',
target.TransSerial='ser2',
target.PartyName='par2',
target.PartyLocalityID='ploc2',
target.TotalAmount='totamt2',
target.ShipToPostalCode='postal2',
target.Estado=2,
target.IDZona='zon2',
target.Bonus='bon2',
target.NumFac='fac2';
SELECT * FROM UXMenu;