【发布时间】:2010-11-11 22:26:47
【问题描述】:
您好,现在是第一次使用 SQL!
我有下面的代码,它工作正常,但我需要检查条目是否已经在使用 CustomerName 和 Product 匹配的数据库中,如果它在数据库中,请更新其他字段,如果没有插入所有数据。
我该怎么做?
下面是我用来插入新记录的代码:
DateTime FirstDateSeen = new DateTime();
FirstDateSeen = DateTime.Now.Date;
DateTime LastDateSeen = new DateTime();
LastDateSeen = DateTime.Now.Date;
SqlConnectionStringBuilder MySqlConnection = new SqlConnectionStringBuilder("MY CONNECTION");
SqlConnection db = new SqlConnection(MySqlConnection.ToString());
try //sql string for first seen
{
string sqlIns = "INSERT INTO Customer (Product, Version, CustomerName, CustomerPostcode, FirstSeen, LastSeen)" +
"VALUES (@Product, @Version, @CustomerName, @CustomerPostcode, @FirstSeen, @LastSeen)";
db.Open();
SqlCommand cmdIns = new SqlCommand(sqlIns, db);
cmdIns.Parameters.Add("@CustomerName", UniqueA);
cmdIns.Parameters.Add("@Product", AppName);
cmdIns.Parameters.Add("@Version", AppVer);
cmdIns.Parameters.Add("@CustomerPostcode", UniqueB);
cmdIns.Parameters.Add("@FirstSeen", FirstDateSeen.ToShortDateString());
cmdIns.Parameters.Add("@LastSeen", LastDateSeen.ToShortDateString());
cmdIns.ExecuteNonQuery();
cmdIns.Parameters.Clear();
cmdIns.Dispose();
cmdIns = null;
}
catch (Exception ex)
{
throw new Exception(ex.ToString(), ex);
}
finally
{
db.Close();
}
【问题讨论】:
标签: c# sql sql-server-2005 .net-3.5