asp.net 2.0下用access开发的时候要注意的问题
來(lái)源:http://www.study-code.com/dotnet/aspnet/67492.htm
?
1、數(shù)據(jù)庫(kù)要給訪問(wèn)權(quán)限,在我的電腦里,右擊access數(shù)據(jù)庫(kù),點(diǎn)擊“屬性”菜單,選“安全”選項(xiàng)卡,選擇Users組,賦給完全控制的權(quán)限
如果沒(méi)有安全選項(xiàng)卡,點(diǎn)擊菜單“工具”->“文件夾選項(xiàng)”,進(jìn)入“查看”選項(xiàng)卡,把“使用簡(jiǎn)單文件共享”前的勾去掉
2、連接數(shù)據(jù)庫(kù)的時(shí)候,如果想要做到可以隨時(shí)更改數(shù)據(jù)庫(kù)的名稱(chēng),也不用重新編譯網(wǎng)站,就把數(shù)據(jù)庫(kù)的路徑記錄在web.config文件里:
??? <connectionStrings>
????? <add name="aceConnectionString" connectionString="目錄\\數(shù)據(jù)庫(kù)名稱(chēng).mdb" providerName="System.Data.OleDb" />
??? </connectionStrings>
使用這兩個(gè)方法取值:
public static string connectString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["aceConnectionString"].ConnectionString);
public static string GetDB()
??? {
??????? return HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["aceConnectionString"].ConnectionString);
??? }
在每個(gè)頁(yè)面,如果用到AccessDataSource控件,這個(gè)控件是不會(huì)自己讀取這個(gè)值的,可以在Page_Load頁(yè)面里,使用AdsAd.DataFile = NnllClass.GetDB();給他賦值
protected void Page_Load(object sender, EventArgs e)
??? {
??????? AdsAd.DataFile = NnllClass.GetDB();
??? }
3、如果使用AccessDataSource添加、刪除、修改記錄,要注意參數(shù)的問(wèn)題,有一些網(wǎng)站,對(duì)一些參數(shù)報(bào)錯(cuò)的信息,給出的答案說(shuō)access數(shù)據(jù)庫(kù)應(yīng)該使用拼接的sql語(yǔ)句,不可以使用參數(shù),這種說(shuō)法是不對(duì)的,access數(shù)據(jù)庫(kù)也可以使用參數(shù)傳遞的方式增刪改查數(shù)據(jù)庫(kù)的
就是,因?yàn)関s.net 2005不可以像使用ms sql時(shí)一樣子,自動(dòng)會(huì)檢索出參數(shù),所以全部參數(shù)要自己手動(dòng)添加,在添加參數(shù)的時(shí)候,要很小心,不要少了,也不要多了,也不要搞亂了順序
發(fā)現(xiàn)AccessDataSource控件,好像是根據(jù)綁定字段的順序來(lái)對(duì)應(yīng)傳入的參數(shù)的,也就是說(shuō),如果你在FormView里,是這樣子綁定的:
<asp:TextBox ID="myField1TextBox" runat="server" Text='<%# Bind("myField1") %>' MaxLength="20"></asp:TextBox>
<asp:TextBox ID="myField2TextBox" runat="server" Text='<%# Bind("myField2") %>' MaxLength="20"></asp:TextBox>
定義參數(shù)的時(shí)候,就應(yīng)該這樣子定義:
?<InsertParameters>
??????????? <asp:Parameter Name="myField1" Type="String" Size="20" ConvertEmptyStringToNull="False" />
??????????? <asp:Parameter Name="myField2" Type="String" Size="20" ConvertEmptyStringToNull="False" />
??????? </InsertParameters>
次序不要搞亂了,搞亂了數(shù)據(jù)格式不同時(shí),會(huì)報(bào)錯(cuò),具體的出錯(cuò)信息我忘記了
定義參數(shù)的時(shí)候,還有一個(gè)要注意的問(wèn)題就是,參數(shù)名不要寫(xiě)錯(cuò),參數(shù)不要寫(xiě)漏
使用sql數(shù)據(jù)庫(kù)時(shí),參數(shù)名不對(duì),或者參數(shù)個(gè)數(shù)不對(duì),都會(huì)得到很明顯的出錯(cuò)信息,就是,access數(shù)據(jù)庫(kù),在這種時(shí)候,是不報(bào)錯(cuò)的,如果參數(shù)名寫(xiě)錯(cuò)了,只會(huì)說(shuō)有n個(gè)參數(shù)沒(méi)有值,如果是寫(xiě)漏了參數(shù),他有時(shí)候會(huì)直接替你更新數(shù)據(jù)庫(kù),而更新的時(shí)候,是以綁定的順序來(lái)對(duì)應(yīng)參數(shù)的值的,這時(shí)候,如果后面的參數(shù)值,大于前面的參數(shù)對(duì)應(yīng)的字段大小,就會(huì)得到“字段太小”的出錯(cuò)信息
?
<InsertParameters>
??????????? <asp:Parameter Name="myField1" Type="String" Size="20" ConvertEmptyStringToNull="False" />
??????????? <asp:Parameter Name="myField2" Type="String" Size="20" ConvertEmptyStringToNull="False" />
??????? </InsertParameters>
如果少了一個(gè)
??????????? <asp:Parameter Name="myField3" Type="String" Size="200" ConvertEmptyStringToNull="False" />
在更新的時(shí)候,會(huì)把myField3的值,寫(xiě)進(jìn)myField2里,如果myField3的值的長(zhǎng)度大于20,就會(huì)報(bào)“字段太小”了
3、如果不習(xí)慣AccessDataSource控件,那就自己綁定數(shù)據(jù)吧,給出一些方法:
#region 返回?cái)?shù)據(jù)庫(kù)地址
??? public static string GetDB()
??? {
??????? return HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["aceConnectionString"].ConnectionString);
??? }
??? #endregion
???
#region 打開(kāi)數(shù)據(jù)庫(kù):SqlConnection OpenConnect()
??? public OleDbConnection OpenConnect(Page aPage,bool setMsg)
??? {
??????? try
??????? {
??????????? OleDbConnection dbConnect = new OleDbConnection(connectString);
??????????? if (!dbConnect.State.Equals(ConnectionState.Open))
??????????? {
??????????????? dbConnect.Open();
??????????? }
??????????? return dbConnect;
??????? }
??????? catch (OleDbException ex)
??????? {
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, "連接數(shù)據(jù)庫(kù)失敗:" + ex.Message.Replace("\n", "").Replace("\r", ""));
??????????? }
??????????? return null;
??????? }
??? }
??? #endregion
???
#region 關(guān)閉數(shù)據(jù)庫(kù):void CloseConnect(SqlConnection dbConnect)
??? public void CloseConnect(OleDbConnection dbConnect)
??? {
??????? if (dbConnect.State.Equals(ConnectionState.Open))
??????? {
??????????? dbConnect.Close();
??????? }
??? }
??? #endregion
???
#region 讀取數(shù)據(jù),返回DataSet
??? public DataSet GetDataSet(Page aPage, OleDbConnection dbConnection, CommandType commandType, string commandText, out string msg, bool setMsg)
??? {
??????? msg = "";
??????? //創(chuàng)建并設(shè)置SqlCommand
??????? OleDbCommand dbCommand = new OleDbCommand();
??????? dbCommand.Connection = dbConnection;
??????? dbCommand.CommandType = commandType;
??????? dbCommand.CommandText = commandText;
??????? //創(chuàng)建SqlDataAdapter并獲取數(shù)據(jù)
??????? DataSet ds = new DataSet();
??????? try
??????? {
??????????? OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
??????????? dataAdapter.Fill(ds);
??????? }
??????? catch (InvalidOperationException eo)
??????? {
??????????? msg = "讀取數(shù)據(jù)失敗:" + eo.Message.Replace("\n", "").Replace("\r", "") + "\\n";
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, msg);
??????????? }
??????? }
??????? catch (OleDbException ex)
??????? {
??????????? msg = "讀取數(shù)據(jù)失敗:" + ex.Message.Replace("\n", "").Replace("\r", "") + "\\n";
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, msg);
??????????? }
??????? }
??????? //返回?cái)?shù)據(jù)
??????? return ds;
??? }
??? #endregion
???
#region 讀取數(shù)據(jù),返回SqlDataReader
??? public OleDbDataReader GetDataReader(Page aPage, OleDbConnection dbConnection, CommandType commandType, string commandText, OleDbParameter[] p, out string msg, bool setMsg)
??? {
??????? msg = "";
??????? //設(shè)置命令
??????? OleDbCommand dbCommand = new OleDbCommand();
??????? dbCommand.Connection = dbConnection;
??????? dbCommand.CommandType = commandType;
??????? dbCommand.CommandText = commandText;
??????? //設(shè)置參數(shù)
??????? if (p != null)
??????? {
??????????? foreach (OleDbParameter pi in p)
??????????? {
??????????????? dbCommand.Parameters.Add(pi);
??????????? }
??????? }
??????? //讀取第一個(gè)結(jié)果集
??????? OleDbDataReader dbReader = null;
??????? try
??????? {
??????????? dbReader = dbCommand.ExecuteReader();
??????? }
??????? catch (InvalidOperationException eo)
??????? {
??????????? msg = "讀取數(shù)據(jù)失敗:" + eo.Message.Replace("\n", "").Replace("\r", "") + "\\n";
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, msg);
??????????? }
??????? }
??????? catch (OleDbException ex)
??????? {
??????????? msg = "讀取數(shù)據(jù)失敗:" + ex.Message.Replace("\n", "").Replace("\r", "") + "\\n";
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, msg);
??????????? }
??????? }
??????? return dbReader;
??? }
??? #endregion
???
#region 執(zhí)行sql命令或存儲(chǔ)過(guò)程
??? public int execCommand(Page aPage, OleDbConnection dbConnection, CommandType commandType, string commandText, OleDbParameter[] p, out string msg, bool setMsg)
??? {
??????? bool needCloseDB = false;
??????? int row = 0;
??????? msg = "";
??????? //創(chuàng)建并設(shè)置SqlConnection
??????? if (dbConnection == null)
??????? {
??????????? needCloseDB = true;
??????????? dbConnection = OpenConnect(aPage, setMsg);
??????????? if (dbConnection == null) return -1;
??????? }
??????? //創(chuàng)建并設(shè)置SqlCommand
??????? OleDbCommand dbCommand = new OleDbCommand();
??????? dbCommand.Connection = dbConnection;
??????? dbCommand.CommandType = commandType;
??????? dbCommand.CommandText = commandText;
??????? //設(shè)置參數(shù)
??????? if (p != null)
??????? {
??????????? foreach (OleDbParameter pi in p)
??????????? {
??????????????? dbCommand.Parameters.Add(pi);
??????????? }
??????? }
??????? //執(zhí)行命令
??????? try
??????? {
??????????? row = dbCommand.ExecuteNonQuery();
??????? }
??????? catch (InvalidOperationException eo)
??????? {
??????????? msg = eo.Message.Replace("\n", "").Replace("\r", "") + "\\n";
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, msg);
??????????? }
??????????? row = -1;
??????? }
??????? catch (OleDbException ex)
??????? {
??????????? //執(zhí)行過(guò)程出錯(cuò)
??????????? msg = ex.Message.Replace("\n", "").Replace("\r", "") + "\\n";
??????????? if (setMsg)
??????????? {
??????????????? SetMsgJS(aPage.Master, msg);
??????????? }
??????????? row = -1;
??????? }
??????? //關(guān)閉數(shù)據(jù)庫(kù)
??????? if (needCloseDB)
??????? {
??????????? CloseConnect(dbConnection);
??????? }
??????? return row;
??? }
??? #endregion
上面的方法都用到一個(gè)SetMsgJS方法,這個(gè)方法是用來(lái)給母版頁(yè)設(shè)置信息的
public static void SetMsgJS(MasterPage masterPage, string msg)
??? {
??????? Literal lt = (Literal)masterPage.FindControl("LiteralMsg");
??????? if (lt != null)
??????? {
??????????? lt.Text += msg;
??????? }
??? }
在母版頁(yè)里定義是這樣子的:
???
<script type="text/javascript">
??????? var msg = "";
??????? msg = "<asp:Literal ID="LiteralMsg" runat="server"></asp:Literal>";
??????? if(msg != "")
??????? {
??????????? alert(msg);
??????? }
??? </script>
轉(zhuǎn)載于:https://www.cnblogs.com/kangshifu/archive/2008/08/11/1265124.html
總結(jié)
以上是生活随笔為你收集整理的asp.net 2.0下用access开发的时候要注意的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Flex 幻灯片播放
- 下一篇: Groovy预览--文本处理