处理消息
可以通过对 VerticaConnection 委托类使用 InfoMessage 事件来捕获 Vertica 向 ADO.NET 驱动程序提供的信息消息和警告消息。此类可捕获严重性不足以强制触发异常但仍可能提供对应用程序有益的信息的消息。
要使用 VerticaInfoMessageEventHander 类:
-
创建一个方法以处理从事件处理程序发送的消息:
static void conn_InfoMessage(object sender, VerticaInfoMessageEventArgs e) { Console.WriteLine(e.SqlState + ": " + e.Message); }
-
为 InfoMessage 事件创建一个连接并注册新的 VerticaInfoMessageHandler 委托:
_conn.InfoMessage += new VerticaInfoMessageEventHandler(conn_InfoMessage);
-
执行查询。如果生成了消息,则会运行事件处理函数。
-
可以使用以下命令取消订阅事件:
_conn.InfoMessage -= new VerticaInfoMessageEventHandler(conn_InfoMessage);
示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vertica.Data.VerticaClient;
namespace ConsoleApplication {
class Program {
// define message handler to deal with messages
static void conn_InfoMessage(object sender, VerticaInfoMessageEventArgs e) {
Console.WriteLine(e.SqlState + ": " + e.Message);
}
static void Main(string[] args) {
//configure connection properties
VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();
builder.Host = "192.168.1.10";
builder.Database = "VMart";
builder.User = "dbadmin";
//open the connection
VerticaConnection _conn = new VerticaConnection(builder.ToString());
_conn.Open();
//create message handler instance by subscribing it to the InfoMessage event of the connection
_conn.InfoMessage += new VerticaInfoMessageEventHandler(conn_InfoMessage);
//create and execute the command
VerticaCommand cmd = _conn.CreateCommand();
cmd.CommandText = "drop table if exists fakeTable";
cmd.ExecuteNonQuery();
//close the connection
_conn.Close();
}
}
}
运行该示例后,将显示以下内容:
00000: Nothing was dropped