default.aspx.cs

来自「用Excel文件做数据库进行数据的读写,实现数据集的读取」· CS 代码 · 共 61 行

CS
61
字号
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.OleDb;

//*********************Write by PudnPh

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            string Username = TextBox1.Text.ToString();
            string Password = TextBox2.Text.ToString();

            string Path = Server.MapPath("App_Data") + "\\FreeDB.xls";

            string ConnStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+Path+"';Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(ConnStr);
            conn.Open();

            string SelectText = "select [UserID] from [Sheet1$]";
            OleDbDataAdapter da = new OleDbDataAdapter(SelectText, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            int UserID=0;

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                UserID = Convert.ToInt32(ds.Tables[0].Rows[i]["UserID"].ToString());
            }

            UserID = UserID + 1;

            SelectText = "INSERT INTO[Sheet1$]([UserID],[UserName],[Password]) VALUES ('" + UserID + "','" + Username + "','" + Password + "')";

            OleDbCommand cmd = new OleDbCommand(SelectText, conn);

            int j = cmd.ExecuteNonQuery();
            if (j > 0)
            {
                Response.Write("<script>alert('注册成功')</script>");
            }
            conn.Close();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?