Dapper Contrib Insert MySQL Syntax Error - dapper

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

Related

Db::getInstance()->ExecuteS error 'must be used only with select' when i'm doing it

I have the same problem when i try to make a custom sql. My Prestashop version 1.7.3.4
$sql_order_detail = 'SELECT `id_order_detail`, `product_id`, `product_quantity`, `unit_price_tax_incl` FROM '._DB_PREFIX_.'order_detail WHERE `id_order` = ' . $id_order;
$order_details = Db::getInstance()->ExecuteS($this->sql_order_detail, $array = true, $use_cache = 0);
I test my sql through logs and phpMyAdmin:
SELECT `id_order_detail`, `product_id`, `product_quantity`, `unit_price_tax_incl` FROM ps_order_detail WHERE `id_order` = 24
The error is:
stderr: Db->executeS() must be used only with select, show, explain or describe queries,
Solved using:
(mysteriously)
$sql = new DbQuery();
$sql->select('*');
$sql->from('order_detail');
$sql->where('id_order = ' . $id_order);
$order_details = Db::getInstance()->executeS($sql);

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.

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

Help with SSIS Package

Hi all i've got a complex SSIS package, however i'm hitting my head against a brick wall with a particular part. I've got a maintenance part that will delete files that are older than 3 months old (from today's date). The files all have the date in the filename, for example AA-CDR-20110606030000-2-001A648E6F74-026874.xml
So i've written a task that will loop over all the files in a particular folder using a Foreach loop, then i have a script that will load in the filename using a variable set using the foreach loop. Then delete the file using the script below. This works fine when running in debug mode. However when trying to execute this on the server i get a failure with a "System.FormatException: String was not recognized as a valid DateTime.". I really dont understand why, any ideas?
DateTime deleteDate = DateTime.Today.AddMonths(-3);
String file = Dts.Variables["FileName"].Value.ToString();
String fileDateString = file.Substring(42, 8);
String fileDateYear = fileDateString.Substring(0, 4);
String fileDateMonth = fileDateString.Substring(4, 2);
String fileDateDay = fileDateString.Substring(6, 2);
DateTime fileDateTime = Convert.ToDateTime(fileDateDay + "-" + fileDateMonth + "-" + fileDateYear);
if (fileDateTime < deleteDate)
{
System.IO.File.Delete(file);
}
It seems that there is a file on the production server that does not follow the pattern and the date cannot be extracted from its name.
Try to use something like this:
try
{
DateTime fileDateTime = Convert.ToDateTime(fileDateDay + "-" + fileDateMonth + "-" + fileDateYear);
if (fileDateTime < deleteDate)
{
System.IO.File.Delete(file);
}
}
catch (System.FormatException)
{
// log the Dts.Variables["FileName"] value here to see why it could not be parsed
}

Resources