excel导入sql代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            string path = System.IO.Path.GetFullPath(this.FileUpload1.PostedFile.FileName);
            string conn = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =" + path + ";Extended Properties=Excel 8.0";
            //"Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" + "Extended Properties=Excel 8.0;"
            OleDbConnection thisconnection = new OleDbConnection(conn);
            thisconnection.Open();
            string Sql = "select * from [Sheet1$]";
            OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
            DataSet ds = new DataSet();

            mycommand.Fill(ds);
            int x = ds.Tables[0].Rows.Count;
            string xx = ds.Tables[0].Rows[1][0].ToString();
            SqlConnection thisconnection1 = new SqlConnection("server=192.168.0.250;database=sms_mjn;uid=111;pwd=1111");
            thisconnection1.Open();
            int count = ds.Tables[0].Rows.Count;
            for (int j = 0; j < count; j++)
            {
                string id_1;
                id_1 = ds.Tables[0].Rows[j][0].ToString();
                string id_2 = ds.Tables[0].Rows[j][1].ToString();
                string id_3 = ds.Tables[0].Rows[j][2].ToString();
                string excelsql = "insert into sendto (phone,msg,status0)values('" + id_1 + "','" + id_2 + "','" + id_3 + "')";
                SqlCommand mycommand1 = new SqlCommand(excelsql, thisconnection1);
                mycommand1.ExecuteNonQuery();
            }

            thisconnection1.Close();
            thisconnection.Close();
            Response.Write("<script>alert('添加成功!');</script>");

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
  
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值