前提需要安装HANA Windows Client
ODBC测试数据库连接
在CMD中用odbcad32.exe打开ODBC驱动管理程序
配置完成后,测试连接成功,就可以读取HANA数据库了
使用c#读取HANA数据库
public class HanaHelp
{
public HanaHelp() { }
public OdbcConnection getConnection()
{
String connStr = "Driver={H00};ServerNode=0.0.0.0:xx;uid=aa;pwd=password;DatabaseName=xx";
var conn = new OdbcConnection(connStr);
return conn;
}
public DataTable ListAll()
{
try
{
OdbcConnection conn = this.getConnection();
String sql = "SELECT * FROM VBAP";
conn.Open();
Console.WriteLine($"当前HANA数据库状态{conn.State}");
Console.WriteLine($"当前连接的HANA数据库服务器的版本{conn.ServerVersion}");
var TableName = sql.Split(‘ ‘)[3];
Console.WriteLine($"当前要读取的数据库表:{TableName}");
OdbcCommand selectCmd = new OdbcCommand(sql, conn);
var adapter = new OdbcDataAdapter(selectCmd);
DataTable employeeTable = new DataTable("employees");
adapter.Fill(employeeTable);
conn.Close();
return employeeTable;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
HanaHelp command = new HanaHelp();
DataTable a = command.ListAll();
if (a != null)
{
Console.WriteLine("读取到第一行数据为:");
for (int i = 0; i < a.Rows[0].ItemArray.Length; i++)
{
Console.Write(a.Rows[0].ItemArray[i]+" ");
}
}
Console.ReadKey();
}
}
原文地址:https://www.cnblogs.com/fuchj/p/10417900.html
时间: 2024-10-11 09:37:27