在我的数据库表中,我想创建不存在的新条目,如果它们存在则更新它们。我有一个表将UserID LernfeldID和Schoolyear链接到Marks。当UserID不匹配时,应该只插入数据库,这意味着该条目不存在。如果有条目,则应该更新。
我使用Datatable
对象,因为它似乎适合我所阅读的内容。我想查看DBNull.Value
,但它不起作用。我的数据库只接收SELECT
语句。如果我使用for循环更新DataTable
并将其发送回数据库,则可以正常工作。但是我仍然需要做"条目存在"校验。你可以帮帮我吗?
我的代码如下所示:
public static void DB_GetMarks()
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
try
{
SqlCommand cmdQueryMarks = new SqlCommand();
cmdQueryMarks.Connection = con;
cmdQueryMarks.CommandText = "SELECT UserID, LernfeldID, SchuljahrID, Note "
+ "FROM UsersToLernfelder "
+ "WHERE USERID = @usrID "
+ "AND SchuljahrID = @YearID "
+ "ORDER BY LernfeldID ASC";
cmdQueryMarks.Parameters.AddWithValue("@usrID", _UserID);
cmdQueryMarks.Parameters.AddWithValue("@YearID", _YearID);
SqlDataAdapter da = new SqlDataAdapter(cmdQueryMarks);
DataTableMapping dtm = da.TableMappings.Add("Table", "UsersToLernfelder");
dtm.ColumnMappings.Add("UserID", "User");
dtm.ColumnMappings.Add("LernfeldID", "Lernfeld");
dtm.ColumnMappings.Add("SchuljahrID", "Schuljahr");
DataTable tbl = new DataTable();
da.Fill(tbl);
int i = 0;
foreach (DataRow row in tbl.Rows)
{
object value = row["UserID"];
if (value == DBNull.Value)
{
DataRow newEntry = tbl.NewRow();
newEntry["UserID"] = UserID;
newEntry["Lernfeld"] = i + 1;
newEntry["Schuljahr"] = YearID;
newEntry["Note"] = MarksArr[i];
tbl.Rows.Add(newEntry);
}
else
{
row["marks"] = MarksArr[i];
}
i++;
}
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
da.Update(tbl);
}
catch (Exception ex)
{
_Err = ex.Message;
}
}
}
PS。这是我的第一篇文章,请原谅任何可能的错误
答案 0 :(得分:0)
你看起来Merge命令。 this answer也会帮助你。
答案 1 :(得分:0)
您无法获得Null Rows,因为您已使用@UserID
过滤了记录
如果您想使用不存在的用户更新UsersToLernfelder
,那么您应该有一个Users
和Marks
的列表。
public static void DB_GetMarks(int _UserID, int _YearID)
{
Dictionary<int, double> MarksArr = new Dictionary<int, double>();
MarksArr.Add(1, 50);
MarksArr.Add(2, 49);
MarksArr.Add(3, 48);
using (SqlConnection con = new SqlConnection(""))
{
try
{
SqlCommand cmdQueryMarks = new SqlCommand();
cmdQueryMarks.Connection = con;
cmdQueryMarks.CommandText = "SELECT UserID, LernfeldID, SchuljahrID, Note "
+ "FROM UsersToLernfelder "
+ "WHERE USERID = @usrID "
+ "AND SchuljahrID = @YearID "
+ "ORDER BY LernfeldID ASC";
cmdQueryMarks.Parameters.AddWithValue("@usrID", _UserID);
cmdQueryMarks.Parameters.AddWithValue("@YearID", _YearID);
SqlDataAdapter da = new SqlDataAdapter(cmdQueryMarks);
DataTableMapping dtm = da.TableMappings.Add("Table", "UsersToLernfelder");
dtm.ColumnMappings.Add("UserID", "User");
dtm.ColumnMappings.Add("LernfeldID", "Lernfeld");
dtm.ColumnMappings.Add("SchuljahrID", "Schuljahr");
DataTable tbl = new DataTable();
da.Fill(tbl);
int i = 0;
foreach (var item in MarksArr)
{
DataRow Result = (from DataRow dr in tbl.Rows where Convert.ToInt32(dr["USERID"]) == item.Key select dr).FirstOrDefault();
if (Result == null)
{
DataRow newEntry = tbl.NewRow();
newEntry["UserID"] = item.Key;
newEntry["Lernfeld"] = i + 1;
newEntry["Schuljahr"] = _YearID;
newEntry["Note"] = item.Value;
tbl.Rows.Add(newEntry);
}
else
{
foreach (DataRow _dr in tbl.Rows)
{
if (Convert.ToInt32(_dr["UserID"]) == item.Key)
{
_dr["marks"] = item.Value;
}
}
}
i++;
}
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
da.Update(tbl);
}
catch (Exception ex)
{
_Err = ex.Message;
}
}
}