SYBASE ODBC .Net CORE - asp.net

1. Has anyone successfully used ODBC from c# to Sybase ASE?
2. Or Better, has anyone successfully used Sybase ASE with .NET Core?
I am using .NET Core 1.1 and the current Sybase.AdoNet4.AseClient.dll doesn't work, so I am attempting to use ODBC. I have tried to use two ODBC packages:
Mono.Data.OdbcCore (NuGet)
MSA.Net.Core.ODBC (NuGet)
Both work well with in-line queries and both work well calling a Sybase Store Procedures WITHOUT Parameter but both have the same exact error when trying to send parameters to a sp, where the parameters are required.
Here is a snippet:
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "XX_GetLookUp";
command.Parameters.Add("#Type", OdbcType.VarChar).Value = "ACC";
using (var reader = command.ExecuteReader()) {
...
}
I constantly receiving the following error during the command.ExecuteReader():
System.Data.Odbc.OdbcException: 'ERROR [ZZZZZ] [SAP][ASE ODBC Driver][Adaptive Server Enterprise]Procedure iKYC_GetLookUp expects parameter #Type, which was not supplied.
I have tried to use Type with and without the # but each generate the same error.
I did try several other flavors as well:
command.CommandText = "{call dbo.iKYC_GetLookUp(?)}";
command.CommandText = "{call dbo.iKYC_GetLookUp}";
command.CommandText = "{dbo.iKYC_GetLookUp(?)}";
command.CommandText = "dbo.iKYC_GetLookUp(?)";
command.CommandText = "dbo.iKYC_GetLookUp ?";
Where each above generates: System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'
I also tried to build the parameter object separate, with no luck (same error, missing #Type):
OdbcParameter p = new OdbcParameter();
p.ParameterName = "#Type";
p.DbType = DbType.AnsiString;
p.Direction = ParameterDirection.InputOutput;
p.Value = "ACC";
cmd.Parameters.Add(p);
As stated prior, if I make the parameter as null, on the store procedure, the code works and data is returned as expected, so the issue appears to be around the parameters being passed.
Has anyone successfully used ODBC from c# to Sybase ASE?
Has anyone successfully used Sybase ASE with .NET Core?
Additional information:
I am using Visual Studio 2017 (v 15.2). Core 1.1 and using Sybase ASE 16.0 sp2.

We had a similar issue - we wanted to use ADO.NET on the AWS Serverless stack - which is .NET Core only. We raised an issue with SAP requesting support for .NET Core, but got nothing back.
We had a go with ODBC and couldn't get it to work with return values on our stored procedures, and bit the bullet and wrote our own ADO.NET Core DbProvider for ASE 15-16.
It implements the TDS 5 protocol under the hood natively in .NET Core and outperforms the SAP/Sybase implementation in all of our test cases.
https://github.com/DataAction/AdoNetCore.AseClient
There's docs on the wiki to run the unit, integration and benchmark tests for yourself.
Most features are implemented, and pull requests and feedback are welcome. .NET 4.6 and .NET Core 1.0, 1.1, 2.0 and 2.1 are supported.
We have this working using AWS Lambda and .NET Core 2.0.

Related

SignalR + SQLNotifications in .NetCore

Hi I have a webApi and I want to notify when a record changes in the DB. I'm trying to use SQLNotification but this code generates error
sqlcommand does not contain definition for Notification
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Notification = null;
DataTable dt = new DataTable();
SqlDependency dependency = new SqlDependency(command);
}
Is posible to use SQLNotifications in NetCore 2.0?
I may be mistaken but I don't believe it is available currently based on:
Microsoft's announcement stating:
Simplified Scale-Out Model
Unfortunately, when it comes to scaling out applications there is no “one size fits all” model – each application is different and has different requirements that need to be considered when scaling out the application. We have worked to improve, and simplify, the scale-out model and are providing a Redis based scale-out component in this Alpha. Support for other providers is being evaluated for the final release, for example service bus.
As well as this open issue and specifically David Fowler's comment:
It's just a very very bad way to do real time notifications as that's not what sql was meant for.

What is the difference between .net data providers and odbc drivers?

Can you please help me understand the difference between these two technologies of connecting to databases?
I am new to the field of database connectivity, but may you can explain on specific examples which technology is used in each case? What for example should I use if I am developing the application ob Visual Studio and going to connect in with MySQL and why?
May be the question will seem stupid for the professionals, but I would be very thankful if you will clarify it for me.
A .NET data provider is a software component that provides access to a data source. They specifically designed to work in .NET applications (C#, VB.NET, etc.) and will have at least some standard classes/functions: Connection, Command, DataReader, DataAdapter (similar to the native System.Data.SQLClient classes).
An ODBC driver is very similar, but is written using the ODBC standard, and while it can be used programmatically in applications, it is also capable of being used as a data connector in any number of popular BI/reporting/ETL tools (like Tableau, QlikView, PowerBI, etc.). To use an ODBC driver in a .NET application, you'll need to use the Microsoft.Data.ODBC library.
For your use case, you'll want to use an ADO.NET data provider so that you can be sure that you're using a product that was specifically designed to be used in .NET applications. Some basic code would look similar to the following:
string connString = "server=XXX.XXX.XXX.XXX;database=myDatabase;user=myUser;password=myPassword;...";
using (MySQLConnection conn = new MySQLConnection(connString){
MySQLCommand cmd = new MySQLCommand("SELECT * FROM some_table", conn);
MySQLDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
Console.WriteLine("=================");
for (int i=0; i<rdr.FieldCount; i++) {
Console.WriteLine(rdr.GetName(i) + ":\t\t" + rdr.GetValue(i));
}
}
}

Connect to SQL server using Linq, asp.net

I`m creating a website using asp.net, and I need to use a local SQL server (using Microsoft SQL server). And I have created database and tables in it using the MS SQL Server Management Studio.
Now I successfully connect to the database and do some simple add/query using the following commands:
string connectionString = "data source=ABCD\\SQLEXPRESS;initial catalog=PMD;Trusted_Connection=yes;";
string sqlQuery = "INSERT INTO PMD (username, userID, userAddress)";
sqlQuery += " VALUES (#user, id, add)";
SqlConnection dataConnection = new SqlConnection(connectionString);
SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection);
dataCommand.Parameters.AddWithValue("user", USER.Value);
dataCommand.Parameters.AddWithValue("id", ID.Value);
dataCommand.Parameters.AddWithValue("add", ADDRESS.Text);
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
The command above can add one column to the table, with values stated.
The query is done in a similar way. Compared with Linq, this is not very concise.
So I was wondering how can I change the code so I can use Linq.
The biggest question for me now is how to connect to the base. I already know all the syntax of Linq.
eg: var query=from c in db.username where c.Contain(“Micheal”) select c (or maybe db.PMD.username)
How can I get the db to link with ABCD/SQLEXPRESS, table PMD?
First you need an Object/Relational Mapper (O/RM). You can't just put LINQ on top of your old ADO.NET code.
Microsoft provides two: Linq2SQL and Entity Framework.
Linq2SQL has been discontinued. If I had to choose between the two, I'd go with Entity Framework.
Here you can find an introduction: http://www.asp.net/entity-framework
For example, install Entity Framework, then connect to sql server with entity framework

ServiceStack OrmLite using Sqlite64 as memory database results in missing auth tables

I am trying to use Sqlite as a memory database with ServiceStack ORMlite in my unit tests.
If I run my tests with SQLite saving to a file, ie. using the connectionstring
"Data Source=|DataDirectory|unittest.db;Version=3;"
, it works fine and auth tables are generated fine by ServiceStacks
userRepository.CreateMissingTables();
However, when I try using SQLite as a memory database by using this connectionstring
":memory:"
I get an exception when saying
SQLite error
no such table: UserAuth
the first time I try to fetch a user by doing this
userRepository.GetUserAuthByUserName(...)
This is after I have called userRepository.CreateMissingTables() and it works fine if I switch to using SQLite with a file database. Does anybody know what the problem could be? (I had to down grade to version 3.9.0 of ORMLite because of bad references to version 1.0.65.0 of ORM lite in Ormlite 3.9.4)
ServiceStack v5
Recent versions of ServiceStack automatically disables AutoDisposeConnection for SQLite :memory: connections, so you can configure your OrmLiteConnectionFactory normally, e.g:
var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider);
var sqliteRepo = new OrmLiteAuthRepository(dbFactory);
sqliteRepo.CreateMissingTables();
ServiceStack v3
You lose your database whenever you close the DB connection of an Sqlite in memory database.
So when you configure your DB Factory you need to tell it to never dispose of the connection, which you can do with the autoDisposeConnection constructor param, e.g:
var dbFactory = new OrmLiteConnectionFactory(":memory:",
autoDisposeConnection:false,
dialectProvider:SqliteDialect.Provider);
var sqliteRepo = new OrmLiteAuthRepository(dbFactory);
sqliteRepo.CreateMissingTables();

ODBC driven ASP.NET application running on iis doesn't work on speceific system

I'm having a strange problem with IIS, asp.net and ODBC.
My application is driven by SQL server via ODBC driver (I know it's bad practice, but my entire DAL is already written and will not be changed).
The problem is that when I run an SP with my web interface, on any other computer other than the production server it works fine, but on the production server I get the following error:
Exception Message: System.Data.Odbc.OdbcException (0x80131937): ERROR [42000] [Microsoft] [ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to int
Obviously when I run it under management studio it works fine.
I think the problem is somewhere between the IIS and the odbc driver, but I'm not sure exactly where.
I'm running .net framework 4.
This is the calling method:
ODBCComm command = new ODBCComm();
command.Query = "SP_web_update_calls_dest #id=?,#name=? ,#ivrCode=?,#DDI=?,#destType=?,#trkGroup=?,#result=? output";
command.AddInputParam(id);
return AddParamsAndExecute(name, ivrCode, DDI, destType, trkGroup, command);
it basically wraps arround:
OdbcCommand.ExecuteDirect();
Thanks a lot,
Yuval.
I believe your first param is being used in the ID spot. I'm not sure why this would work in pre-production and fail only in production, but try this instead...
ODBCComm command = new ODBCComm();
command.Query = "SP_web_update_calls_dest #id=?,#name=? ,#ivrCode=?,#DDI=?,#destType=?,#trkGroup=?,#result=? output";
return AddParamsAndExecute(id, name, ivrCode, DDI, destType, trkGroup, command);
I have no idea what ODBCComm is or what AddParamsAndExecute does because you haven't included the relavant code, however, here's what the request should look like:
OdbcCommand Cmd = new OdbcCommand("SP_web_update_calls_dest", _Connection);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add("#id", OdbcType.Int);
Cmd.Parameters["#id"].Value = id;
Cmd.Parameters.Add("#name", OdbcType.NVarChar);
Cmd.Parameters["#name"].Value = name;
Cmd.Parameters.Add("#ivrCode", OdbcType.Int);
Cmd.Parameters["#ivrCode"].Value = ivrCode;
Cmd.Parameters.Add("#DDI", OdbcType.VarChar);
Cmd.Parameters["#DDI"].Value = DDI;
Cmd.Parameters.Add("#destType", OdbcType.Int);
Cmd.Parameters["#destType"].Value = destType;
Cmd.Parameters.Add("#trkGroup", OdbcType.Int);
Cmd.Parameters["#trkGroup"].Value = trkGroup;
Cmd.Parameters.Add("#result", OdbcType.Int);
Cmd.Parameters["#result"].Direction = ParameterDirection.Output;
Cmd.ExecuteNonQuery();
int result = (int)Cmd.Parameters["#result"].Value;
OK,
I think I solved it.
There were actually 2 problems, and I'm not exactly sure what caused them.
Anyway, I switched the ODBC driver to SQL Server Native Client 10.
This basically solved the problem, but for some reason it doesn't support the output modifier in queries so I had to remove that.
So I got it solved, but still have no idea what caused the problem. I'm guessing it has something to do with different versions of drivers.

Resources