比较数据库中的值 [英] Comparing values in database

查看:59
本文介绍了比较数据库中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制定时间表,作为我的项目.我想比较两个字段的时隙和类.如果它们已经在数据库中可用,则打印适当的消息,否则它将存储在数据库中.请帮助...

解决方案



从代码中,您可以将2个变量作为参数传递给SP并获取DB中的行数.如果记录存在,则返回1,否则从SP中插入return 0.

或使用合并语句.

 MERGE <   target_table   >  [AS TARGET]
使用<   table_source  >  [AS资源]
开启<   search_condition  > 
[匹配时
然后<   merge_matched  > ]
[未按目标匹配时]
然后<   merge_not_matched  > ]
[未按来源匹配时
然后<   merge _    匹配 > ]; 



请参考下面的链接,例如

SQL Server 2008中的合并语句 [ cmd = SqlCommand(" + CLASS_ID.Text + " + TIME.Text + " ,con); dr = cmd.ExecuteReader(); 如果(!dr.HasRows) { 字符串 str = " + SUBJECT_CODE.Text + " " + CLASS_ID.Text + " ','" + DropDownList1.SelectedItem + ',' " + TIME.Text + ',' + FACULTY_ID.Text + " + DropDownList2.SelectedItem + " ',' + DURATION.Text + " ; cmd2 = SqlCommand(str,con); cmd2.ExecuteNonQuery(); }


i m devloping timetable as my project. i wanted to compare two field timeslots and class. if they are already available in database then print approprite message otherwise it will store in database. pls help...

Hi,

From the code, you can pass the 2 variables as parameter to SP and get the count of rows in the DB.If the records exists return 1 else insert return 0 from the SP.

OR use a merge statement.

MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_ matched> ];



Refer the below link for example

Merge Statement in SQL Server 2008[^]

Hope this helps.


this may not be 100% currect but it will give some idea

cmd = new SqlCommand("select * from timetable where clo1='"+CLASS_ID.Text+"' and col2='"+TIME.Text+"'", con);
 dr = cmd.ExecuteReader();
if(!dr.HasRows)
{
	String str = "insert into TIMETABLE values ('" + SUBJECT_CODE.Text + "','" + OFFER_ID.Text + "','" + CLASS_ID.Text + "','" + DropDownList1.SelectedItem + "','" + TIME.Text + "','" + FACULTY_ID.Text + "','" + DropDownList2.SelectedItem + "','" + DURATION.Text + "') ";
            cmd2 = new SqlCommand(str, con);
            cmd2.ExecuteNonQuery();
}


这篇关于比较数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆