.Net+MySQL組合開發(二) 數據訪問篇

一、建立數據庫、表、添加數據
這里我們使用圖形化操作的SQL Manager 2005 Lite for MySQL來建立數據,它的操作界面非常類似OFFICE軟件,使用方便、很容量上手、下面開始建立數據庫及表
單擊"Creat New DataBase":新建DB

輸入密碼:


選擇客戶端編碼為gb2312,防亂碼;也可以在新建好的數據庫右鍵選擇"DataBase Registration Info"更改編碼:

新建表:輸入表名:

新建字段




點擊加號,手動添加數據,點出對號,提交數據:

選擇DDL選項,直接看到剛才操作的腳本;也可以不用上面的方式操作,直接寫sql腳本來創建數據:

二、ADO.NET數據操作
推薦使用MySQL Connector Net 5.0.3
web.config添加連接字符串:
None.gif<add?name="MySqlServer"?connectionString="Data?Source=127.0.0.1;User?ID=root;Password=123;DataBase=BOOK;Charset=gb2312"/>

為了方便,把對數據的訪問封裝到一個類中:
?1None.gif//?執行sql?
?2None.gifpublic?int?ExecuteSql(string?strSql,?MySqlParameter?[]?myPar)
?3ExpandedBlockStart.gif????{
?4InBlock.gif????????try
?5ExpandedSubBlockStart.gif????????{
?6InBlock.gif????????????myConnection.Open();
?7InBlock.gif????????????MySqlCommand?cmd?=?new?MySqlCommand(strSql,?myConnection);
?8InBlock.gif????????????if?(myPar?!=?null)
?9ExpandedSubBlockStart.gif????????????{
10InBlock.gif????????????????foreach?(MySqlParameter?spar?in?myPar)
11ExpandedSubBlockStart.gif????????????????{
12InBlock.gif????????????????????cmd.Parameters.Add(spar);
13ExpandedSubBlockEnd.gif????????????????}

14ExpandedSubBlockEnd.gif????????????}

15InBlock.gif????????????int?result?=?cmd.ExecuteNonQuery();
16InBlock.gif????????????myConnection.Close();
17InBlock.gif????????????return?result;
18ExpandedSubBlockEnd.gif????????}

19InBlock.gif????????catch
20ExpandedSubBlockStart.gif????????{
21InBlock.gif????????????return?0;
22ExpandedSubBlockEnd.gif????????}

23ExpandedBlockEnd.gif????}
獲取數據:
?1None.gif?public?DataSet?GetDataSet(string?strSql)
?2ExpandedBlockStart.gif????{
?3InBlock.gif????????try
?4ExpandedSubBlockStart.gif????????{
?5InBlock.gif????????????MySqlDataAdapter?da?=?new?MySqlDataAdapter(strSql,?myConnection);
?6InBlock.gif????????????DataSet?ds?=?new?DataSet();
?7InBlock.gif????????????da.Fill(ds);
?8InBlock.gif????????????return?ds;
?9ExpandedSubBlockEnd.gif????????}

10InBlock.gif????????catch
11ExpandedSubBlockStart.gif????????{
12InBlock.gif????????????return?null;
13ExpandedSubBlockEnd.gif????????}

14ExpandedBlockEnd.gif????}

在頁面中我們用一個gridview實現數據的讀取、寫入、編輯、刪除等操作:

數據綁定:
1None.gif?//databind
2None.gif????protected?void?BindGrid()
3ExpandedBlockStart.gif????{
4InBlock.gif????????DataSet?ds?=?obj.GetDataSet("SELECT?*?FROM?book?ORDER?BY?bid");
5InBlock.gif????????GridView1.DataSource?=?ds;
6InBlock.gif????????GridView1.DataBind();
7InBlock.gif????????ds.Dispose();
8ExpandedBlockEnd.gif????}
添加數據:
?1None.gif?string?strSQL?=?"INSERT?INTO?book?(bname,author,publish)?VALUES?(?bname,?author,?publish)";
?2None.gif????????????MySqlParameter[]?mysp?=
?3ExpandedBlockStart.gif????????????{
?4InBlock.gif????????????????new?MySqlParameter?("?bname",MySqlDbType.VarChar?),
?5InBlock.gif????????????????new?MySqlParameter?("?author",MySqlDbType.VarChar),
?6InBlock.gif????????????????new?MySqlParameter?("?publish",MySqlDbType.VarChar)
?7ExpandedBlockEnd.gif????????????}
;
?8None.gif????????????mysp[0].Value?=?txtName.Text.Trim();
?9None.gif????????????mysp[1].Value?=?txtAuthor.Text.Trim();
10None.gif????????????mysp[2].Value?=?txtPublish.Text.Trim();
11None.gif????????????if?(obj.ExecuteSql(strSQL,?mysp)?==?1)
12ExpandedBlockStart.gif????????????{
13InBlock.gif????????????????Response.Write("<script>alert('提交成功');</script>");
14InBlock.gif????????????????BindGrid();
15InBlock.gif????????????????txtName.Text?=?txtAuthor.Text?=?txtPublish.Text?=?"";
16ExpandedBlockEnd.gif????????????}
注意參數符號是"?"而不是"@",這一點不同于sql server
其它編輯等操作代碼等請下載文件
下載詳細完整的代碼文件/Files/chy710/MySQL_ADONET.rar
下篇預告:mysql開發中的亂碼問題
本文轉自chy710博客園博客,原文鏈接:http://www.cnblogs.com/chy710/archive/2007/02/02/637935.html,如需轉載請自行聯系原作者

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280137.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280137.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280137.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Git vs SVN

一、Git vs SVN Git 和 SVN 孰優孰好&#xff0c;每個人有不同的體驗。Git是分布式的&#xff0c;SVN是集中式的這是 Git 和 SVN 最大的區別。若能掌握這個概念&#xff0c;兩者區別基本搞懂大半。因為 Git 是分布式的&#xff0c;所以 Git 支持離線工作&#xff0c;在本地可以…

.net api 和java平臺對接技術總結

這兩天 一直和京東對接接口&#xff0c;我們用.net api 提供接口&#xff0c;對方用java調用&#xff0c;本來沒什么問題&#xff0c;但是對方對數據安全要求特別嚴&#xff0c;要驗簽&#xff0c;于是噩夢開始了。 1、在傳輸的時候&#xff0c;約定傳輸格式&#xff1a; HttpW…

Burpsuite學習(4)

2019獨角獸企業重金招聘Python工程師標準>>> burpsuite spider模塊通過跟蹤 HTML 和 JavaScript 以及提交的表單中的超鏈接來映射目標應用程序&#xff0c;它還使用了一些其他的線索&#xff0c;如目錄列表&#xff0c;資源類型的注釋&#xff0c;以及 robots.txt 文…

Git刪除分支/恢復分支

這是https://www.cnblogs.com/utank/p/7880441.html的方法&#xff0c;雖然很老現在有點不一樣&#xff0c;但總體還是能用的。 總結就是兩種方法 1.用commit的id恢復 2.用reflog的頭指針恢復 ?刪除一個已被終止的分支 如果需要刪除的分支不是當前正在打開的分支&#xff0c;使…

NetCore2.0Web應用之Startup

為什么80%的碼農都做不了架構師&#xff1f;>>> 作為main函數的程序啟動文件UseStartup 默認就是調用我們的整個應用程序的啟動文件 class Program{static void Main(string[] args){var host new WebHostBuilder().UseKestrel() // 指定WebServer為Kes…

windows XP系統下oracle完整卸載過程

NT環境下&#xff1a; 1、以NT的Administrator 登陸 2、通過控制面版-〉服務&#xff0c;停掉所有ORACLE服務 3、打開注冊表&#xff08;REGEDIT命令&#xff09;&#xff0c;刪除HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE 4、打開注冊表中的 HKEY_LOCAL_MACHINE\SYSTEM\CurrentCont…

Hadoop----hdfs的基本操作

2019獨角獸企業重金招聘Python工程師標準>>> HDFS操作文件的基本命令 1.創建文件夾 $>hdfs dfs -mkdir /user/centos/hadoop 2.展示目錄 $>hdfs dfs -ls -r /user/centos/hadoop 3.遞歸展示 $>hdfs dfs -lsr /user/centos/hadoop 4.上傳文件 $&g…

原生sql實現restful接口調用

index.php <?phpinclude ./Request.php; include ./Response.php; //獲取數據 $dataRequest::getRequest(); $resultResponse::sendResponse($data); echo $result; ?> Request.php <?php class Request{private static $method_typearray(get,post,put,delete,pa…

彈幕效果

<!DOCTYPE html><html> <head> <meta charset"UTF-8"> <title>彈幕效果</title> <script src"../../jquery-1.12.4.min.js" type"text/javascript" charset"utf-8"></script> </h…

基于.NetCore開發博客項目 StarBlog - (21) 開始開發RESTFul接口

1前言最近電腦壞了&#xff0c;開源項目的進度也受到一些影響這篇醞釀很久了&#xff0c;作為本系列第二部分&#xff08;API接口開發&#xff09;的第一篇&#xff0c;得想一個好的開頭&#xff0c;想著想著就鴿了好久&#xff0c;索性不扯那么多了&#xff0c;直接開寫吧~2關…

03 Oracle分區表

Oracle分區表 先說句題外話… 歡迎成都天府軟件園的小伙伴來面基交流經驗~ 一&#xff1a;什么是分區&#xff08;Partition&#xff09;&#xff1f; 分區是將一個表或索引物理地分解為多個更小、更可管理的部分。 分區對應用透明&#xff0c;即對訪問數據庫的應用而言&…

windows獲取本地時間_如何在Windows 8中重新獲得本地登錄

windows獲取本地時間By default a fresh Windows 8 installation prompts you to create a synchronized cloud-enabled login. While there are distinct perks to Microsoft’s live login system, sometimes you just want to keep things simple and local. Read on as we …

如何解決高并發,秒殺問題

相信不少人會被這個問題困擾&#xff0c;分享大家一篇這樣的文章&#xff0c;希望能夠幫到你&#xff01; 一、秒殺業務為什么難做&#xff1f;1&#xff09;im系統&#xff0c;例如qq或者微博&#xff0c;每個人都讀自己的數據&#xff08;好友列表、群列表、個人信息&#xf…

Spring原理之代理與動態代理模式總結(四)

2019獨角獸企業重金招聘Python工程師標準>>> 代理模式 1&#xff0c;什么是代理模式&#xff1f; 代理模式的作用是&#xff1a;為其他對象提供一種代理以控制對這個對象的訪問。2&#xff0c;代理模式有什么好處&#xff1f; 在某些情況下&#xff0c;一個客戶不…

可執行文件添加快捷方式_如何停止Windows向快捷方式文件名添加“-快捷方式”...

可執行文件添加快捷方式When you make a new shortcut in Windows, it automatically adds “- Shortcut” to the end of the shortcut’s file name. This doesn’t seem like a big deal, but they can be bothersome. Sure, you can remove the text yourself when you cre…

Red hat6.4重新安裝yum

今天在Red Hat上安裝軟件時&#xff0c;發現需要依賴軟件&#xff0c;然而在用yum指令時&#xff0c;出現了下面的錯誤&#xff1a; This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. 出現這個問題的原因是&…

使用 BenchmarkDotNet 比較指定容量的 List 的性能

我們之前提到 List 是 .NET 中常用的數據結構&#xff0c;其在存儲大量數據時&#xff0c;如果能夠指定它的初始化容量&#xff0c;就會有性能提升。這個優化的方法并不是很明顯&#xff0c;因此本文將使用 BenchmarkDotNet 庫&#xff0c;通過定量對比的方式來證明這一點。實驗…

看明星合影爭C位,學PPT中C位排版法

在娛樂圈里&#xff0c;C位是大咖位&#xff0c;是對藝人實力的最好證明&#xff0c;藝人們自然會想著去力爭C位&#xff0c;正所謂“不想當將軍的兵不是好兵&#xff0c;不想站C位的明星不是好明星”。那么&#xff0c;C位是什么意思&#xff1f;C位&#xff0c;網絡流行語&am…

javafx由淺到深的 認識(一)

javafx是一款比較新興的語言框架,隨著javafx越來越實用,估計許多程序員也會慢慢接觸它,故我在這里對它由淺到深進行介紹一下. 首先,要了解javafx,就應該先知道.xml文件的布局軟件,以往java都是通過敲代碼來進行布局的,但javafx有力新的突破,它實現了拖動方式,目前我使用的輔助軟…

linux用戶的根目錄_為什么Linux允許用戶刪除根目錄?

linux用戶的根目錄Most of the time, none of us willingly performs an action that will literally break our operating systems and force us to reinstall them. But what if such an action could easily occur even by accident on the user’s part? Today’s SuperUs…