如何查找插入的行数 [英] how to find number of rows inserted

查看:71
本文介绍了如何查找插入的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找结果集中插入的总行数





how to find total number of rows inserted in result set


    protected void Button1_Click(object sender, EventArgs e)
    {
       if(TextBox.Text !=String.Empty)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                string textBoxText = ((TextBox)row.FindControl("TextBox1")).Text;
                cmd = new SqlCommand("Insert into Text(number)values(@number)", con);
                cmd.Parameters.Add(new SqlParameter("@number", textBoxText));
                con.Open();
                int a = cmd.ExecuteNonQuery();
                con.Close();
                ShowAlertMessage("inserted");
                TextBox.Text = "";


            }

        }
else
        {
             ShowAlertMessage("Enter the value");

        }
    }

推荐答案

我看到几个问题,比如SQLserver停止响应或应用程序可能挂断。



而不是一个一个地插入值,使用存储过程(SP) [ ^ ],例如:

I see several issues, like SQLserver stop responding or application may hang-up.

Rather then inserting value one-by-one, use stored procedure (SP)[^], like:
CREATE PROCEDURE usp_AddValue
    @val INT
AS
BEGIN
   INSERT INTO TableName (FieldName)
   VALUES(@val)
   --return number of rows affected
   RETURN @@ROWCOUNT
END



为什么要使用SP?请注意 SQL注入 [ ^ ]。

如何:防止ASP.NET中的SQL注入 [ ^ ]

在他们阻止你之前停止SQL注入攻击 [ ^ ]

SQL安全:新的SQL截断攻击以及如何避免攻击 [ ^ ]

SQL注入及其如何避免 [ ^ ] br />
@@ ROWCOUNT(T-SQL) [ ^ ]

解决:使用GridView Web服务器控件中的存储过程显示数据 [< a href =http://msdn.microsoft.com/en-us/library/k10148y1%28v=vs.100%29.aspxtarget =_ blanktitle =New Window> ^ ]

如何:执行返回行的存储过程 [< a href =http://msdn.microsoft.com/en-us/library/d7125bke.aspxtarget =_ blanktitle =新窗口> ^ ]

如何:执行返回单个值的存储过程 [ ^ ]



我没有想法为什么要遍历行集合。听起来,就像你想要更新数据而不是添加数据。在这种情况下,使用SP:


Why to use SP? Becasue of SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Security: New SQL Truncation Attacks And How To Avoid Them[^]
SQL Injection and how to avoid it[^]
@@ROWCOUNT (T-SQL)[^]
Walkthrough: Displaying Data Using a Stored Procedure in the GridView Web Server Control[^]
How to: Execute a Stored Procedure that Returns Rows[^]
How to: Execute a Stored Procedure that Returns a Single Value[^]

I have no idea why do you loop through the collection of rows. Sounds, like you wanted to update data instead of adding them. In that case, use SP:

CREATE PROCEDURE usp_UpdateAll
    @val INT
AS
BEGIN
   UPDATE TableName SET FieldName = @val
   --return number of rows affected
   RETURN @@ROWCOUNT
END





如果你愿意喜欢更新单个记录:



If you would like to update single record:

CREATE PROCEDURE usp_UpdateSingle
    @id INT,
    @val INT
AS
BEGIN
   UPDATE TableName SET FieldName = @val
   WHERE Key = @id
   --return number of rows affected
   RETURN @@ROWCOUNT
END





祝你好运!



Good luck!


int insertCount =0;
con.Open();
foreach (GridViewRow row in GridView1.Rows)
{
    string textBoxText = ((TextBox)row.FindControl("TextBox1")).Text;
    cmd = new SqlCommand("Insert into Text(number)values(@number)", con);
    cmd.Parameters.AddWithValue("@number", textBoxText);
    int a = cmd.ExecuteNonQuery();
    if(a>0)
       insertCount++;
    TextBox.Text = "";

}
con.Close();
ShowAlertMessage(string.Format("{0} records inserted", insertCount));


这篇关于如何查找插入的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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