이 안내서는 C# 응용 프로그램 내에 여러 문 (여러 줄에 걸쳐있는 잠재적 인 여러 줄에 걸쳐있는 SQL 스크립트 파일을 실행하는 방법을 보여줍니다. 이 작업에 대해 Microsoft SQL Server Management Objects (SMO)를 활용합니다.
여기에 C# 코드 예제 :
using System;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
public class SqlScriptRunner
{
public void RunScript(string scriptPath, string connectionString)
{
// Read the entire SQL script from the file.
string sqlScript = File.ReadAllText(scriptPath);
// Establish a database connection.
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create a Server object using the connection.
Server server = new Server(new ServerConnection(connection));
// Execute the script using SMO's ExecuteNonQuery.
server.ConnectionContext.ExecuteNonQuery(sqlScript);
}
}
}
구현 단계 :
microsoft.sqlserver.management.smo
및 microsoft.sqlserver.management.common
어셈블리 포함 sqlscripttrunner
의 인스턴스를 생성하고 runscript
메서드를 호출하여 SQL 스크립트 파일과 유효한 데이터베이스 연결 문자열에 대한 전체 경로를 제공합니다. 이 방법은 C# 응용 프로그램 내에서 복잡한 SQL 스크립트를 처리하는 깨끗하고 효율적인 방법을 제공합니다. 사용
문을 사용하면 데이터베이스 연결을 자동으로 닫아서 적절한 리소스 관리를 보장합니다.
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3