返回
Foxnic-SQL (7) —— DAO 特性 : 执行 SQL 语句
后端
2024-02-23 10:33:08
Foxnic-SQL (7) —— DAO 特性 : 执行 SQL 语句
Foxnic-SQL 支持多种语句执行方式,包括直接执行 SQL 字符串、执行 SQL 对象,SQL 对象自执行、多语句执行与增量处理、分页查询,并提供了丰富的 SQL 异常处理与诊断机制。文中将对这几种执行方式进行简要概述,并辅以示例进行说明。
直接执行 SQL 字符串
//创建一个新的数据库连接
Foxnic.SQL.Connection connection = new Foxnic.SQL.Connection();
connection.Open("server=127.0.0.1;uid=sa;pwd=;database=FoxnicSQL;");
//创建并执行一个新的 SQL 语句
string sql = "SELECT * FROM Orders WHERE OrderDate > '2019-01-01'";
Foxnic.SQL.Command command = new Foxnic.SQL.Command(sql, connection);
Foxnic.SQL.DataReader reader = command.ExecuteReader();
//遍历结果集
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",
reader["OrderID"],
reader["OrderDate"],
reader["CustomerID"],
reader["TotalAmount"]);
}
//释放资源
reader.Close();
command.Dispose();
connection.Close();
执行 SQL 对象
//创建一个新的数据库连接
Foxnic.SQL.Connection connection = new Foxnic.SQL.Connection();
connection.Open("server=127.0.0.1;uid=sa;pwd=;database=FoxnicSQL;");
//创建并执行一个新的 SQL 对象
Foxnic.SQL.Select select = new Foxnic.SQL.Select();
select.Columns.Add("*");
select.From.Add("Orders");
select.Where.Add("OrderDate > '2019-01-01'");
Foxnic.SQL.Command command = new Foxnic.SQL.Command(select, connection);
Foxnic.SQL.DataReader reader = command.ExecuteReader();
//遍历结果集
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",
reader["OrderID"],
reader["OrderDate"],
reader["CustomerID"],
reader["TotalAmount"]);
}
//释放资源
reader.Close();
command.Dispose();
connection.Close();
SQL 对象自执行
//创建一个新的数据库连接
Foxnic.SQL.Connection connection = new Foxnic.SQL.Connection();
connection.Open("server=127.0.0.1;uid=sa;pwd=;database=FoxnicSQL;");
//创建并执行一个新的 SQL 对象
Foxnic.SQL.Select select = new Foxnic.SQL.Select();
select.Columns.Add("*");
select.From.Add("Orders");
select.Where.Add("OrderDate > '2019-01-01'");
select.Execute(connection);
//遍历结果集
while (select.Reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",
select.Reader["OrderID"],
select.Reader["OrderDate"],
select.Reader["CustomerID"],
select.Reader["TotalAmount"]);
}
//释放资源
select.Reader.Close();
connection.Close();
多语句执行与增量处理
//创建一个新的数据库连接
Foxnic.SQL.Connection connection = new Foxnic.SQL.Connection();
connection.Open("server=127.0.0.1;uid=sa;pwd=;database=FoxnicSQL;");
//创建并执行一个新的 SQL 语句
string sql = @"
-- 创建一个新的表
CREATE TABLE Orders (
OrderID INT NOT NULL IDENTITY PRIMARY KEY,
OrderDate DATETIME NOT NULL,
CustomerID INT NOT NULL,
TotalAmount DECIMAL(18, 2) NOT NULL
);
-- 插入一些数据
INSERT INTO Orders (OrderDate, CustomerID, TotalAmount)
VALUES ('2019-01-01', 1, 100.00),
('2019-01-02', 2, 200.00),
('2019-01-03', 3, 300.00);
-- 查询数据
SELECT * FROM Orders;";
Foxnic.SQL.Command command = new Foxnic.SQL.Command(sql, connection);
Foxnic.SQL.DataReader reader = command.ExecuteReader();
//遍历结果集
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",
reader["OrderID"],
reader["OrderDate"],
reader["CustomerID"],
reader["TotalAmount"]);
}
//释放资源
reader.Close();
command.Dispose();
connection.Close();
分页查询
//创建一个新的数据库连接
Foxnic.SQL.Connection connection = new Foxnic.SQL.Connection();
connection.Open("server=127.0.0.1;uid=sa;pwd=;database=FoxnicSQL;");
//创建并执行一个新的 SQL 语句
Foxnic.SQL.Select select = new Foxnic.SQL.Select();
select.Columns.Add("*");
select.From.Add("Orders");
select.Where.Add("OrderDate > '2019-01-01'");
select.OrderBy.Add("OrderID");
select.Paging.PageSize = 10;
select.Paging.PageIndex = 1;
Foxnic.SQL.Command command = new Foxnic.SQL.Command(select, connection);
Foxnic.SQL.DataReader reader = command.ExecuteReader();
//遍历结果集
while (reader.Read())
{
Console.WriteLine("{0}\t{1}\t{2}\t{3}",
reader["OrderID"],
reader["OrderDate"],
reader["CustomerID"],
reader["TotalAmount"]);
}
//释放资源
reader.Close();
command.Dispose();
connection.Close();
SQL 异常处理与诊断
try
{
//执行 SQL 语句
}
catch (Foxnic.SQL.Exception ex)
{
//处理 SQL 异常
}