View pg_type not found - npgsql

Npgsql v3.0.0-beta0001 (Prerelease) was having below issue while opening connection.
NpgsqlConnection conn = new NpgsqlConnection(#"Server=xx.xx.xx.xx;Port=9996;Database=xxx;User Id=xx;Password=xx;CommandTimeout=40;");
conn.Open();
ex {" : View 'pg_type' not found"} System.Exception {Npgsql.NpgsqlException}
StackTrace " at Npgsql.NpgsqlConnector.DoReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage)\r\n at Npgsql.NpgsqlConnector.ReadSingleMessage(DataRowLoadingMode dataRowLoadingMode, Boolean returnNullForAsyncMessage)\r\n at Npgsql.NpgsqlConnector.SkipUntil(BackendMessageCode stopAt1, BackendMessageCode stopAt2)\r\n at Npgsql.NpgsqlDataReader.SkipUntil(BackendMessageCode stopAt1, BackendMessageCode stopAt2)\r\n at Npgsql.NpgsqlDataReader.NextResultInternal()\r\n at Npgsql.NpgsqlDataReader.NextResult()\r\n at Npgsql.NpgsqlDataReader.Init()\r\n at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior)\r\n at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)\r\n at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)\r\n at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)\r\n at Npgsql.TypeHandlerRegistry.LoadBackendTypes(NpgsqlConnector connector)\r\n at Npgsql.TypeHandlerRegistry.Setup(NpgsqlConnector connector)\r\n at Npgsql.NpgsqlConnector.Open()\r\n at Npgsql.NpgsqlConnectorPool.GetPooledConnector(NpgsqlConnection Connection)\r\n at Npgsql.NpgsqlConnectorPool.RequestConnector(NpgsqlConnection connection)\r\n at Npgsql.NpgsqlConnection.Open()\r\n at Test.Program.MssDenodoUpdate(String ssConnectionName, String ssTableName, TestDenodoUpdate ssUpdateRecord, String ssWhereClause, Int32 ssTimeout, Int32& ssUpdatedRows) in d:\EMRP\EMRP\DenodoConnector\Source\ConsoleApplication2\Program.cs:line 66" string

EntityFramework needs a "maintenance" DB in order to create databases, etc.
Before npgsql version 2.2.5. this maintenance DB was "postgres" but, since then this maintenance DB is "template1", which exists on all PostgreSQL versions.
To make EF work with Denodo you have to create in VDP the dummy database called template1.
Hope this helps!

Virtual DataPort is compatible with the .Net Data Provider for PostgreSQL version 2.0. The recommended versions are 2.0.12, 2.2.0, 2.2.3 and 2.2.7 .
I used .netframework 4.5, npgsql version 2.2.0
string query = "SELECT * FROM car";
string connectionString = "Server=localhost;Port=9996;Username=admin;Password=admin;Database=admin;Timeout=300;CommandTimeout=300";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
{
connection.Open();
using (NpgsqlCommand command = new NpgsqlCommand(query, connection))
{
using (DbDataReader reader = command.ExecuteReader())
{
int readRows = 0;
Console.WriteLine();
Console.WriteLine("RESULTS:");
while (reader.Read() && readRows < maxRows)
{
for (int col = 0; col < reader.FieldCount; col++)
{
Console.Write(reader.GetName(col) + "=" + reader[col]);
Console.Write(" ");
}
Console.WriteLine();
readRows++;
}
}
}
}
}

Related

SqlPackage.exe - "System.StackOverflowException"

I have a series of PowerShell scripts that are run by a TFS agent are part of a build process. These are run on several servers(Windows Server 2012 R2) that publish a series of DACPAC's to a given set of Databases. Recently I updated all the TFS build agents to the latest version(TFS 2018)
Today I noticed that one of these servers in my build process is no longer running, in particular it is failing to run "SqlPackage.exe" due to a "System.StackOverflowException" error(So very appropriate for this site).
This same issue can be reproduced by running the power shell script by hand, but only on this one server, all the others run without issue. The script looks like this:
$arguments = '/a:Publish /pr:"' + $scriptPath + $database + ".publish.xml" + '" /sf:"' + $dacPac + '" /tcs:"Data Source=' + $servername + ';Persist Security Info=True;User ID=' + $username + ';Password=' + $password + ';Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True"'
Start-Process -FilePath "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\130\SqlPackage.exe" -ArgumentList $arguments -NoNewWindow -PassThru -Wait
When run by hand, the debugged the exception is:
An unhandled exception of type 'System.StackOverflowException' occurred in Microsoft.SqlServer.TransactSql.ScriptDom.dll
I'm really not sure what configuration on this server would cause this sort of issue. Resource wise the server is very powerful with large amounts of available memory, the other servers run it just fine. I've tried various versions of "SqlPackage"(13, 14) but it doesn't seem to have any effect. I've swapped out the DacPac's but that doesn't seem to work either...
Has anyone seen this issue before? What sort of server configuration can cause this sort of issue?
Update 1: hmmm, just switch to the new "14.0", "SqlPackage.exe" now I'm getting it on all my machines, I wonder if it has to do with any realated dll's such as the ones I installed in SSDT.
Actually now that I think about this, I think this issue started on the server when I first installed VS 2017, I wonder if that has any effect on "SqlPackage.exe"?
I also found this interesting post, I wonder if I can work around it this way...
I never figured out how to solve this for "SqlPackage", we ended up creating our own package deployer console app and calling that instead via a console app ("DacpacDeployUtility"):
static int Main(string[] args)
{
try
{
string destinationServer = args[0];
string destinationDatabase = args[1];
string userID = args[2];
string password = args[3];
string publishFileFullPath = args[4];
string dacpacFileFullPath = args[5];
SetupRegistryQueryExecutionTimeout();
PublishDacpacSimple(destinationServer, destinationDatabase, userID, password, publishFileFullPath, dacpacFileFullPath);
return 0; //where 0 = success
}
catch (Exception ex)
{
Console.WriteLine("Error in Main: " + ex.Message + "\n" + ex.StackTrace);
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Value in args[" + i + "]: " + (i == 3 ? "**********" : args[i]));
}
Console.WriteLine("Failed to publish dacpac.");
//Return error state
return 1;
}
}
private static void SetupRegistryQueryExecutionTimeout()
{
//Fixes an annoying issue with slow sql servers: https://stackoverflow.com/a/26108419/2912011
RegistryKey myKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\VisualStudio\\12.0\\SQLDB\\Database", true);
if (myKey != null)
{
myKey.SetValue("QueryTimeoutSeconds", "0", RegistryValueKind.DWord);
myKey.Close();
}
myKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\VisualStudio\\14.0\\SQLDB\\Database", true);
if (myKey != null)
{
myKey.SetValue("QueryTimeoutSeconds", "0", RegistryValueKind.DWord);
myKey.Close();
}
myKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\VisualStudio\\15.0\\SQLDB\\Database", true);
if (myKey != null)
{
myKey.SetValue("QueryTimeoutSeconds", "0", RegistryValueKind.DWord);
myKey.Close();
}
}
private static void PublishDacpacSimple(string destinationServer,
string destinationDatabase,
string userID,
string password,
string publishFileFullPath,
string dacpacFileFullPath)
{
string connectionString = BuildConnectionString(destinationServer, destinationDatabase, userID, password);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(publishFileFullPath);
DacServices ds = new DacServices(connectionString);
using (DacPackage package = DacPackage.Load(dacpacFileFullPath))
{
var options = new DacDeployOptions();
options.CommandTimeout = 600;
ds.Message += (object sender, DacMessageEventArgs eventArgs) => Console.WriteLine(eventArgs.Message.Message);
ds.Deploy(package, destinationDatabase, true, options);
}
}
Then call that in the PowerShell script:
$DacPacDeployerPath = """" + $scriptPath + "..\..\..\DacpacDeployUtility\bin\release\EBMDacpacDeployUtility.exe"""
$Output = Start-Process -FilePath $DacPacDeployerPath -ArgumentList $arguments -NoNewWindow -PassThru -Wait

google apps script jbdc sql server database connection string error

I'm trying to connect to my local sql server database at my home network using the code that I found from this site Querying SQL Server with Google Apps Script via JDBC about 3 years ago which was marked being correct. However, I get the error message "We're sorry, a server error occurred. Please wait a bit and try again.". This error is from line 2 where the connection string is defined. I retried several times, but I always get the same error. When I searched this error, it seems like it could be too many things and I was not able to find any answers for my issue. Thanks.
This was the code that was marked being correct:
function readAzure() {
var conn = Jdbc.getConnection("jdbc:sqlserver://XYZ.database.windows.net:1433;databaseName=MYDATABSENAME","USERNAME","PASSWORD");
var stmt = conn.createStatement();
var rs = stmt.executeQuery("select * from helloworld");
var doc = SpreadsheetApp.create('azure');
var cell = doc.getRange('a1');
var row = 0;
while(rs.next()) {
cell.offset(row, 0).setValue(rs.getString(1));
cell.offset(row, 1).setValue(rs.getString(2));
row++;
}
rs.close();
stmt.close();
conn.close();
}
I also found another connection string code and when I try this format I get the same error.
var conn = Jdbc.getConnection("jdbc:sqlserver://IP-address:1433;" + "databaseName=DBName;user=username;password=password;");
Based on this documentation, you need to ensure that your database accepts connections from any of Apps Script's IP addresses. These are the address ranges you'll need to whitelist.
64.18.0.0 - 64.18.15.255
64.233.160.0 - 64.233.191.255
66.102.0.0 - 66.102.15.255
66.249.80.0 - 66.249.95.255
72.14.192.0 - 72.14.255.255
74.125.0.0 - 74.125.255.255
173.194.0.0 - 173.194.255.255
207.126.144.0 - 207.126.159.255
209.85.128.0 - 209.85.255.255
216.239.32.0 - 216.239.63.255
Also, from the documentation link above, there is a sample code that you can follow to set up external database via JDBC.
Here is a code that demonstrates how to write a single record to the database as well as a batch of 500 records.
// Replace the variables in this block with real values.
var address = 'database_IP_address';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
// Write one row of data to a table.
function writeOneRecord() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.prepareStatement('INSERT INTO entries '
+ '(guestName, content) values (?, ?)');
stmt.setString(1, 'First Guest');
stmt.setString(2, 'Hello, world');
stmt.execute();
}
// Write 500 rows of data to a table in a single batch.
function writeManyRecords() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
conn.setAutoCommit(false);
var start = new Date();
var stmt = conn.prepareStatement('INSERT INTO entries '
+ '(guestName, content) values (?, ?)');
for (var i = 0; i < 500; i++) {
stmt.setString(1, 'Name ' + i);
stmt.setString(2, 'Hello, world ' + i);
stmt.addBatch();
}
var batch = stmt.executeBatch();
conn.commit();
conn.close();
var end = new Date();
Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
}
For more information, just read through to this documentation and check this thread and related SO question.

Dapper Contrib Insert MySQL Syntax Error

Hey I am getting syntax error in MySQL using Dapper.Contrib
MySQL server version for the right syntax to use near '[cause_code],[cause_name]) values ('000-DDH', 'No Money')' at line 1
the correct syntax for Insert in mysql is
"Insert Into `tbl_cause` (`cause_code`, `cause_name`) VALUES('blah', 'blah')";
my code:
var entity = new Cause { cause_code = "000-DDH", cause_name = "No Money" };
using (IDbConnection cn = ConStr.Conn())
{
long ins = cn.Insert(entity);
if (ins > 0)
{
MessageBox.Show("Cause Code: " + entity.cause_code + " Successfully Added!");
GDRD();
}
else {
MessageBox.Show("Cause Code: " + entity.cause_code + " While trying to Add an Error Occurred!");
}
}
how to solve this? thanks in Advance
It's you again :)
Does the class Cause have a Table("tbl_cause") directive?
Read the dapper contrib extensions documentation here

Exception: conversion from UNKNOWN to UNKNOWN is unsupported

I'm converting some jdbc code from MySql to SQL Server. When trying to
query = "Update ReportSetup "
+ "set N_ID=?, "
+ "R_Default=?, "
+ "R_Name=?, "
+ "R_Module=? "
+ " where R_ID = ?";
}
PreparedStatement stmt =
(PreparedStatement) con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
stmt.setInt(1, ri.getNodeID());
stmt.setInt(2, (ri.isDefault()) ? 1 : 0);
stmt.setString(3, ri.getName());
Object o = ri.getReportModule();
stmt.setObject(4, o);
The last statement stmt.setObject(4,o) throws the exception.
ri.getReportModule returns an instance of a class which implements Externalizable.
The method writeExternal() of that class is implemented as
public final void writeExternal(final ObjectOutput objectOutput) throws IOException {
for (int i=0; i<pdV.size(); i++) {
PropertyDescriptor pd = pdV.elementAt(i);
try {
Method m = pd.getReadMethod();
Object val = pd.getReadMethod().invoke(this);
System.out.print("writing property " + i + ": " + pd.getName() + " = " + val);
objectOutput.writeObject(val);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The database column in question is defined as
varbinary(max), not null
The code works well using MySql but I can't figure out what to do to make it run with Sql Server.
Any suggestions would be very much appreciated
The problem was that sql server is not happy to save a serialization (as done when implementing externalizable). .setObject() fails. The solution is to use setBinaryStream().
// Sql Server can't do an stmt.setObject(4,o) "Conversion from UNKNOWN to UNKNOWN not supported"
// Serialize the object to an output stream and then read it in again into the stmt.
Object o = ri.getReportModule();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(bos);
objectOutput.writeObject(o);
objectOutput.flush();
InputStream objectInput = new ByteArrayInputStream(bos.toByteArray());
stmt.setBinaryStream(4, objectInput);
Cheers
Christian

How can I obtain an Active Directory Group name from a SQL Server stored SID?

This is a follow-up of a question I asked earlier this morning (posted here.) Following the instructions provided, I've managed to query my SQL Server 2000 database for a SID associated with an AD Group. The SID, however, looks like this:
0x0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567
What can I do to obtain the name of the AD Group referenced by the SID? I've tried googling PowerShell scripts, however, most of their examples of SIDs look like this:
S-1-5-21-1454471165-1004335555-1606985555-5555
Obviously, that doesn't look like the value I'm getting back from the SQL Server. How can I do this?
If you're using sqlps (SQL Powershell host) which works against SQL 2000 (I've tested this on my 2000 instance) you can use this:
$query = #"
select sid from syslogins where isntgroup = 1
AND name = 'CONTOSO\mylogin'
"#
invoke-sqlcmd -ServerInstance "myserver" -Database master -Query $query |
foreach {$SID = new-object security.principal.securityidentifier($_.SID,0); $SID.translate([system.security.principal.NTAccount]) }
For those without sqlps:
use this online C# shell do format single sid to text
http://rextester.com/AFAC13570
code backup:
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
namespace Rextester
{
public class Program
{
public static string ConvertByteToStringSid(Byte[] sidBytes)
{
StringBuilder strSid = new StringBuilder();
strSid.Append("S-");
// Add SID revision.
strSid.Append(sidBytes[0].ToString());
// Next six bytes are SID authority value.
if (sidBytes[6] != 0 || sidBytes[5] != 0)
{
string strAuth = String.Format
("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}",
(Int16)sidBytes[1],
(Int16)sidBytes[2],
(Int16)sidBytes[3],
(Int16)sidBytes[4],
(Int16)sidBytes[5],
(Int16)sidBytes[6]);
strSid.Append("-");
strSid.Append(strAuth);
}
else
{
Int64 iVal = (Int32)(sidBytes[1]) +
(Int32)(sidBytes[2] << 8) +
(Int32)(sidBytes[3] << 16) +
(Int32)(sidBytes[4] << 24);
strSid.Append("-");
strSid.Append(iVal.ToString());
}
// Get sub authority count...
int iSubCount = Convert.ToInt32(sidBytes[7]);
int idxAuth = 0;
for (int i = 0; i < iSubCount; i++)
{
idxAuth = 8 + i * 4;
if (idxAuth >= sidBytes.Length)
{
Console.WriteLine("OK :old NT account");
return strSid.ToString();
}
UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth);
strSid.Append("-");
strSid.Append(iSubAuth.ToString());
}
return strSid.ToString();
}
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine(
ConvertByteToStringSid(
SoapHexBinary.Parse(
"0x01050000000000051500000079542007311FAE6D096510145E540300".Substring(2)
).Value
)
);
}
}
}
credits:
https://www.sqlservercentral.com/Forums/FindPost1322822.aspx
How do you convert Byte Array to Hexadecimal String, and vice versa?

Resources