如何获得最后插入的ident值&将其插入另一个表。 [英] how to get last inserted ident value & insert it into another table.

查看:100
本文介绍了如何获得最后插入的ident值&将其插入另一个表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是asp.net和sql server 2008的新手。我必须先写商店程序,然后我会向您解释数据库。



Hi i am new at asp.net and sql server 2008. i have to write store procedure before that i explain you about database.

Content(contentid ident,contenttext varchar(500))
sentence(sentenceid ident,sentencetext varchar(500))
contentsentence(contentid,sentenceid,order no all are int)





,内容和句子表都有pk和fk与contentsentence表的关系。



i有编写将内容分成句子并将数据输入句子表的代码。所以我必须编写存储过程,获取当前生成的句子id,可以从句子表中找到使用@@ identity并从中生成哪个句子,并输入orderno whic h是手动插入并存储在一个变量中。当此存储过程触发其当前生成的sentenceid的取值时,其相关内容id和手动输入orderno。这个feilds插入contentsentence表。



i不知道如何为此写商店程序所以请帮助我!!!





and both content and sentence table has relation of pk and fk with contentsentence table.

i have write code that split content into sentence and data enter into sentence table.so i have to write store procedure that get current generated sentence id which can find using @@identity from sentence table and get contentid from which sentence is generated and also enter orderno which is manual inserted and stored in one variable.so when this store procedure fires its take value of currently generated sentenceid its related content id and manual entered orderno. this feilds inserted in contentsentence table.

i don't know how to write store procedure for this so please help me!!!

protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e )
 { 
 if (e.CommandName == "click")
 {
  
 int index = Convert.ToInt32(e.CommandArgument.ToString());
  
 if(index<0)
 {
 return;
 }
  
 GridViewRow row = GridView1.Rows[index];
  
 var text = GridView1.Rows[index].Cells[0].Text;
  
 string tb = ((TextBox)GridView1.Rows[index].FindControl("txt1")).Text;
  
 String[] sentences = Regex.Split(text, @"(?<=[.!?])\s+(?=\p{Lt})");
  
 System.Data.DataTable dt1 = new System.Data.DataTable();
  
 dt1.Columns.Add("SentenceText1");
  
 foreach (string value in sentences)
 {
 if(!string.IsNullOrWhiteSpace(value))
 dt1.Rows.Add(value);
 }
 SqlConnection con = new SqlConnection(conn);
 con.Open();
 string sql = "";
 for (int i = 0; i < dt1.Rows.Count; i++)
 {
 sql = sql + "INSERT INTO Sentences(SentenceText) SELECT '" + dt1.Rows[i]["SentenceText1"].ToString() + "' FROM Sentences WHERE NOT EXISTS(SELECT '" + dt1.Rows[i]["SentenceText1"].ToString() + "' FROM Sentences WHERE SentenceText='" + dt1.Rows[i]["SentenceText1"].ToString() + "')";
 sql = sql + "";
 SqlCommand cmd = new SqlCommand(sql, con);
 cmd.ExecuteNonQuery();
 }





i的存储值为dt1.Rows [i] [SentenceText1]。ToString()在一个变量中name abc以及如何在存储过程中使用此变量名称abc作为输入参数来插入该变量的值。



i have store value of dt1.Rows[i]["SentenceText1"].ToString() in one variable name abc and how can use this variable name abc in store procedure as input parameter to insert that variable's value.

推荐答案

这对您有用:



http://support.microsoft.com/kb/310070 [< a href =http://support.microsoft.com/kb/310070target =_ blanktitle =新窗口> ^ ]



http://net-informations.com/csprj/data-providers/cs- procedure-parameter.htm [ ^ ]



http:/ /www.csharp-station.com/Tutorial/AdoDotNet/lesson07 [ ^ ]
This will useful for you:

http://support.microsoft.com/kb/310070[^]

http://net-informations.com/csprj/data-providers/cs-procedure-parameter.htm[^]

http://www.csharp-station.com/Tutorial/AdoDotNet/lesson07[^]


这篇关于如何获得最后插入的ident值&amp;将其插入另一个表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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