如何在使用C#上传到数据库之前验证Excel数据 [英] How to validate excel data before uploading to database using C#

查看:141
本文介绍了如何在使用C#上传到数据库之前验证Excel数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我可以将批量数据上传到SQLServer数据库,但在上传之前我需要验证Excel数据。



例如:如果任何单元格为空,则必须显示msg,如plz填充单元格..



我用googled很多没有运气:( Plz帮助我尽快,因为它非常重要。

Hi All,

I''m able to upload bulk data to SQLServer DB but before uploading I need to validate excel data.

For example: If any cell left as empty, have to show msg like plz fill the cell..

I googled alot bt no luck:( Plz help me asap as it is very important.

推荐答案

我使用以下代码从工作表获取数据到数据表。请检查是否我会帮助你。



I get the data from a sheet to datatable with the following code. Please check if it''ll help you or not.

/// <summary>
        /// Fetch Sheet As DataTable
        /// </summary>
        /// <param name="SheetName">SheetName</param>
        /// <returns>A DataTable</returns>
        public static System.Data.DataTable FetchSheet(string ExcelFilePath, string SheetName)
        {
            try
            {
                string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + ExcelFilePath + ";" + "Extended Properties='Excel 8.0;HDR=YES;'";
                OleDbConnection objConn = new OleDbConnection(sConnectionString);
                objConn.Open();
                OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + SheetName + "


,objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
System.Data.DataTable dtReturned = new System.Data.DataTable();
objAdapter.Fill(dtReturned);
objConn.Close();
return dtReturned;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null ;
}
}
", objConn); OleDbDataAdapter objAdapter = new OleDbDataAdapter(); objAdapter.SelectCommand = objCmdSelect; System.Data.DataTable dtReturned = new System.Data.DataTable(); objAdapter.Fill(dtReturned); objConn.Close(); return dtReturned; } catch (Exception ex) { MessageBox.Show(ex.Message); return null; } }


HI,



在插入数据之前数据库,您可以使用for / foreach将该信息存储在一个数据表

中,您可以使用该数据表获取所有行。

在页面中,您获取Excel列值,然后你可以检查列是否为空意味着你显示一些消息或其他数据插入数据库。



最近我也做了同样的事情这对我有用。


Before insert data to database , you can store that information in one datatable
by using for / foreach you can fetch all the rows by using that datatable.
In page wise your fetching the Excel column value, then u can check that column is empty means you display some message or otherwise data to be inserted into database.

Recently i''m also do the same thing this is works for me.


这篇关于如何在使用C#上传到数据库之前验证Excel数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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