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

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 夢在天涯 閱讀(1347) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1817668
  • 排名 - 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>
              日韩视频中文字幕| 免费一级欧美片在线播放| 久久免费偷拍视频| 国内精品视频在线观看| 久久中文字幕导航| 亚洲手机视频| 欧美福利精品| 先锋影音国产精品| 最新亚洲激情| 国产欧美一区二区精品仙草咪| 亚洲影音一区| 亚洲国产日韩欧美一区二区三区| aaa亚洲精品一二三区| 国产精品美女久久久久aⅴ国产馆| 午夜精品在线| 夜夜嗨av一区二区三区四区| 久久亚洲图片| 久久国产精品一区二区| 亚洲午夜国产成人av电影男同| 影音先锋亚洲精品| 狠狠色综合网| 伊人色综合久久天天五月婷| 国产精品揄拍一区二区| 欧美日韩性视频在线| 欧美激情中文字幕一区二区| 久久精品免费看| 久久成人精品无人区| 欧美在线视频一区二区| 国产精品成人播放| 欧美日韩在线看| 欧美丝袜一区二区| 国产精品日韩欧美一区二区三区| 欧美视频在线观看| 国产精品毛片a∨一区二区三区| 国产精品分类| 国产亚洲毛片| 亚洲国产精品嫩草影院| 亚洲人成在线观看| 亚洲网友自拍| 欧美在现视频| 欧美国产免费| 一区二区三区国产在线观看| 亚洲欧美综合v| 免费成人美女女| 国产日韩精品一区二区浪潮av| 狠狠色狠狠色综合| 一区二区三区波多野结衣在线观看| 午夜精品视频在线| 亚洲国产精品一区二区www在线| 夜夜嗨av一区二区三区中文字幕| 欧美一区二区日韩| 欧美午夜激情在线| 日韩亚洲在线观看| 欧美成人激情在线| 欧美一区精品| 国产日韩欧美一区二区| 欧美一区三区三区高中清蜜桃| 亚洲激情二区| 欧美 日韩 国产在线| 国产在线精品自拍| 欧美在线亚洲| 性久久久久久久久久久久| 欧美色图天堂网| 一区二区三区回区在观看免费视频| 玖玖国产精品视频| 伊人久久大香线蕉av超碰演员| 亚洲在线成人| 亚洲午夜一级| 国产精品久久久久永久免费观看| 亚洲裸体视频| 一区二区日韩欧美| 国产精品久久一区二区三区| 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲美女中文字幕| 国产精品视频yy9299一区| 亚洲中午字幕| 久久国产精品99国产| 亚洲国产精品激情在线观看| 亚洲欧洲综合另类| 欧美天天视频| 美女国产精品| 欧美系列亚洲系列| 久久国产乱子精品免费女| 久久久久久久久久久久久久一区| 亚洲大片av| 一区二区三区黄色| 尤物视频一区二区| 夜夜嗨av一区二区三区| 国内精品久久久久国产盗摄免费观看完整版| 久久久最新网址| 欧美日本在线| 欧美日韩综合在线免费观看| 久久先锋资源| 国产伦精品一区二区三区| 最新国产乱人伦偷精品免费网站 | 欧美日韩在线播放三区四区| 欧美一区综合| 欧美日韩国产大片| 亚洲电影网站| 亚洲高清免费视频| 久久久久国色av免费观看性色| 亚洲免费在线电影| 欧美精品三级日韩久久| 欧美国产日韩一区二区| 久久久一区二区三区| 国产日韩欧美在线观看| 中文日韩在线视频| 亚洲淫片在线视频| 国产精品白丝av嫩草影院| 亚洲国产精品传媒在线观看| 亚洲电影视频在线| 久久综合999| 亚洲人永久免费| 99re6这里只有精品视频在线观看 99re6这里只有精品 | 国产精品99久久久久久久vr| 欧美激情第五页| 在线亚洲免费| 久久精品国产999大香线蕉| 国产一区二区精品久久| 美女视频一区免费观看| 亚洲精品免费网站| 亚洲男人影院| 亚洲成色www久久网站| 欧美另类视频| 久久er99精品| 亚洲日产国产精品| 新67194成人永久网站| 激情久久久久久| 欧美四级在线观看| 久久久久国产精品一区二区| 亚洲第一二三四五区| 香蕉成人啪国产精品视频综合网| 一区二区三区无毛| 欧美日韩午夜精品| 久久综合色影院| 欧美一区日本一区韩国一区| 亚洲福利视频网站| 久久精品视频免费播放| 亚洲一区二区在| 日韩视频在线你懂得| 亚洲高清一区二区三区| 狠狠色伊人亚洲综合成人| 国产精品爱久久久久久久| 欧美大片专区| 免费在线成人| 一本色道**综合亚洲精品蜜桃冫| 国产视频精品免费播放| 国产精品成人在线| 欧美日韩国产电影| 欧美电影在线观看| 欧美欧美全黄| 欧美日韩xxxxx| 欧美日韩一区视频| 欧美精品高清视频| 欧美日韩一区二区三区在线 | 亚洲欧美在线aaa| 午夜精品福利视频| 欧美在线播放| 另类亚洲自拍| 欧美三级中文字幕在线观看| 欧美三级乱人伦电影| 国产精品久久久久天堂| 国产欧美日韩免费看aⅴ视频| 国产欧美 在线欧美| 在线观看视频一区二区| 亚洲人成网站999久久久综合| 一区二区三区四区精品| 欧美一区二区在线播放| 毛片精品免费在线观看| 亚洲美女精品成人在线视频| 中文久久精品| 可以免费看不卡的av网站| 欧美视频专区一二在线观看| 国产综合久久久久久| 亚洲精品资源| 欧美大片免费| 亚洲综合色自拍一区| 欧美高清在线一区二区| 国产日韩欧美在线观看| 亚洲一区二区三区四区视频| 麻豆精品一区二区综合av| 亚洲视频999| 欧美日本韩国| 日韩一区二区电影网| 麻豆国产精品一区二区三区 | 久久综合国产精品| 亚洲综合精品四区| 欧美在线中文字幕| 日韩亚洲精品电影| 免费美女久久99| 精品成人一区二区三区| 性感少妇一区| 亚洲欧美在线观看| 国产视频在线观看一区二区三区| 亚洲欧美国产日韩天堂区| 亚洲黄网站在线观看| 欧美国产精品劲爆| 一区二区三区日韩精品| 亚洲精品国产精品乱码不99按摩 | 欧美日韩中文字幕在线|