GridView读取数据XLS ACCESS MDB 并写入到数据库

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel_CS.aspx.cs" Inherits="ReadExcel_CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="写入数据库" />
        <asp:GridView ID="GridView1" runat="server">
             </asp:GridView>
        &nbsp;
            <asp:GridView ID="GridView2" runat="server">
             </asp:GridView>
    </div>
    </form>
</body>
</html>
-------------------------------------

   using System;
 using System.Data;
 using System.Configuration;
 using System.Collections;
 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.OleDb;
 
 public partial class ReadExcel_CS : System.Web.UI.Page
{
     public DataTable DT;
     protected void Page_Load(object sender, EventArgs e)
     {
         //read excel file
         string strConn;
         strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
         "Data Source=C://test.xls;" +
         "Extended Properties=Excel 8.0;";
 
         OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
 
         DataSet myDataSet = new DataSet();
         myCommand.Fill(myDataSet, "ExcelInfo");
         this.GridView1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
         this.GridView1.DataBind();

          DT = (DataTable)myDataSet.Tables["ExcelInfo"];
         /*
         //read access file
         string strConn2;
         strConn2 = "Provider=Microsoft.Jet.OLEDB.4.0;" +
         "Data Source=c://test.mdb;" +
         "Persist Security Info=True;";
 
         OleDbDataAdapter myCommand2 = new OleDbDataAdapter("select * from test", strConn2);
 
         DataSet myDataSet2 = new DataSet();
         myCommand2.Fill(myDataSet2, "AccessInfo");
         this.GridView2.DataSource = myDataSet2.Tables["AccessInfo"].DefaultView;
         this.GridView2.DataBind();
          */
 
     }
     protected void Button1_Click(object sender, EventArgs e)
     {
         /*
         //写入数据库david
         for (int i = 1; i < GridView1.Rows.Count; i++)
         {
             for (int j = 1; j < 3 ; j++)
             {
                 string a = GridView1.Rows[i].Cells[j].ToString() ;
                 Response.Write(a.ToString() ); 
             }
            
         }
          */
         for (int i = 0; i < DT.Rows.Count; i++)
         {
             //for (int j = 0; j < DT.Columns.Count; j++)
             //{
                 string A=DT.Rows[i][0].ToString();
                 string B= DT.Rows[i][1].ToString();
                 string C = DT.Rows[i][2].ToString();
                 string D = DT.Rows[i][3].ToString();

                 string sqlinsert = "insert TEST(A,B,C,D)values('"+A+"','"+B+"','"+C+"','"+D+"')";
                 DataAccess.DataAccessSHDB.GetNonQuery(sqlinsert.ToString());  
                 //Response.Write(a.ToString()+","); 
             //}
         }
     }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用DevExpress控件的Gridview进行数据添加时,可以按照以下步骤进行操作: 1. 首先,需要确保已经连接到数据库,并创建好与Gridview所对应的数据表。 2. 接下来,需要使用GridView的DataRows属性来遍历所有行。可以使用循环来逐行遍历,然后获取每行的数据。 3. 对于每一行数据,需要创建一个与数据表对应的Insert SQL命令,将数据插入到数据库中。 4. 在循环中,逐行执行Insert SQL命令,将数据插入到数据库中。 下面是一个简单的示例代码,用于说明如何使用DevExpress的Gridview将行数据添加到数据库中: using DevExpress.XtraGrid.Views.Grid; using System.Data.SqlClient; private void AddRowsToDatabase() { // 连接到数据库 string connectionString = "YourConnectionString"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); // 获取GridView的DataRows属性 GridView gridView = gridView1; // 这里假设Gridview的名称为gridView1 for (int i = 0; i < gridView.DataRowCount; i++) { // 获取每一行的数据 object[] rowValues = ((DataRowView)gridView.GetRow(i)).Row.ItemArray; // 创建Insert SQL命令 SqlCommand command = new SqlCommand("INSERT INTO TableName (Column1, Column2, Column3) VALUES (@Value1, @Value2, @Value3)", connection); command.Parameters.AddWithValue("@Value1", rowValues[0]); command.Parameters.AddWithValue("@Value2", rowValues[1]); command.Parameters.AddWithValue("@Value3", rowValues[2]); // 执行Insert SQL命令 command.ExecuteNonQuery(); } // 关闭数据库连接 connection.Close(); } 请注意,以上代码仅为示例,需要根据实际情况进行调整。确保连接字符串正确,以及数据表的列名和参数名称等都正确对应。 希望以上回答能够帮助到您,如有更多疑问,请随时追问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值