Home Backend Development C#.Net Tutorial Introduction to sample code for C# Access database operations

Introduction to sample code for C# Access database operations

Mar 02, 2017 am 11:51 AM

C# Access database operation sample code introduction

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;


namespace AccessPractice
{
    public static class AccessHelper
    {
        public static bool Execute(string path,string sql) 
        {
            try
            {
                string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                //建立SQL查询
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句
                odCommand.CommandText = sql;


                odCommand.ExecuteNonQuery();
                odcConnection.Close();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }


        }


        public static DataTable ReadAllData(string tableName, string mdbPath,int topN, ref bool success)
        {
            DataTable dt = new DataTable();
            try
            {
                //1、建立连接
                string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath + ";";
                OleDbConnection odcConnection = new OleDbConnection(strConn);
                //2、打开连接
                odcConnection.Open();
                //建立SQL查询
                OleDbCommand odCommand = odcConnection.CreateCommand();
                //3、输入查询语句
                odCommand.CommandText = "select * from " + tableName;
                //建立读取
                OleDbDataReader odrReader = odCommand.ExecuteReader();
                //查询并显示数据
                int size = odrReader.FieldCount;
                for (int i = 0; i < size; i++)
                {
                    DataColumn dc;
                    dc = new DataColumn(odrReader.GetName(i));
                    dt.Columns.Add(dc);
                }
                DataRow dr;
                int count = 0;
                while (odrReader.Read())
                {
                    if (++count == topN) 
                    {
                        break;
                    }
                    dr = dt.NewRow();
                    for (int i = 0; i < size; i++)
                    {
                        dr[odrReader.GetName(i)] = odrReader[odrReader.GetName(i)].ToString();
                    }
                    dt.Rows.Add(dr);
                }
                //关闭连接
                odrReader.Close();
                odcConnection.Close();
                success = true;
                return dt;
            }
            catch
            {
                success = false;
                return dt;
            }
        }


    }
}
Copy after login


The above is the content of the C# Access database operation sample code introduction. For more related content, please pay attention to the PHP Chinese website ( www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

How to open xml format How to open xml format Apr 02, 2025 pm 09:00 PM

Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse.

What are the differences and connections between c and c#? What are the differences and connections between c and c#? Apr 03, 2025 pm 10:36 PM

Although C and C# have similarities, they are completely different: C is a process-oriented, manual memory management, and platform-dependent language used for system programming; C# is an object-oriented, garbage collection, and platform-independent language used for desktop, web application and game development.

C# multi-threaded method to prevent jamming C# multi-threaded method to prevent jamming Apr 03, 2025 pm 02:54 PM

The following ways to avoid "stuck" multithreading in C#: avoid time-consuming operations on UI threads. Use Task and async/await to perform time-consuming operations asynchronously. Update the UI on the UI thread via Application.Current.Dispatcher.Invoke. Use the CancellationToken to control task cancellation. Make rational use of thread pools to avoid excessive creation of threads. Pay attention to code readability and maintainability, making it easy to debug. Logs are recorded in each thread for easy debugging.

How to avoid third-party interfaces returning 403 errors in Node environment? How to avoid third-party interfaces returning 403 errors in Node environment? Apr 01, 2025 pm 02:03 PM

How to avoid the third-party interface returning 403 error in the Node environment. When calling the third-party website interface using Node.js, you sometimes encounter the problem of returning 403 error. �...

How to convert xml into word How to convert xml into word Apr 03, 2025 am 08:15 AM

There are three ways to convert XML to Word: use Microsoft Word, use an XML converter, or use a programming language.

Advanced C# .NET: Concurrency, Parallelism, and Multithreading Explained Advanced C# .NET: Concurrency, Parallelism, and Multithreading Explained Apr 03, 2025 am 12:01 AM

C#.NET provides powerful tools for concurrent, parallel and multithreaded programming. 1) Use the Thread class to create and manage threads, 2) The Task class provides more advanced abstraction, using thread pools to improve resource utilization, 3) implement parallel computing through Parallel.ForEach, 4) async/await and Task.WhenAll are used to obtain and process data in parallel, 5) avoid deadlocks, race conditions and thread leakage, 6) use thread pools and asynchronous programming to optimize performance.

How to change the format of xml How to change the format of xml Apr 03, 2025 am 08:42 AM

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages ​​such as Python. Be careful when modifying and back up the original files.

See all articles