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

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>
              欧美激情亚洲另类| 激情综合色综合久久| 亚洲一区国产一区| 亚洲毛片网站| 99在线|亚洲一区二区| 在线亚洲国产精品网站| 亚洲综合色噜噜狠狠| 西西裸体人体做爰大胆久久久| 亚洲欧美在线aaa| 久久精品国产精品亚洲精品| 久久久久久久国产| 欧美+亚洲+精品+三区| 欧美日韩成人一区| 国产精品影音先锋| 国内外成人在线视频| 亚洲国产导航| 亚洲男人影院| 欧美成人免费在线观看| 日韩网站在线| 久久精品欧美日韩| 欧美三级乱码| 伊人精品在线| 亚洲综合精品四区| 女人天堂亚洲aⅴ在线观看| 亚洲精品你懂的| 在线视频精品一区| 久久伊人亚洲| 国产精品久久二区二区| 国产综合香蕉五月婷在线| 亚洲麻豆国产自偷在线| 久久精品1区| 亚洲日本成人在线观看| 亚洲综合欧美| 欧美电影在线观看完整版| 国产精品美女在线观看| 亚洲精品孕妇| 欧美77777| 欧美一区永久视频免费观看| 欧美国产国产综合| 影音先锋中文字幕一区| 欧美亚洲一区二区在线| 亚洲人体1000| 欧美 日韩 国产在线| 国产一区二区三区视频在线观看| 亚洲精品久久久久中文字幕欢迎你| 香蕉久久夜色精品国产| 最新日韩在线| 久久综合久久综合久久综合| 亚洲激情啪啪| 美女黄网久久| 在线日韩欧美视频| 久久久久高清| 欧美在线999| 狠狠入ady亚洲精品| 久久久www免费人成黑人精品| 中文日韩在线| 国产精品老牛| 午夜一区二区三区在线观看| 日韩亚洲在线观看| 欧美日韩亚洲一区二区| 一区二区三区四区精品| 亚洲人成绝费网站色www| 欧美风情在线观看| 亚洲精品久久| 亚洲美女av黄| 欧美新色视频| 午夜免费日韩视频| 香蕉久久夜色精品| 激情小说亚洲一区| 亚洲电影观看| 欧美三级午夜理伦三级中文幕| 亚洲视频狠狠| 亚洲自拍另类| 韩国精品久久久999| 噜噜噜91成人网| 欧美激情小视频| 亚洲在线观看免费视频| 亚洲欧美日韩综合| 在线观看的日韩av| 91久久精品一区二区三区| 欧美日韩国产成人在线观看| 亚洲图片在区色| 亚洲欧美日韩国产一区二区三区| 国产日韩一级二级三级| 免费观看成人www动漫视频| 欧美激情一二区| 欧美一进一出视频| 免费在线一区二区| 午夜精品剧场| 蜜臀久久99精品久久久画质超高清| 一本久久青青| 亚洲欧美综合国产精品一区| 在线观看视频日韩| 9国产精品视频| 一区精品久久| 一区二区三区欧美在线观看| 国内精品伊人久久久久av影院| 亚洲黄页一区| 国产偷久久久精品专区| 亚洲国产裸拍裸体视频在线观看乱了中文| 欧美体内she精视频| 免费精品视频| 国产视频一区在线观看一区免费| 亚洲电影视频在线| 国内视频精品| 亚洲一区在线免费| 亚洲精品欧美极品| 欧美综合国产精品久久丁香| 亚洲最新在线视频| 久久久免费精品| 午夜影院日韩| 亚洲日本成人| 在线观看欧美| 亚洲欧美综合另类中字| 99热在这里有精品免费| 久久久999国产| 先锋影音国产精品| 欧美日韩中文字幕在线视频| 欧美大片免费看| 国产亚洲福利社区一区| 一本色道久久综合亚洲精品不卡| 亚洲激情视频| 久久免费少妇高潮久久精品99| 午夜精品免费视频| 国产精品超碰97尤物18| 亚洲精品乱码久久久久久按摩观| 黄色综合网站| 久久九九免费视频| 久久免费视频这里只有精品| 国产麻豆日韩欧美久久| 亚洲图色在线| 午夜精品亚洲一区二区三区嫩草| 欧美日韩精品在线观看| 欧美国产综合| 亚洲人成亚洲人成在线观看图片| 久久久久久尹人网香蕉| 久久影院午夜论| 国内外成人在线视频| 欧美怡红院视频一区二区三区| 亚洲欧美日韩精品一区二区| 欧美视频一区二区三区| 亚洲欧洲一区二区三区| 一区二区三区欧美在线| 欧美三区在线观看| 亚洲视频免费在线| 久久福利影视| …久久精品99久久香蕉国产 | 香港久久久电影| 欧美一区二区三区在| 国产日本欧美一区二区| 欧美一区二区黄色| 蜜臀av性久久久久蜜臀aⅴ四虎| 韩日欧美一区| 久久蜜桃资源一区二区老牛| 欧美 亚欧 日韩视频在线| 亚洲国产精品成人综合| 欧美精品久久久久久久| 正在播放亚洲一区| 久久精品中文字幕免费mv| 1769国内精品视频在线播放| 免费久久99精品国产自在现线| 亚洲国产一区二区三区a毛片| 欧美超级免费视 在线| 亚洲国产美女精品久久久久∴| 夜夜嗨av一区二区三区网页| 欧美性大战久久久久久久| 午夜精品视频网站| 欧美国产在线视频| 亚洲午夜一区| 国模精品娜娜一二三区| 欧美搞黄网站| 午夜在线观看免费一区| 亚洲第一偷拍| 久久精品国产亚洲高清剧情介绍 | 国产麻豆精品在线观看| 狼人天天伊人久久| 亚洲图色在线| 伊人精品在线| 欧美视频中文字幕在线| 欧美在线不卡视频| 99在线精品视频在线观看| 久久久夜色精品亚洲| 亚洲一区二区三区精品视频| 精品99视频| 国产农村妇女精品一区二区| 麻豆精品传媒视频| 亚洲欧美久久| 激情亚洲成人| 国产精品亚洲а∨天堂免在线| 免费亚洲视频| 久久国产精品久久久久久电车| 99精品视频免费在线观看| 毛片av中文字幕一区二区| 亚洲欧美日韩精品在线| 一区二区三区久久久| 亚洲国产综合在线看不卡| 国产一区在线视频| 国产精品一区在线播放| 欧美三级中文字幕在线观看| 欧美国产国产综合|