昨天寫了一半,一直沒弄清楚當ACCESS資料庫的串連代碼寫成類的時候路徑該怎麼寫,搞了半天,還是用絕對路徑解決了,似乎Server.MapPath沒法在cs檔案中使用.
要實現的功能如下:
盡量用類的思想來完成資料的插入,因為這個例子簡單,所以我也就不多說什麼.大家自己看代碼,不懂的可以到論壇交流.
1、首先是ACCESS資料庫的設計,資料庫名:myData,表名:student
欄位名稱 資料類型
sid 自動編號
sname 文本
score 數字
2、建立插入的頁面default.aspx,具體代碼如下:
<%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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:Label ID="Label1" runat="server" Text="姓名"></asp:Label>
<asp:TextBox ID="tbxName" runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label2" runat="server" Text="成績"></asp:Label>
<asp:TextBox ID="tbxScore" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="插入資料" />
</div>
</form>
</body>
</html>
3、雙擊default.aspx進入default.aspx.cs,代碼如下:
default.aspx.cs的主要代碼如下:
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
student myStu = new student();
myStu.sname = this.tbxName.Text;
myStu.score = Convert.ToInt32(this.tbxScore.Text);
int i= MyClass.insertScore(myStu);
if (i == 1)
{
Response.Write("插入成功");
}
else
{
Response.Write("插入失敗");
}
}
}
4、在App_Code建立兩個類,一個是MyClass.cs,另一個是student.cs,
MyClass.cs的主要代碼如下:
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;
/// <summary>
/// MyClass 的摘要說明
/// </summary>
public class MyClass
{
public MyClass()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
public static OleDbConnection creatCon()
{//Data Source=後面請寫你自己的資料庫的絕對路徑
return new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:/Documents and Settings/nan/My Documents/Visual Studio 2005/WebSites/WebSite3/App_Data/myData.mdb");
}
public static int insertScore(student myStu)
{
string cmdText = "insert into student(sname,score) values('" + myStu.sname + "','" + myStu.score + "')";
OleDbConnection con = MyClass.creatCon();
con.Open();
OleDbCommand cmd = new OleDbCommand(cmdText, con);
int result = Convert.ToInt32(cmd.ExecuteNonQuery());
con.Close();
return result;
}
}
student.cs的主要代碼如下:
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;
/// <summary>
/// student 的摘要說明
/// </summary>
public class student
{
public string sname;
public int score;
}