C# SQL Server和C中插入函数的问题

C# SQL Server和C中插入函数的问题,c#,sql-server,C#,Sql Server,C: 我所做的: 第一步 当我输入无效id时,这意味着id不包含在学生表和搜索中,它显示不存在 步骤2 当我输入有效id时,表示id包含在student中,也包含在studentresult表和搜索中,它会显示学生标记,如果我想编辑标记并更新,则会显示已更新的标记 步骤3 但当我输入id时,它意味着id包含在student中,但不包含在studentresult表和search中,它再次使用updated函数,所有文本框都包含0,而不是插入 请问,我在上面的代码中犯了什么错误 有人能指引我吗 我

C:

我所做的:

第一步

当我输入无效id时,这意味着id不包含在学生表和搜索中,它显示不存在

步骤2

当我输入有效id时,表示id包含在student中,也包含在studentresult表和搜索中,它会显示学生标记,如果我想编辑标记并更新,则会显示已更新的标记

步骤3

但当我输入id时,它意味着id包含在student中,但不包含在studentresult表和search中,它再次使用updated函数,所有文本框都包含0,而不是插入

请问,我在上面的代码中犯了什么错误

有人能指引我吗

我挣扎了一个小时,我是.net的初学者


谢谢,

sp\u studentresult已断开:如果给定id已有行,则不应将其插入studentresult。只需添加

protected void btnsearch_Click(object sender, EventArgs e)
{
    SqlConnection con = Connection.DBconnection();   {

    SqlCommand com = new SqlCommand("sp_studentresult", con);
    com.CommandType = CommandType.StoredProcedure;  
    com.Parameters.AddWithValue("@id", textstudentid.Text);
    com.Parameters.AddWithValue("@id_student", textstudentid.Text.Trim());
    SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50);

    com.Parameters.AddWithValue("@tamil", txttamil.Text.Trim());
    com.Parameters.AddWithValue("@english", txtenglish.Text.Trim());
    com.Parameters.AddWithValue("@maths", txtmaths.Text.Trim());
    com.Parameters.AddWithValue("@science", txtscience.Text.Trim());
    com.Parameters.AddWithValue("@socialScience", txtsocialscience.Text.Trim()); 

    retval.Direction = ParameterDirection.Output;
    com.Parameters.Add(retval);

    com.ExecuteNonQuery();

    string Output = retval.Value.ToString();

    textstudentid.Text = string.Empty;
    txttamil.Text = string.Empty;
    txtenglish.Text = string.Empty;
    txtmaths.Text = string.Empty;
    txtscience.Text = string.Empty;
    txtsocialscience.Text = string.Empty;   

    SqlDataAdapter adp = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    adp.Fill(ds);

    if (ds.Tables[0].Rows.Count > 0)
    {
        tblid.Visible = true;
        txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
        txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
        txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
        txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
        txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
    }
    else
    {
        tblid.Visible = false;
        output.Text = Output;
    }
}            

导致:

ELSE IF EXISTS (SELECT * FROM student WHERE id=@id_student)

很明显,它将进入插入块。如果您提供的是学生表中存在的有效ID,并且您的代码正在调用sp_studentresult,那么根据代码分支,执行将进入第二个区域,即插入块。我无法理解…请详细说明一下?谢谢您在哪里调用sp_searchupdate?当您单击搜索时,将调用您的sp_studentresult,如果存在,请选择*FROM student,其中id=@id_student。这意味着,如果您在此处提供有效ID,它将直接插入该ID。您到底想实现什么?旁注:您不应该在存储过程中使用sp_uu前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用sp_u,并使用其他东西作为前缀——或者根本不使用前缀@拉妮:我加了一个结果,改变程序对我不起作用。。这是我的输出截图:例如:我输入112 id并搜索。。此id包含在student表中,但不包含在studentresult表中。。所以我必须在这个id中插入标记。。但它会显示值为0的文本框。。之后,我删除0并输入标记,然后单击“更新”。。它显示更新的。。所以最后插入失败了:在更改过程后,一旦从studentresult中删除,studentresult表应该被重置。问题是,现在每个学生有多行。@Rani-可能需要在sp_updateresult中再编写一个查询或代码块,该查询或代码块除了现有的更新查询外还有一个INSERT查询。
ELSE IF EXISTS (SELECT * FROM student WHERE id=@id_student)
ALTER PROCEDURE sp_studentresult
(
    @id int,
    @output varchar(50) output,
    @id_student varchar(50),
    @Tamil Varchar (100),
    @English varchar (50),
    @Maths Varchar (50),
    @Science Varchar (50),
    @SocialScience Varchar (50) 
)
AS
IF NOT EXISTS (SELECT * FROM student WHERE id=@id_student)
BEGIN
SET @output='Doesn not EXIST'
END
ELSE IF EXISTS (SELECT * FROM student WHERE id=@id_student)
        AND NOT EXISTS (SELECT * FROM studentresult WHERE id_student=@id_student) 
BEGIN
INSERT into studentresult (id_student,Tamil,English,Maths,Science,SocialScience) values (@id_student,@Tamil,@English,@Maths,@Science,@SocialScience)
SET @output='Inserted'
END
SELECT * from studentresult where id_student=@id