DataTable修改后,如何更新数据库
DataTable中的数据可以修改多个后,同时更新到数据库,这样比较方便。下面是实现的代码: DataTable table = new DataTable();//初始化一个DataTable对象 string sqlConnectionString = Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;
DataTable中的数据可以修改多个后,同时更新到数据库,这样比较方便。下面是实现的代码:
<span> DataTable table = new DataTable();//初始化一个DataTable对象 string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//连接数据库 SqlCommand sqlCommand = new SqlCommand("select * from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//这句挺重要的,好像是批量更新的时候用 //DataSet dtst = new DataSet(); 用dataSet也可以完成,下面有对应实现的代码 sqlConnection.Open(); sqlAdap.Fill(table); //table = dtst.Tables["testB"]; DataRow dr = table.Rows[0];//第一行 //dr.BeginEdit(); //dr.EndEdit(); 貌似这两句主要是数据绑定到控件的时候才用 for (int i = 0; i </span>Copy after login
下面是DataTable与dataGridView 控件绑定,当dataGridView中的数据改变时,更新数据库的代码:
DataTable和dataGridView控件绑定代码:
<span>private void Form1_Load(object sender, EventArgs e) { string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//初始化sqlconnection DataTable table = new DataTable(); SqlDataAdapter sqlAdap = new SqlDataAdapter("select * from testB ", sqlConnection); sqlConnection.Open(); sqlAdap.Fill(table); sqlConnection.Close(); //将testB表中的数据显示在dataGridView中 this.dataGridView1.DataSource = table; }</span>
实现将dataGridView中的改动更新到数据库代码:
<span>private void butUpdate_Click(object sender, EventArgs e) { DataTable table = new DataTable(); table = (DataTable)this.dataGridView1.DataSource; string sqlConnectionString = "Data Source=WangHaitao-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); SqlCommand sqlCommand = new SqlCommand("select * from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//必须有 sqlConnection.Open(); sqlAdap.Fill(table); //testB表中必须存在主键,否则无法更新 sqlAdap.Update(table); sqlConnection.Close(); MessageBox.Show("aa"); }</span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

1. First open DingTalk. 2. Open the group chat and click the three dots in the upper right corner. 3. Find my nickname in this group. 4. Click to enter to modify and save.

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

Windows updates may cause some of the following problems: 1. Compatibility issues: Some applications, drivers, or hardware devices may be incompatible with new Windows updates, causing them to not work properly or crash. 2. Performance issues: Sometimes, Windows updates may cause the system to become slower or experience performance degradation. This may be due to new features or improvements requiring more resources to run. 3. System stability issues: Some users reported that after installing Windows updates, the system may experience unexpected crashes or blue screen errors. 4. Data loss: In rare cases, Windows updates may cause data loss or file corruption. This is why before making any important updates, back up your

When publishing products on the Xianyu platform, users can customize the geographical location information of the product according to the actual situation, so that potential buyers can more accurately grasp the specific location of the product. Once the product is successfully put on the shelves, there is no need to worry if the seller's location changes. The Xianyu platform provides a flexible and convenient modification function. So when we want to modify the address of a published product, how do we modify it? This tutorial guide will provide you with a detailed step-by-step guide. I hope it can help. Everyone! How to modify the release product address in Xianyu? 1. Open Xianyu, click on what I published, select the product, and click Edit. 2. Click the positioning icon and select the address you want to set.

According to news from this site on May 3, MSI today released the AMDAM4AGESA1.2.0.Ca firmware update, which fixes the Zenbleed security vulnerability in the AMD Ryzen4000 series Zen2 APU. The firmware update released by MSI this time is suitable for almost all X570 motherboards. It mainly fixes CVE-2023-20593 for Zen2 processors, which AMD classifies as a medium threat. Note from this site: The vulnerability tracking number is CVE-2023-20593, which can steal confidential data at a speed of 30KB per core per second. This attack affects all software running on the CPU, including virtual machines, sandboxes, containers and processes. Although the purpose of AGESA1.2.0.Ca

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.
