Visual C# 2005 또는 Visual C# 사용 방법NET는 DataSet 객체를 통해 데이터베이스를 업데이트합니다.
13515 단어 .net
DataSet, 이 데이터를 수정하는 방법, 그리고 데이터베이스로 다시 보내서 원본 데이터 원본을 업데이트하는 방법.
DataSet 객체는 Microsoft입니다.NET Framework에서 데이터 액세스의 주요 부분은 테이블, 뷰 및 관계를 저장할 수 있는 메모리의 객체입니다.
정상으로 돌아가다
요구 사항
//
loadTOCNode(2, 'summary');
//다음 표에는 권장되는 하드웨어, 소프트웨어, 네트워크 인프라 및 필요한 서비스 팩이 정리되어 있습니다.
이 문서에서는 다음 항목에 대해 잘 알고 있다고 가정합니다.
DataSet 객체를 통해 데이터베이스를 업데이트하는 방법
//
loadTOCNode(2, 'summary');
//이 절에서는 어떻게 사용하는지 보여 준다
DataSet 객체는 데이터베이스의 데이터를 업데이트합니다.사용 가능
SqlCommand 객체는 데이터베이스에 데이터를 직접 삽입, 업데이트 및 삭제하는 것이 중요합니다.
이 문서를 더 잘 이해하려면 아래 기사 번호를 클릭하여 Microsoft Knowledgebase의 해당 기사를 확인하십시오.
314145
(http://support.microsoft.com/kb/314145/) Visual C# 사용 방법NET가 데이터베이스에서 DataSet 객체 채우기
기사
314145
(http://support.microsoft.com/kb/314145/)에서 다루는 몇 가지 주제는 데이터베이스에서 데이터를 검색하여 전송하는 방법
DataSet에서
DataSet이 데이터베이스와 차별화되는 방법 및 방법
로드
DataSet 을 사용하면 데이터를 수정할 수 있습니다.
DataSet은 이러한 변경 사항을 추적합니다.... 할 만하다
DataSet 객체는 데이터베이스에서 검색되어 메모리에 캐시된 데이터로 간주됩니다.
DataSet 객체는 테이블, 관계식 및 구속조건을 포함하는 컬렉션으로 구성됩니다.
업데이트하기
DataSet 업데이트 내용을 데이터베이스로 다시 보내려면 다음 절차를 따르십시오.
using System;
using System.Data;
using System.Data.SqlClient;
string sConnectionString;
// Modify the following string to correctly connect to your SQL Server.
sConnectionString = "Password=<strong password>;User ID=<username>;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)";
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();
// Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn);
// Create an instance of a DataSet, and retrieve data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
다음 코드를 단계 4의 코드 뒤에 붙여넣습니다.
//****************
// BEGIN ADD CODE
// Create a new instance of a DataTable.
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Authors"];
DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow();
// Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1;
// Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent);
Console.WriteLine("Add was successful, Click any key to continue!!");
Console.ReadLine();
// END ADD CODE
//*****************
// BEGIN EDIT CODE
drCurrent = tblAuthors.Rows.Find("213-46-8915");
drCurrent.BeginEdit();
drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
drCurrent.EndEdit();
Console.WriteLine("Record edited successfully, Click any key to continue!!");
Console.ReadLine();
// END EDIT CODE
//*****************
// BEGIN SEND CHANGES TO SQL SERVER
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
// END SEND CHANGES TO SQL SERVER
//*****************
//BEGIN DELETE CODE
drCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();
Console.WriteLine("Record deleted successfully, Click any key to continue!!");
Console.ReadLine();
//END DELETE CODE
//*****************
// CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
정상으로 돌아가다
전체 코드 목록
//
loadTOCNode(2, 'summary');
//
using System;
using System.Data;
using System.Data.SqlClient;
namespace PopulateDataSet
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
string sConnectionString;
// Modify the following string to correctly connect to your SQL Server.
sConnectionString = "Password=;User ID=sa;"
+ "Initial Catalog=pubs;"
+ "Data Source=(local)";
SqlConnection objConn
= new SqlConnection(sConnectionString);
objConn.Open();
// Create an instance of a DataAdapter.
SqlDataAdapter daAuthors
= new SqlDataAdapter("Select * From Authors", objConn);
// Create an instance of a DataSet, and retrieve
// data from the Authors table.
DataSet dsPubs = new DataSet("Pubs");
daAuthors.FillSchema(dsPubs,SchemaType.Source, "Authors");
daAuthors.Fill(dsPubs,"Authors");
//****************
// BEGIN ADD CODE
// Create a new instance of a DataTable.
DataTable tblAuthors;
tblAuthors = dsPubs.Tables["Authors"];
DataRow drCurrent;
// Obtain a new DataRow object from the DataTable.
drCurrent = tblAuthors.NewRow();
// Set the DataRow field values as necessary.
drCurrent["au_id"] = "993-21-3427";
drCurrent["au_fname"] = "George";
drCurrent["au_lname"] = "Johnson";
drCurrent["phone"] = "800 226-0752";
drCurrent["address"] = "1956 Arlington Pl.";
drCurrent["city"] = "Winnipeg";
drCurrent["state"] = "MB";
drCurrent["contract"] = 1;
// Pass that new object into the Add method of the DataTable.
tblAuthors.Rows.Add(drCurrent);
Console.WriteLine("Add was successful, Click any key to continue!!");
Console.ReadLine();
// END ADD CODE
//*****************
// BEGIN EDIT CODE
drCurrent = tblAuthors.Rows.Find("213-46-8915");
drCurrent.BeginEdit();
drCurrent["phone"] = "342" + drCurrent["phone"].ToString().Substring(3);
drCurrent.EndEdit();
Console.WriteLine("Record edited successfully, Click any key to continue!!");
Console.ReadLine();
// END EDIT CODE
//*****************
// BEGIN SEND CHANGES TO SQL SERVER
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors);
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
// END SEND CHANGES TO SQL SERVER
//*****************
//BEGIN DELETE CODE
drCurrent = tblAuthors.Rows.Find("993-21-3427");
drCurrent.Delete();
Console.WriteLine("SRecord deleted successfully, Click any key to continue!!");
Console.ReadLine();
//END DELETE CODE
//*****************
// CLEAN UP SQL SERVER
daAuthors.Update(dsPubs, "Authors");
Console.WriteLine("SQL Server updated successfully, Check Server explorer to see changes");
Console.ReadLine();
}
}
}
정상으로 돌아가다
참고 자료
ADO를 사용하는 방법NET, DataSet 대상과 SQL에 대한 더 많은 정보는 아래의 Microsoft 사이트를 방문하십시오: 데이터 접근 (MSDN의 소리 칼럼) http:...
//
loadTOCNode(1, 'references');
//
ADO를 사용하는 방법NET、
DataSet 객체 및 SQL에 대한 자세한 내용은 다음 Microsoft 웹 사이트를 참조하십시오.
데이터 액세스 심층 분석(
MSDN 사운드 칼럼)
http://msdn.microsoft.com/columns/
(http://msdn.microsoft.com/columns/)
ADO 프로그래머를 위한 ADOONET
http://msdn.microsoft.com/library/default.asp?url=/library/zh-cn/dndotnet/html/adonetprogmsdn.asp
(http://msdn.microsoft.com/library/default.asp?url=/library/zh-cn/dndotnet/html/adonetprogmsdn.asp)
MSDN Online .NET 개발자 센터
http://msdn2.microsoft.com/zh-cn/netframework/default.aspx
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
.net 데이터베이스 조작 프레임워크 SqlSugar의 간단한 시작SqlSugar는 오래된 브랜드입니다.NET 데이터베이스 조작 프레임워크, 과당 빅데이터 과학기술팀이 유지 보수 및 업데이트, Github star 수 EF 및 Dapper 버금가 지원 데이터베이스: MySql, S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.