插入两个带有检查条件的表

编程入门 行业动态 更新时间:2024-10-26 20:30:25
本文介绍了插入两个带有检查条件的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨 我有一个项目,需要帮助.有人知道我该怎么办? 我有一个页面可以为任何患者提供唯一的专利服务,每个患者都有某种服务和一些paper.i在sql中有2张表纸和givservice.我想在Giveservice表上输入具有相同Patientid的服务我必须这样做吗? 这是我的代码:

私有 void insert_Click(对象发​​件人,EventArgs e) { SqlConnection con = 新 SqlConnection(); con.ConnectionString = " server = .; database = database; integrated security = true;" ; SqlCommand com1 = 新 SqlCommand(); SqlCommand com2 = 新 SqlCommand(); SqlCommand com3 = 新 SqlCommand(); SqlCommand com4 = 新 SqlCommand(); SqlCommand com5 = 新 SqlCommand(); com1.CommandText = " DECLARE @papercode smallint" ; com2.CommandText = " 插入纸中(日期,drcode,病人ID,单位ID,人格)值(@ date,@ drcode ,@ patientid,@ unitid,'15')"; com5.CommandText = " SET @ papercode = SCOPE_IDENTITY()" ; com3.CommandText = " 从患者,纸张中选择@ papercode = papercode,其中Patient.patientid = Paper.patientid" ; com4.CommandText = " 插入givservice(papercode,price,tedad,khid)值(@ papercode,@ price,@ tedad,@ khid)"; com1.Connection = con; com2.Connection = con; com3.Connection = con; com4.Connection = con; com1.Parameters.AddWithValue(" @ date" ,txtdate.Text); com1.Parameters.AddWithValue(" @ Patientid" ,txtpaziresh.Text); com1.Parameters.AddWithValue(" @ unitid" ,cmdunitname.SelectedValue); com1.Parameters.AddWithValue(" @ drcode" ,cmddrname.SelectedValue); com4.Parameters.AddWithValue(" @ tedad" ,txttedad.Text); com4.Parameters.AddWithValue(" @ price" ,cmdprice.SelectedValue); com4.Parameters.AddWithValue(" @ khid" ,cmdkh.SelectedValue); SqlParameter idOut = com5.Parameters.Add(" @ papercode" ,SqlDbType.SmallInt); idOut.Direction = ParameterDirection.Output; con.Open(); com1.ExecuteNonQuery(); SqlDataReader dr = com3.ExecuteReader(); con.Close(); }

解决方案

正在使用什么版本的SQL yoiu.如果其2005或更高版本,则可以使用带有输出子句的insert语句. 以下链接为您提供了有关该链接的详细信息. technet.microsoft/en-us/library/ms177564.aspx [ ^ ] 希望对您有所帮助.

hi i have a project and need help.anybody knows what must i do? i have a page that give a patentid to any patient for giving service that is unique.every patient has some service and some paper.i have 2 table paper and givservice in sql.i want enter services that have the same patientid on giveservice table how must i do that? here is my code:

private void insert_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); con.ConnectionString = "server=.;database=database;integrated security=true;"; SqlCommand com1 = new SqlCommand(); SqlCommand com2 = new SqlCommand(); SqlCommand com3 = new SqlCommand(); SqlCommand com4 = new SqlCommand(); SqlCommand com5=new SqlCommand(); com1.CommandText="DECLARE @papercode smallint"; com2.CommandText = " insert into paper(date,drcode,patientid,unitid,personid)values(@date,@drcode,@patientid,@unitid,'15')"; com5.CommandText = "SET @papercode=SCOPE_IDENTITY()"; com3.CommandText = " Select @papercode=papercode From patient,Paper Where patient.patientid=Paper.patientid"; com4.CommandText = "insert into givservice (papercode,price,tedad,khid) values ( @papercode,@price,@tedad,@khid)"; com1.Connection = con; com2.Connection = con; com3.Connection = con; com4.Connection = con; com1.Parameters.AddWithValue("@date", txtdate.Text); com1.Parameters.AddWithValue("@patientid",txtpaziresh.Text); com1.Parameters.AddWithValue("@unitid",cmdunitname.SelectedValue); com1.Parameters.AddWithValue("@drcode", cmddrname.SelectedValue); com4.Parameters.AddWithValue("@tedad", txttedad.Text); com4.Parameters.AddWithValue("@price", cmdprice.SelectedValue); com4.Parameters.AddWithValue("@khid", cmdkh.SelectedValue); SqlParameter idOut = com5.Parameters.Add("@papercode", SqlDbType.SmallInt); idOut.Direction = ParameterDirection.Output; con.Open(); com1.ExecuteNonQuery(); SqlDataReader dr= com3.ExecuteReader(); con.Close(); }

解决方案

What version of SQL yoiu are using. If its 2005 or above then you can use insert statement with output clause. Following link gives you detail about the same. technet.microsoft/en-us/library/ms177564.aspx[^] Hope it helps.

更多推荐

插入两个带有检查条件的表

本文发布于:2023-10-30 12:10:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   两个

发布评论

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

>www.elefans.com

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