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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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 夢(mèng)在天涯 閱讀(1349) 評(píng)論(1)  編輯 收藏 引用 所屬分類(lèi): C#/.NET

評(píng)論

# 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  回復(fù)  更多評(píng)論   

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類(lèi)

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1819132
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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精品欧美一区二区三区综合在线| 亚洲自拍三区| 亚洲国产岛国毛片在线| 国产精品jizz在线观看美国| 欧美在线观看你懂的| 亚洲国产日韩欧美在线图片| 亚洲欧美日韩直播| 一区在线影院| 欧美日韩另类在线| 久久久九九九九| 99在线精品视频| 久久综合久久综合久久| 亚洲视频碰碰| 亚洲国产精品久久久| 国产精品久久久99| 免费一级欧美片在线播放| 亚洲伊人观看| 亚洲国产精品ⅴa在线观看 | 亚洲性夜色噜噜噜7777| 免费毛片一区二区三区久久久| 亚洲一区二区三区视频播放| 亚洲高清网站| 国产欧美69| 欧美日韩国产成人在线91| 欧美一区日韩一区| 在线视频日本亚洲性| 欧美激情女人20p| 久久九九全国免费精品观看| 亚洲一区二区精品在线| 1024亚洲| 在线电影一区| 国产情人综合久久777777| 欧美日韩视频第一区| 欧美成人免费大片| 久久久久9999亚洲精品| 亚洲免费婷婷| 一区二区三区成人| 日韩午夜高潮| 亚洲精品日韩精品| 亚洲黄一区二区三区| 欧美国产极速在线| 免费91麻豆精品国产自产在线观看| 香蕉久久夜色精品| 亚洲欧美国产一区二区三区| 中日韩高清电影网| 日韩视频精品在线观看| 亚洲国产精品999| 伊伊综合在线| 国内精品嫩模av私拍在线观看| 国产精品永久免费在线| 国产精品午夜av在线| 欧美亚洲不卡| 国产精品久久久久9999吃药| 国产精品国产三级国产专播品爱网| 欧美女激情福利| 欧美激情在线免费观看| 欧美极品aⅴ影院| 欧美精品免费在线观看| 欧美—级高清免费播放| 欧美日韩三区四区| 国产精品成人一区| 国产精品手机在线| 国产色爱av资源综合区| 韩日视频一区| 亚洲国内高清视频| 亚洲免费观看在线观看| 国产精品99久久久久久久久 | 先锋影音网一区二区| 亚洲欧洲av一区二区三区久久| 西瓜成人精品人成网站| 久久激情视频免费观看| 久久综合久久综合九色| 欧美激情在线有限公司| 亚洲每日在线| 午夜视频精品| 麻豆久久精品| 欧美日韩成人精品| 国产欧美日韩免费| 亚洲国产精品久久久久秋霞不卡 | 亚洲一区二区精品在线| 午夜天堂精品久久久久| 久久久噜噜噜久久人人看| 欧美成人资源网| 99精品欧美一区二区三区| 午夜电影亚洲| 久久综合久久久久88| 欧美日本一区二区三区| 国产精品家教| 在线观看国产成人av片| 一区二区三区av| 久久gogo国模啪啪人体图| 久久躁狠狠躁夜夜爽| 亚洲精品综合精品自拍| 午夜免费电影一区在线观看| 蜜臀久久99精品久久久久久9| 欧美日韩综合网| 国产亚洲精品久久久久婷婷瑜伽| 亚洲欧洲一区二区三区| 午夜精品短视频| 欧美成人久久| 亚洲免费视频网站| 欧美激情在线免费观看| 国产一区三区三区| 亚洲一级在线观看| 欧美国产日本在线| 校园激情久久| 欧美视频在线观看免费网址| 在线免费观看日本欧美| 亚洲一区免费| 欧美国产综合一区二区| 欧美伊人久久久久久久久影院| 欧美久久一区| 在线精品一区二区| 欧美永久精品| 一区二区三区四区五区视频 | 欧美区一区二区三区| 影音先锋国产精品| 欧美亚洲网站| 亚洲人成在线播放网站岛国| 久久精品视频播放| 国产精品免费aⅴ片在线观看| 亚洲日本精品国产第一区| 久久精品人人| 亚洲午夜高清视频| 欧美精品一区二区三区在线看午夜| 国内精品久久久久久| 亚洲欧美国产77777| 日韩网站免费观看| 欧美精品日韩精品| 亚洲精品网站在线播放gif| 久久精品亚洲一区二区| 亚洲视频图片小说| 欧美视频一区二区| 一区二区欧美精品| 91久久久久久久久久久久久| 久久亚洲一区二区三区四区| 国色天香一区二区| 久久久亚洲人| 欧美中文字幕视频| 国内久久精品| 久久久久久自在自线| 午夜欧美不卡精品aaaaa| 国产乱码精品一区二区三区忘忧草 | 女女同性精品视频| 久久久中精品2020中文| 黄色一区二区在线| 麻豆精品精华液| 久久久免费观看视频| 1769国内精品视频在线播放| 麻豆精品精品国产自在97香蕉| 欧美一区91| 有码中文亚洲精品| 免费成人在线视频网站| 快射av在线播放一区| 亚洲激情av| 亚洲精品免费观看| 欧美日韩在线视频一区| 亚洲一区观看| 新狼窝色av性久久久久久| 国产主播喷水一区二区| 免费不卡在线观看| 欧美国产精品v| 亚洲永久免费av| 亚洲尤物在线视频观看| 国产亚洲人成网站在线观看| 六月丁香综合| 欧美激情精品| 午夜在线观看欧美| 久久国产一区二区| 最新国产成人在线观看| 99re视频这里只有精品| 国产精品久久久久久久久久ktv| 久久成人精品一区二区三区| 久久久噜噜噜| 一区二区免费在线视频| 亚洲欧美日韩国产另类专区| 一区二区亚洲精品| 亚洲精品小视频| 国产一区二区三区黄| 亚洲精华国产欧美| 国产欧美日韩一区| 亚洲第一区在线观看| 国产精品一二一区| 亚洲国产1区| 国产亚洲毛片在线|