简体   繁体   中英

import data from excel to sql using asp.net

when i import data from excel to sql loops dont stop when columns became null, how i do this.

this is my code :-

protected void insertdata_Click(object sender, EventArgs e)
{
        OleDbConnection oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("test4.xlsx") + ";Extended Properties=Excel 12.0;");
        try
        {
            OleDbCommand ocmd = new OleDbCommand("select [Notifications],[ReqStart],[Type],[ReqTime],[OrderNum],[FuncLoc],[CustomerCode],[CustomerName],[Rg],[city],[Phone],[Street],[Distrit] from [Sheet1$]", oledbConn);
            oledbConn.Open();
            OleDbDataReader odr = ocmd.ExecuteReader();

            string Notifications = "";
            string ReqStart = " ";
            string Type = " ";
            string ReqTime = " ";
            string OrderNum = " ";
            string FuncLoc = " ";
            string CustomerCode = " ";
            string CustomerName = " ";
            string Rg = " ";
            string city = " ";
            string Phone = " ";
            string Street = " ";
            string Distrit = " "; 


            while (odr.Read())
            {
                Notifications = valid(odr, 0);
                ReqStart = valid(odr, 1);
                Type = valid(odr, 2);
                ReqTime = valid(odr, 3);
                OrderNum = valid(odr, 4);
                FuncLoc = valid(odr, 5);
                CustomerCode = valid(odr, 6);
                CustomerName = valid(odr, 7);
                Rg = valid(odr, 8);
                city = valid(odr, 9);
                Phone = valid(odr, 10);
                Street = valid(odr, 11);
                Distrit = valid(odr, 12);

                insertdataintosql(Notifications, ReqStart, Type, ReqTime, OrderNum, FuncLoc, CustomerCode, CustomerName, Rg, city, Phone, Street, Distrit);

            }
            oledbConn.Close();

        }
        catch(DataException ex)
        {
            lblmssg.Text = ex.Message;
            lblmssg.ForeColor = System.Drawing.Color.Red;
        }
        finally
        {
            lblmssg.Text = "Data Inserted Sucessfully";
            lblmssg.ForeColor = System.Drawing.Color.Green;
        }
    }

    protected string valid(OleDbDataReader myreader, int stval)
    {
        object val = myreader[stval];
        if (val != DBNull.Value)
        {
            return val.ToString();
        }
        else
        {
            return Convert.ToString(0);
        }
    }

    public void insertdataintosql(string Notifications, string ReqStart, string Type, string ReqTime, string OrderNum, string FuncLoc, string CustomerCode, string CustomerName, string Rg, string city, string Phone, string Street, string Distrit)
    {
        SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=White_Whale;Integrated Security=True");


        using (SqlCommand cmd=new SqlCommand())
        {
            cmd.Connection = sqlconn;
            cmd.CommandText = "insert into Customers(customer_code,customer_name,RgCode,CityCode,Phone,Distriet,Street) values(@customer_code,@customer_name,@RgCode,@CityCode,@Phone,@Distriet,@Street)";
            cmd.Parameters.Add("@customer_code", SqlDbType.VarChar, 20).Value = CustomerCode;
            cmd.Parameters.Add("@customer_name", SqlDbType.VarChar, 100).Value = CustomerName;
            cmd.Parameters.Add("@RgCode", SqlDbType.Int).Value = Convert.ToInt32(Rg);
            cmd.Parameters.Add("@CityCode", SqlDbType.Int).Value = Convert.ToInt32(city);
            cmd.Parameters.Add("@Phone", SqlDbType.VarChar, 12).Value = Phone;
            cmd.Parameters.Add("@Distriet", SqlDbType.VarChar, 12).Value = Distrit;
            cmd.Parameters.Add("@Street", SqlDbType.NVarChar, 100).Value = Street;
            cmd.CommandType = CommandType.Text;
            sqlconn.Open();
            cmd.ExecuteNonQuery();
            sqlconn.Close();
        }

        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = sqlconn;
            cmd.CommandText = "insert into Notification(notification,type_code,order_Num,funLoc_Code,customer_code) values (@notification,@type_code,@order_Num,@funLoc_Code,@customer_code)";
            cmd.Parameters.Add("@notification", SqlDbType.VarChar,30).Value = Notifications;

            //cmd.Parameters.Add("@req_start", SqlDbType.Date).Value = DateTime.ParseExact(ReqStart,"dd-MM-yyyy",null);
            cmd.Parameters.Add("@type_code", SqlDbType.Int).Value = Convert.ToInt32(Type);
            //cmd.Parameters.Add("@ReqTime", SqlDbType.Bit).Value = ReqTime;
            cmd.Parameters.Add("@order_Num", SqlDbType.VarChar, 30).Value = OrderNum;
            cmd.Parameters.Add("@funLoc_Code", SqlDbType.Int).Value = Convert.ToInt32(OrderNum);
            cmd.Parameters.Add("@customer_code", SqlDbType.VarChar, 20).Value = CustomerCode;
            cmd.CommandType = CommandType.Text;
            sqlconn.Open();
            cmd.ExecuteNonQuery();
            sqlconn.Close();
        }

    }
}

One thing you could do is create an array of objects equal to the number of columns in your spreadsheet and then check if all of those values are DBNull using LINQ

object[] dataTest = new object[colCount];
If (dataTest.All(x => x == DBNull.Value))
    break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM