青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Simple ADO Database Read, Insert, Update and Delete using C#.

Introduction

Accessing databases is a common part of most applications and with the introduction of C# and ADO.NET, has become quite simple. This article will demonstrate the four most basic database operations. 

  • Reading data. This includes various data types such as integers, strings and dates.
  • Writing data. As with reading we will write these common types. This will be done using a SQL statement.
  • Updating or modifying data. Again we will use a simple SQL statement.
  • Deleting data. Using SQL.

These operations will be performed against a Microsoft Access 2000 database, however SQL or other ADO data sources could be used by simply changing the connection string. 

Getting started

To use the ADO classes we need to include the ADO.NET namespace and a few handy date classes. Add the following line of code to the file where you want to perform the database operation. It should appear below the namespace line and above the class definition.

  using System.Data;             // State variables
  using System.Data.ADO;         // Database
  using System.Globalization;    // Date

Depending on the type of project you are working with, you may need to add a reference to the System.Data namespace. You will know this if the compiler errors on the code you just added. To add the System.Data namespace;

  • Right click on the Solution explorer - References branch.
  • Select Add reference.
  • Select the .NET Framework tab.
  • Double click on the System.data.dll entry.
  • Select OK.
  • System.Data should now appear in the References list of the Solution explorer.

The connection string is used during most operations, so I would recommend you make it a member of the class you will be working in. Note: In your application the path to the database file would be something else.

//Attributes
 public const string DB_CONN_STRING =
   "Driver={Microsoft Access Driver (*.mdb)}; "+
   "DBQ=D:\\CS\\TestDbReadWrite\\SimpleTest.mdb";

Reading data

Now things get interesting. Reading is done using the ADODataReader class. (See Chris Maunder's article The ADO.NET ADODataReader class for more info on this class. ) The steps to perform the read are;

  • We open the database with an ADOConnection.
        ADOConnection conn =
                   new ADOConnection(DB_CONN_STRING);
        conn.Open();
  • We create a SQL statement to define the data to be retrieved. This command is executed to return an ADODataReader object. Note the out keyword in the Execute method. This is C# talk for pass by reference.
        ADODataReader dr;
        ADOCommand cmd =
           new ADOCommand( "SELECT * FROM Person", conn );
        cmd.Execute( out dr);
  • We loop through each record in the ADODataReader until we are done. Note: The data is returned directly as a string and the field name is used to indicate the field to read.
        while( dr.Read() )
        {
            System.Console.WriteLine( dr["FirstName"] );
        }
  • We clean up.  

However, as good programmers we would have also wrapped the lot in a try/catch/finally to ensure we handled anything bad.

 try
 {
     .... the database operations ...
 }
 catch( Exception ex )
 {
   System.Console.WriteLine( "READING:" );
   System.Console.WriteLine( "  ERROR:" + ex.Message );
   System.Console.WriteLine( "  SQL  :" + sSqlCmd );
   System.Console.WriteLine( "  Conn.:" + DB_CONN_STRING );
 }
 finally
 {
   // Close the connection
   if( conn.State == DBObjectState.Open )
     conn.Close();
 }

Reading different data types

The dr["stuff"] is usually able to return a string of some sort. However to get an int or DateTime object it is often necessary to cast the data. This is usually done with a simple case or using one of ADODataReader's many build in conversions. ie

 int nOrdinalAge = dr.GetOrdinal( "Age" );
 int nAge = dr.GetInt32( nOrdinalAge );

 DateTime tUpdated = (DateTime)dr["Updated"];

Note the use of GetOrdinal to locate the field to read by name. If the field is blank (not been populated yet), the above code will throw an exception. To catch this condition we check if data exists with the IsNull method as follows.

 int nOrdinalAge = dr.GetOrdinal( "Age" );
 if( dr.IsNull( nOrdinalAge ) )
 {
   System.Console.WriteLine( " Age  : Not given!" );
 }
 else
 {
   int nAge = dr.GetInt32( nOrdinalAge );
   System.Console.WriteLine( " Age  : " + nAge );
 }

Insert, Modify, Delete and other SQL commands

Inserting, Modifying and Deleting can very simply be done using SQL statements. The following code performs a SQL command to insert a record.

 // SQL command
 String sSQLCommand =
 "INSERT INTO Person (Age, FirstName, Description, Updated)  " +
 "VALUES( 55, 'Bob', 'Is a Penguin', '2001/12/25 20:30:15' );";
 // Create the command object
 ADOCommand cmdAdder = new ADOCommand(
     sSQLCommand,
     DB_CONN_STRING);
 cmdAdder.ActiveConnection.Open();
 // Execute the SQL command
 int nNoAdded = cmdAdder.ExecuteNonQuery();
 System.Console.WriteLine(
 "\nRow(s) Added = " + nNoAdded + "\n" );

Note: The try/catch was not shown in the above example but should wrap the above code.

Inserting

The above code inserted a record by building a SQL command which was later executed. Some things to note in the formatting of the command are;

  • Numerical values are presented directly. No single quotes (').
  • Strings are presented wrapped in single quotes ('blah').
  • Be sure the strings do not include any embedded single or double quotes. This will upset things.
  • Date and times are presented wrapped in single quotes in international format ('YYYYY/MM/DD HH:MM:SS').

Modifying

The UPDATE command indicates the records to be modified and the modification to be made. The return value of the ExecuteNonQuery() indicates the number of records changes so this would return 5 if there were 5 Peter's in the table. 

 String sSQLCommand =
   "UPDATE Person SET Age = 27 WHERE FirstName = 'Peter'";

Deleting

The DELETE command indicates the records to be deleted. This could be several several records. The return value of the ExecuteNonQuery() indicates the number of records changes so this would return 2 if there were 2 Bobo in the table. Both Bobo's would be deleted.

 String sSQLCommand =
   "DELETE FROM Person WHERE FirstName = 'Bobo'";

About the sample code

The sample is a simple console application that perform each of the database operations on a provided Microsoft Access database. To build it, open the TestDbReadWrite.csproj file as a project in the Visual Studio.NET IDE. Change the DB_CONN_STRING variable in MainConsole.cs to point to the SimpleTest.mdb. Build it and away you go.

Conclusion

Now you should be able to perform the basic database operation in C#, get out there and cut some code. Take the time to learn SQL. Also read articles on the why and how this works. If you get really bored check out my site at www.mctainsh.com for more updates on simple coding.

Downloads

posted on 2005-11-21 12:20 夢在天涯 閱讀(1337) 評論(1)  編輯 收藏 引用 所屬分類: C#/.NET

評論

# re: Simple ADO Database Read, Insert, Update and Delete using C#. 2014-05-12 21:28 Heemanshu Bhalla

Nice tutorial

there is also a nice link here it has also provided snapshots with step by step and demo app to download

http://geeksprogrammings.blogspot.com/2014/05/sql-insert-update-delete-using-c.html  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811981
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              久久精品72免费观看| 欧美国产日韩在线| 亚洲国产精品视频一区| 在线观看日韩精品| 亚洲国产日韩一区二区| 99亚洲伊人久久精品影院红桃| 亚洲国产精品久久久久秋霞蜜臀 | 欧美日韩小视频| 欧美手机在线| 国内成+人亚洲+欧美+综合在线| 国产一区香蕉久久| 亚洲精选中文字幕| 欧美在线日韩| 最新国产成人av网站网址麻豆| 欧美日韩免费高清一区色橹橹| 美日韩在线观看| 国产一区再线| 在线视频你懂得一区 | 国产精品一卡| 亚洲日本va在线观看| 亚洲理论在线观看| 亚洲日本va午夜在线影院| 亚洲欧美一区二区视频| 欧美精品国产一区二区| 韩国欧美国产1区| 欧美自拍丝袜亚洲| 亚洲少妇一区| 国产精品亚洲精品| 性欧美xxxx大乳国产app| 一区二区三区 在线观看视| 久久综合色一综合色88| 在线播放国产一区中文字幕剧情欧美| 午夜日韩在线| 欧美怡红院视频| 国产一区二区三区高清播放| 欧美一区二区三区视频在线观看| 日韩午夜在线视频| 国产精品系列在线播放| 久久久久.com| 欧美阿v一级看视频| 亚洲天堂av图片| 香蕉成人伊视频在线观看 | 久久精品91| 国产精品久久久久aaaa| 亚洲天堂第二页| 午夜一区二区三区不卡视频| 国产精品视频xxxx| 欧美刺激性大交免费视频| 老牛国产精品一区的观看方式| 日韩视频免费在线观看| 亚洲一区二区三区乱码aⅴ| 狠狠色伊人亚洲综合网站色| 亚洲高清电影| 国产日韩欧美精品综合| 亚洲欧洲日本一区二区三区| 国产美女高潮久久白浆| 亚洲国产日韩美| 国产亚洲女人久久久久毛片| 亚洲精品一区二区三区樱花 | 亚洲婷婷在线| 亚洲国产高清自拍| 先锋亚洲精品| 亚洲欧美日韩一区二区三区在线| 久久综合电影| 久久综合狠狠综合久久综合88| 美女任你摸久久| 国产精品亚洲综合久久| 一本大道久久a久久综合婷婷| 日韩午夜在线观看视频| 美女主播精品视频一二三四| 麻豆成人av| 亚洲成色777777女色窝| 久久天天躁狠狠躁夜夜av| 久久久久久久久久看片| 国内精品久久久久影院色| 久久成人久久爱| 亚洲二区三区四区| 91久久精品一区二区别| 欧美日韩日本网| 亚洲欧美日韩在线观看a三区| 久久狠狠一本精品综合网| 黄色在线一区| 国产精品久久999| 久久人人爽国产| 一区二区日韩免费看| 久久久久国产精品厨房| 日韩视频一区二区三区在线播放| 欧美视频中文字幕在线| 久久久久国产精品人| 亚洲视频一二| 亚洲高清视频一区| 另类专区欧美制服同性| 亚洲调教视频在线观看| 亚洲黄色在线| 国产欧美日韩精品一区| 欧美精品手机在线| 久久久久久夜| 午夜久久福利| 亚洲综合好骚| 国产精品日韩精品| 亚洲一二三四区| 久久黄色影院| 香蕉成人久久| 激情婷婷欧美| 欧美日韩一本到| 久久视频在线看| 久久久亚洲一区| 欧美影视一区| 欧美在线国产精品| 欧美在线视频观看| 亚洲在线免费观看| 亚洲一二三四区| 翔田千里一区二区| 久久狠狠一本精品综合网| 亚洲午夜国产成人av电影男同| 一区电影在线观看| 亚洲专区在线视频| 久久狠狠婷婷| 欧美久久99| 国产日韩成人精品| 影音先锋国产精品| 亚洲男女毛片无遮挡| 欧美一区二区三区婷婷月色| 久久综合婷婷| 亚洲人成7777| 亚洲一区二区三区在线观看视频| 性视频1819p久久| 欧美精品午夜| 国产综合久久久久久鬼色| **网站欧美大片在线观看| 日韩亚洲视频| 久久久青草青青国产亚洲免观| 亚洲激情国产精品| 久久精品99国产精品| 国产精品jvid在线观看蜜臀| 亚洲高清一区二区三区| 亚洲欧美伊人| 一区二区三区蜜桃网| 麻豆国产精品一区二区三区| 国产精品入口| 亚洲欧美国产精品专区久久| 亚洲大片av| 老司机精品福利视频| 激情综合中文娱乐网| 午夜在线一区二区| 亚洲欧美日韩国产另类专区| 亚洲国产精品黑人久久久| 欧美影视一区| 国产字幕视频一区二区| 久久精品国产亚洲精品| 亚洲在线黄色| 尤物九九久久国产精品的分类| 久久久久久亚洲精品杨幂换脸| 欧美在线一区二区三区| 国产一区二区三区四区| 亚洲高清视频中文字幕| 欧美人妖在线观看| 亚洲欧美一区二区激情| 午夜一区在线| 亚洲伦理自拍| 亚洲午夜免费视频| 国产在线欧美| 国产精品激情电影| 欧美jizzhd精品欧美巨大免费| 欧美电影免费| 久久久久久网| 欧美日韩蜜桃| 久久久欧美一区二区| 欧美人与性禽动交情品| 久久视频在线视频| 国产精品免费看片| 欧美视频在线观看免费网址| 久久青草欧美一区二区三区| 国产精品v片在线观看不卡| 欧美成人四级电影| 精久久久久久| 欧美一区免费视频| 欧美主播一区二区三区美女 久久精品人 | 欧美激情综合色综合啪啪| 美国成人毛片| 国产欧美激情| 亚洲欧美国产高清| 午夜老司机精品| 欧美激情在线狂野欧美精品| 欧美日韩一本到| 一区二区高清在线| 欧美mv日韩mv国产网站| 久久精品一本| 在线观看久久av| 欧美大片专区| 亚洲深夜福利网站| 久久久久久久一区| 亚洲第一页中文字幕| 欧美国产高清| 一区二区三区免费观看| 久久免费国产精品1| 狠狠色香婷婷久久亚洲精品| 可以看av的网站久久看| 亚洲毛片视频| 久久精品国产77777蜜臀|