在数据表中追加带有记录行的变量 [英] Append a variable with row of record in datatable

查看:70
本文介绍了在数据表中追加带有记录行的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有以下代码,用于从存储过程中检索记录行:



Hi, i have the following codes in C# which is to retrieve rows of records from stored procedure:

using (conn = new SqlConnection(GeminiConnString))
{
    using (command = new SqlCommand("dbo.CZ_InsertIntoProjectManagementOutstandingExcel", conn))
    {
            conn.Open();
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@startDate", StartDate);
            command.Parameters.AddWithValue("@endDate", EndDate);

            command.ExecuteNonQuery();

        //Sto-Pro Details
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dt = ds.Tables[0];

        conn.Close();
    }
}





然后我想附加一个值,例如编号,例如1进入第一行记录,2进入第二行记录。我有以下代码:





Then after that i want to append a value which is the numbering e.g. 1 into the first row of record and 2 to the second row of record. I have the below codes:

//the lastColValue is the last value i retrieve from a column name 'No'. Then the '1' above or any other numbering will follow after the last value. E.g. lastColValue is 10 then newColValue would be 11
string lastColvalue = ExcelDt.Rows[ExcelDt.Rows.Count - 1]["No"].ToString();
int newColValue = Convert.ToInt32(lastColvalue);
for (int i = 0; i < dt.Rows.Count; i++)
{
    newColValue = newColValue + 1;
    //add here
}

推荐答案





请不要使用此代码更新一列,每行的ID。



在查询中使用以下语法作为额外列并在数据表中获取然后它将自动生成。它会提高你的性能并减少时间。



Hi,

Please not use this code for updating one column, id for every Row.

Use below syntax in your query as an extra column and fetch in datatable then it will come automatically. It will increase your performance and reduce time.

Row_Number() over(order by (Select 0)) as SrNo


这篇关于在数据表中追加带有记录行的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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