samedi 27 juin 2015

ordering sql results from IN() function

I have a query:

SELECT categories_id
     , categories_name
     ,categories_url
FROM categories
WHERE categories_id IN(0,72,51,52)

For the IN function I have the numbers 0,72,51,52, but when I get the sql results they are 0,51,52,72, i.e. chronological. How can I order them the same way there were originally as 0,72,51,52?

Base64 encoded image string into Oracle BLOB database

I am tasked with taking a base64 encoded image and storing it into an Oracle SQL database as a blob.

I have the string of the base64 encoded image but it doesn't look like what gets exported from the sql database column blob.

When I export the blob from the database into an xml it looks like so:

image="FFD8FFE000104A46494600010100000100010000FFFE003B43524541544F523A2067642D6A7065672076312E3020287573696E6720494A47204A50454720763830292C207175616C697479203D2039300AFFDB0043000302020302020303030304030304050805050404050A070706080C0A0C0C0B0A0B0B0D0E12100D0E110E0B0B101610111314...."

Does anyone know what conversion this is? or is this type of conversion possible in C#?

Any help is greatly appreciated, or just a push in the right direction would be awesome also!

Thanks,

errno: 150 "Foreign key constraint is incorrectly formed"

I'm getting the 150 error when attempting to run the following setup script. Whats wrong?

serverPermissions seems to trigger the error but I'm not exactly sure why. I think the foreign key constraints are all properly mapped as far as I can tell. I am probably missing something obvious.

-- Setup my members database
DROP DATABASE IF EXISTS mymembers;
CREATE DATABASE IF NOT EXISTS mymembers;

/*
* The list of all members in the community
*/
DROP TABLE IF EXISTS members;
CREATE TABLE members
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
steamID3 INT NOT NULL UNIQUE,
PRIMARY KEY (id)
);

/*
* Lists all servers in operation
*/
DROP TABLE IF EXISTS servers;
CREATE TABLE servers
(
sid INT NOT NULL AUTO_INCREMENT,
sname VARCHAR(30), -- name of server (short name)
PRIMARY KEY(sid)
);

/*
* Maps a member and a server together along with a flag. 
* Record (0, 12, 1, 0) indicates member 12 has flag 0 on server 1.
*/
DROP TABLE IF EXISTS serverPermissions;
CREATE TABLE serverPermissions
(
mid INT,
sid INT,
flagid INT,
PRIMARY KEY(mid, sid, flagid),
FOREIGN KEY (mid) REFERENCES members(id),
FOREIGN KEY (sid) REFERENCES servers(sid),
FOREIGN KEY (flagid) REFERENCES flags(id)
);

/*
* flags are used to give custom permissions to players.
* For example a record may be: (0, "VIP", "This play is very important")
*/
DROP TABLE IF EXISTS flags;
CREATE TABLE flags
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(10),
description VARCHAR(100),
PRIMARY KEY(id)
);

Why does my SSIS package takes so long to execute?

I am fairly new creating SSISs; I have this SQL Server 2008 table called BanqueDetailHistoryRef containing 10,922583 rows.

I want to extract the rows that were inserted on a specific date (or dates) and insert them on a table on another server. I am trying to achieve this through a SSIS which diagram looks like this:

OLEDB Source (the table with the 10Million+ records) --> Lookup --> OLEDB Destination

On the look up I have set: enter image description here enter image description here enter image description here

Now, the query (specified on the Lookup transformation):

SELECT * FROM BanqueDetailHistoryRef WHERE ValueDate = '2014-01-06';

Takes around 1 second to run through SQL Server Management Studio, but the described SSIS package is taking really long time to run (like an hour).

Why is causing this? Is this the right way to achieve my desired results?

Insight.Database procedure call using ODBC fails

I am using the ODBC provider to connect to a iSeries DB2 database. All the following calls work great!:

List<Beer> beers = conn.QuerySql<Beer>("SELECT * FROM Beer WHERE Typee = @Typee", new { Typee = "IPA" }).ToList();

var beer = new Beer { ID=41,Typee="Medium", Description = "From dotNet Neither light or dark"};

conn.ExecuteSql("INSERT INTO Beer VALUES (@ID, @Typee, @Description)", new { ID = 4, Typee = "Medium", Description = "From dotNet Neither light or dark" });

conn.ExecuteSql("CALL INSERTBEER (@ID, @Typee, @Description) ", new { ID = 4, Typee = "MediumOD", Description = "From dotNet Neither light or dark" });

conn.ExecuteSql("INSERT INTO Beer VALUES (@ID, @Typee, @Description)", beer);

Which is awesome, I am really digging this micro-ORM. But I dont want to type out the LHS parameters in the call to the InsertBeer stored procedure. I believe that is the type of plumbing work that can be avoided with the Execute() procedure. Sadly, these calls DONT work:

conn.Execute("INSERTBEER", beer);

I also tried:

conn.Execute("insertbeer", new Beer {ID = 4, Typee = "Medium", Description = "From dotNet Neither light or dark"}, CommandType.StoredProcedure, true, Int32.MaxValue, null, beer);   

The error I am getting is:

{"ERROR [42000] [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0104 - Token INSERTBEER was not valid. Valid tokens: ( CL END GET SET CALL DROP FREE HOLD LOCK OPEN WITH."}

Any ideas greatly appreciated!

Improve on How to get the number of occourence for each distinct Value of Group Concat Mysql

I am trying to fiddle around this answer to get a more improved output. I have a table like this

name |status
-------------
mike |yes
mike |yes
mike |no
mike |ney
john |no
john |ney
john |yes

to output something like this

name |status           |total
------------------------------
mike |yes-2,no-1,ney-1 | 4
john |yes-1,no-1,ney-1 | 3

Someone suggested this answer that works great.

SELECT name, GROUP_CONCAT(totalPerStatus) AS status, 
       (SELECT COUNT(*) FROM mytable WHERE name = t.name) AS total
FROM (
  SELECT name,      
         CONCAT(status, '-', COUNT(*)) AS totalPerStatus            
  FROM mytable
  GROUP BY name, status ) t
GROUP BY name;

But I want to improve on this output to get something like this

name | yes | no | ney | total
------------------------------
mike |2    |1   |1    | 4
john |1    |1   |1    | 3

dimanche 10 mai 2015

Using [Authorize] on the GET and POST methods with same name

Maybe the answer to the following is simple, but I have hard time finding the answer:

When I have a GET method in a controller that is secured with the [Authorize] attribute, and a POST method (defined with [HttpPost]), will the same restrictions apply to it as well? Both methods have the same name, but differ in parameters.

Example code:

 [Authorize(Roles = "Administrator")]
 public ActionResult Delete()
 {
     return View();
 }

 [HttpPost]
 public ActionResult Delete(int id)
 {            
     /* the method's logic omitted */
     return RedirectToAction("Index");
 }