需要 Id 才能在 gridview 中上传 excel 工作表

Posted

技术标签:

【中文标题】需要 Id 才能在 gridview 中上传 excel 工作表【英文标题】:Need Id to Upload excel sheet in the gridview 【发布时间】:2015-04-15 15:43:54 【问题描述】:

我具有将 Excel 工作表数据上传到 gridview 的功能。数据将被插入到数据库的子表中。

现在,我的问题是。其中一列与主表有关系。

所以,除非我添加具有关系的列ID,否则它会给我错误

未提供Student_id

这是我的代码

using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))

    con.Open();
    SqlCommand cmd = new SqlCommand("Select count(email) from tbl_student_report where email=@email", con);
    cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = dt.Rows[i]["Email Id"].ToString();

    int count = (int)cmd.ExecuteScalar();

    if (count > 0)
    
        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Duplicate user in the sheet, Sheet will not be uploaded..!!!');window.location ='csrstudentprogress.aspx';", true);
        continue;
    
    cmd = new SqlCommand("INSERT INTO tbl_student_report(NgoId,student_id,name,email,class,attendance,english_subject_marks,math_subject_marks,academic_performance,extra_activities,social_skills,general_health,date_of_record,modified_date,status,active) VALUES(@NgoId,@student_id,@name,@email,@class,@attendance,@english_subject_marks,@math_subject_marks,@academic_performance,@extra_activities,@social_skills,@general_health,@date_of_record,@modified_date,@status,@active)", con);
    cmd.Parameters.Add("@NgoId", SqlDbType.Int).Value = dt.Rows[i]["NgoId"].ToString();
    cmd.Parameters.Add("@student_id", SqlDbType.Int).Value = dt.Rows[i]["StudentId"].ToString();
    cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = dt.Rows[i]["Name"].ToString();
    cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = dt.Rows[i]["Email Id"].ToString();
    cmd.Parameters.Add("@class", SqlDbType.VarChar).Value = dt.Rows[i]["Class"].ToString();
    cmd.Parameters.Add("@attendance", SqlDbType.Decimal).Value = dt.Rows[i]["Attendance"].ToString();

    cmd.Parameters.Add("@english_subject_marks", SqlDbType.Int).Value = dt.Rows[i]["English Subject Marks"].ToString();
    cmd.Parameters.Add("@math_subject_marks", SqlDbType.Int).Value = dt.Rows[i]["Maths Subject Marks"].ToString();
    cmd.Parameters.Add("@academic_performance", SqlDbType.NVarChar).Value = dt.Rows[i]["Academic Performance"].ToString();
    cmd.Parameters.Add("@extra_activities", SqlDbType.NVarChar).Value = dt.Rows[i]["Extra Activities"].ToString();
    cmd.Parameters.Add("@social_skills", SqlDbType.NVarChar).Value = dt.Rows[i]["Social Skills"].ToString();
    cmd.Parameters.Add("@general_health", SqlDbType.NVarChar).Value = dt.Rows[i]["General Health"].ToString();

    cmd.Parameters.Add("@status", SqlDbType.Bit).Value = dt.Rows[i]["Status"].ToString();
    cmd.Parameters.Add("@date_of_record", SqlDbType.DateTime).Value = dt.Rows[i]["Date Of Record"].ToString();
    cmd.Parameters.Add("@modified_date", SqlDbType.DateTime).Value = dt.Rows[i]["Modified Date"].ToString();
    cmd.Parameters.Add("@active", SqlDbType.Bit).Value = dt.Rows[i]["Active"].ToString();

    cmd.ExecuteNonQuery();

    con.Close();

    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Sheet uploaded successfully');window.location ='csrstudentprogress.aspx';", true);
 

请建议在这种情况下该怎么做,因为用户不会在 Excel 表中添加student_id 并上传。

我正在使用sql-server 2008

如何做到这一点??

【问题讨论】:

为什么不直接获取所需的 ID?您是否有某些原因无法根据您手头的信息运行查询来选择 id? @Pseudonym:我没听清楚。顺便问一下你在说什么。 我说的是添加一个你写的查询来访问你需要的 Id 列。 @Pseudonym:另外,我将在 Excel 表中上传大量数据。我也将如何编写查询? @Pseudonym: 你能帮我解决这个问题吗,因为我有点缺乏sql 【参考方案1】:

我通过如下尝试自己完成了它:-

助手类

public static DataTable GetUserIdByName(string userName,string userType)
    
        string conString = ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(conString))
        
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_User WHERE Username=@Username AND UserType=@UserType", con);
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = userName;
            cmd.Parameters.Add("@UserType", SqlDbType.VarChar).Value = userType;

            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(dr);
            return dt;
        
    

在导出函数中调用类就完成了任务:-

DataTable table = GeneralHelper.GetUserIdByName(Session["User"].ToString(), Session["UserType"].ToString());

            for (int i = 0; i < dt.Rows.Count; i++)
            
                using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
                
                    con.Open();

                    if (table != null && table.Rows.Count > 0)
                    
                        string StudentId = GetNgoIdStudentId(dt.Rows[i]["Email Id"].ToString());
                        if (StudentId != null)
                        
                            SqlCommand cmd = new SqlCommand("INSERT INTO tbl_student_report(student_id,name,emailid,class,attendance,english_subject_marks,math_subject_marks,academic_performance,extra_activities,social_skills,general_health,date_of_record,modified_date,status,active) VALUES(@student_id,@name,@emailid,@class,@attendance,@english_subject_marks,@math_subject_marks,@academic_performance,@extra_activities,@social_skills,@general_health,@date_of_record,@modified_date,@status,@active)", con);

                            cmd.Parameters.Add("@NgoId", SqlDbType.Int).Value = table.Rows[0]["NgoId"].ToString();
                            cmd.Parameters.Add("@student_id", SqlDbType.Int).Value = StudentId;

                            cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = dt.Rows[i]["Name"].ToString();
                            cmd.Parameters.Add("@emailid", SqlDbType.NVarChar).Value = dt.Rows[i]["Email Id"].ToString();
                            cmd.Parameters.Add("@class", SqlDbType.VarChar).Value = dt.Rows[i]["Class"].ToString();
                            cmd.Parameters.Add("@attendance", SqlDbType.Decimal).Value = dt.Rows[i]["Attendance"].ToString();

                            cmd.Parameters.Add("@english_subject_marks", SqlDbType.Int).Value = dt.Rows[i]["English Subject Marks"].ToString();
                            cmd.Parameters.Add("@math_subject_marks", SqlDbType.Int).Value = dt.Rows[i]["Maths Subject Marks"].ToString();
                            cmd.Parameters.Add("@academic_performance", SqlDbType.NVarChar).Value = dt.Rows[i]["Academic Performance"].ToString();
                            cmd.Parameters.Add("@extra_activities", SqlDbType.NVarChar).Value = dt.Rows[i]["Extra Activities"].ToString();
                            cmd.Parameters.Add("@social_skills", SqlDbType.NVarChar).Value = dt.Rows[i]["Social Skills"].ToString();
                            cmd.Parameters.Add("@general_health", SqlDbType.NVarChar).Value = dt.Rows[i]["General Health"].ToString();

                            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = dt.Rows[i]["Status"].ToString();
                            if (string.IsNullOrEmpty(dt.Rows[i]["Date Of Record"].ToString()))
                            
                                cmd.Parameters.Add("@date_of_record", SqlDbType.DateTime).Value = DateTime.Now;
                            
                            else
                            
                                cmd.Parameters.Add("@date_of_record", SqlDbType.DateTime).Value = dt.Rows[i]["Date Of Record"].ToString();
                            
                            if (string.IsNullOrEmpty(dt.Rows[i]["Modified Date"].ToString()))
                            
                                cmd.Parameters.Add("@modified_date", SqlDbType.DateTime).Value = DateTime.Now;
                            
                            else
                            
                                cmd.Parameters.Add("@modified_date", SqlDbType.DateTime).Value = dt.Rows[i]["Modified Date"].ToString();
                            
                            cmd.Parameters.Add("@active", SqlDbType.Bit).Value = dt.Rows[i]["Active"].ToString();

                            cmd.ExecuteNonQuery();

                            con.Close();

                            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Sheet uploaded successfully');window.location ='csrstudentprogress.aspx';", true);
                        
                        else
                        
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Student not found');", true);
                        
                    
                    else
                    
                        //Error
                    
                
            

【讨论】:

以上是关于需要 Id 才能在 gridview 中上传 excel 工作表的主要内容,如果未能解决你的问题,请参考以下文章

调用按钮单击gridview内的文件上传

datagridview控件必须绑定数据库才能使用吗

tabHost 底下增加一个GridView菜单,要怎么布局呢?

将所选图像从库中上传到gridview

android之使用GridView+仿微信图片上传功能(附源代码)

如何在 c# ASP.net 中从 GridView 行进行单按钮照片上传