Friday, February 4, 2011

SQL Server, nvarchar(MAX) or ntext, image or varbinary?

When should I choose one or the other? What are the implications regarding space and (full-text) indexing?

BTW: I'm currently using SQL Server 2005 planing to upgrade to 2008 in the following months.

Thanks

  • Once you put it in the blob, it's going to be difficult to be used for normal SQL comparison. See Using Large-Value Data Types.

    From eed3si9n
  • The new (max) fields make it a lot easier to deal with the data from .NET code. With varbinary(max), you simply set the value of a SqlParameter to a byte array and you are done. WIth the image field, you need to write a few hundred lines of code to stream the data into and out of the field.

    Also, the image/text fields are deprecated in favor of varbinary(max) and varchar(max), and future versions of Sql Server will discontinue support for them.

cfqueryparam with like operator in coldfusion

I have been tasked with going through a number of ColdFusion sites that have recently been the subject of a rather nasty SQL Injection attack. Basically my work involves adding <cfqueryparam> tags to all of the inline sql. For the most part I've got it down, but can anybody tell me how to use cfqueryparam with the LIKE operator?

If my query looks like this:

select * from Foo where name like '%Bob%'

what should my <cfqueryparam> tag look like?

  • select * from Foo where name like '%' + <cfqueryPARAM... > + '%'
    
  • @Joel, I have to disagree.

    select a,b,c
    from Foo
    where name like <cfqueryparam cfsqltype="columnType" value="%#variables.someName#%" />
    

    1) Never suggest to someone that they should "select star." Bad form! Even for an example! (Even copied from the question!)

    2) The query is pre-compiled and you should include the wild card character(s) as part of the parameter being passed to the query. This format is more readable and will run more efficiently.

    3) When doing string concatenation, use the ampersand operator (&), not the plus sign. Technically, in most cases, plus will work just fine... until you throw a NumberFormat() in the middle of the string and start wondering why you're being told that you're not passing a valid number when you've checked and you are.

    Joel Coehoorn : Agree on not selecting *, but I was just matching the sample query in the original question.
  • I can't vote yet but would lean towards Adam's answer.

    From Sam Farmer

Is there an event that triggers if the number of ListViewItems in a ListView changes? (Windows Forms)

I'd like to enable/disable some other controls based on how many items are in my ListView control. I can't find any event that would do this, either on the ListView itself or on the ListViewItemCollection. Maybe there's a way to generically watch any collection in C# for changes?

I'd be happy with other events too, even ones that sometimes fire when the items don't change, but for example the ControlAdded and Layout events didn't work :(.

  • I can't find any events that you could use. Perhaps you could subclass ListViewItemCollection, and raise your own event when something is added, with code similar to this.

    Public Class MyListViewItemCollection
        Inherits ListView.ListViewItemCollection
    
        Public Event ItemAdded(ByVal Item As ListViewItem)
    
        Sub New(ByVal owner As ListView)
            MyBase.New(owner)
        End Sub
    
        Public Overrides Function Add(ByVal value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem
            Dim Item As ListViewItem
    
            Item = MyBase.Add(value)
    
            RaiseEvent ItemAdded(Item)
    
            Return Item
        End Function
    End Class
    
    From Kibbee
  • @ Kibbee: OK, but then how would I get my ListView to use that collection internally?

    From Domenic
  • I think the best thing that you can do here is to subclass ListView and provide the events that you want.

    From John
  • @Domenic

    Not too sure, Never quite got that far in the thought process.

    Another solution might be to extend ListView, and when adding and removing stuff, instead of calling .items.add, and items.remove, you call your other functions. It would still be possible to add and remove without events being raised, but with a little code review to make sure .items.add and .items.remove weren't called directly, it could work out quite well. Here's a little example. I only showed 1 Add function, but there are 6 you would have to implement, if you wanted to have use of all the available add functions. There's also .AddRange, and .Clear that you might want to take a look at.

    Public Class MonitoredListView
        Inherits ListView
    
        Public Event ItemAdded()
        Public Event ItemRemoved()
    
        Public Sub New()
            MyBase.New()
        End Sub
    
        Public Function AddItem(ByVal Text As String) As ListViewItem
            RaiseEvent ItemAdded()
    
            MyBase.Items.Add(Text)
        End Function
    
        Public Sub RemoveItem(ByVal Item As ListViewItem)
            RaiseEvent ItemRemoved()
    
            MyBase.Items.Remove(Item)
        End Sub
    
    End Class
    
    From Kibbee

Do you know of any "best practice" or "what works" vi tutorial for programmers?

There are thousands of vi tutorials on the web, most of them generically listing all the commands. There are even videos on youtube which show basic functionality. But does anyone know of a vi tutorial which focuses on the needs of programmers? For example when I program in Perl with vi, moving to the "next paragraph" is meaningless. I want to know which commands seasoned vi users combine to e.g. copy everything inside of parentheses, or copy a functio, or copy and paste a variable (e.g. 2yw), etc. I am sure there are lots of functions using multiple-file capability, and the maps, macros, reading in of files for template code, regular expression search, jumping to functions, perhaps minimal code completion, or other features that emulate what programmers have gotten used to in Visual Studio and Eclipse, etc.

  • A nice collection of vimtips.

    And the best Vim cheatsheet around.

    From cschol
  • Try a search on 'vim'

  • I just ended up reading the vim manual a few times, over the years, picking up useful features on each iteration.

    One thing that really made vim work for me as a perl IDE was starting to use tags, as explained here: http://www.vim.org/tips/tip.php?tip_id=94. Using the pltags script that ships with vim, you can jump around between modules to find your functions, methods, etc.

    From zigdon
  • If you are a beginner, vimtutor would be a good way to start with. (Type vimtutor on your shell and get going).

    And once you get hold of the basics of vim, you can look around web and figure out things for yourself.

    This and this may be an interesting read.

    From Jagmal

LINQ to SQL Mapping From Money to Double

I'm working with LINQ for the first time and wanted to get the Mapping to work when I have a money type in SQL, but my domain object property is of type double. How can I express this in the XML file, or in code so that the mapping does not throw the usual "invalid cast" exception?

  • pretty sure Money maps to System.Decimal

    Check here

  • Correct! This now works, but what about something like smallint to integer?

  • In the DBML XML file, you can set the Expression attribute of a Column element to something like this:

     <Column Name="Table1.Amount" DbType="smallint" Type="System.Int32" 
             Expression="CAST(Table1.Amount as int)" />
    
  • Excellent! Thanks again for the help!

  • Slightly off topic, but this is something everyone should know when working with the Money type in SQLServer.

    You don't want to use a double, you want to use a decimal.

    A Double is a long float, and floating point arithmetic should never be used for financial calculations.

    Think of this, the fractions 1/3, 1/3 and 1/3 equal 1. However, when expressed as a double ie:

    .3333 + .3333 + .3333 = .9999 not 1.

    You may think that losing a thousandth of a cent is trivial, but its not when your working with someone elses money.

    Use System.Decimal.

    From FlySwat

Javascript and CSS parsing performance

I am trying to improve the performance of a web application. I have metrics that I can use to optimise the time taken to return the main HTML page, but I'm concerned about the external CSS and Javascript files that are included from these HTML pages. These are served statically, with HTTP Expires headers, but are shared between all the pages of the application.

I'm concerned that the browser has to parse these CSS and Javascript files for each page that is displayed and so having all the CSS and Javascript for the site shared into common files will negatively affect performance. Should I be trying to split out these files so I link from each page to only the CSS and Javascript needed for that page, or would I get little return for my efforts?

Are there any tools that could help me generate metrics for this?

  • I believe YSlow does, but be aware that unless all requests are over a loopback connection you shouldn't worry. The HTTP overhead of split-up files will impact performance far more than parsing, unless your CSS/JS files exceed several megabytes.

  • Context: While it's true that HTTP overhead is more significant than parsing JS and CSS, ignoring the impact of parsing on browser performance (even if you have less than a meg of JS) is a good way to get yourself in trouble.

    YSlow, Fiddler, and Firebug are not the best tools to monitor parsing speed. Unless they've been updated very recently, they don't separate the amount of time required to fetch JS over HTTP or load from cache versus the amount of time spent parsing the actual JS payload.

    Parse speed is slightly difficult to measure, but we've chased this metric a number of times on projects I've worked on and the impact on pageloads were significant even with ~500k of JS. Obviously the older browsers suffer the most...hopefully Chrome, TraceMonkey and the like help resolve this situation.

    Suggestion: Depending on the type of traffic you have at your site, it may be well worth your while to split up your JS payload so some large chunks of JS that will never be used on a the most popular pages are never sent down to the client. Of course, this means that when a new client hits a page where this JS is needed, you'll have to send it over the wire.

    However, it may well be the case that, say, 50% of your JS is never needed by 80% of your users due to your traffic patterns. If this is so, you should definitely user smaller, packaged JS payloads only on pages where the JS is necessary. Otherwise 80% of your users will suffer unnecessary JS parsing penalties on every single pageload.

    Bottom Line: It's difficult to find the proper balance of JS caching and smaller, packaged payloads, but depending on your traffic pattern it's definitely well worth considering a technique other than smashing all of your JS into every single pageload.

    From kamens
  • To add to kamen's great answer, I would say that on some browsers, the parse time for larger js resources grows non-linearly. That is, a 1 meg JS file will take longer to parse than two 500k files. So if a lot of your traffic is people who are likely to have your JS cached (return visitors), and all your JS files are cache-stable, it may make sense to break them up even if you end up loading all of them on every pageview.

    From levik

File format for generating dynamic reports in applications

We generate dynamic reports in all of our business web applications written for .Net and J2EE. On the server side we use ActiveReports.Net and JasperReports to generate the reports. We then export them to PDF to send down to the browser.

Our clients all use Adobe Reader. We have endless problems with the different versions of Adobe Reader and how they are setup on the client.

What file format/readers are others using for their dynamic reports? We need something that allows for precise layout as many of the reports are forms that are printed with data from out systems. HTML is not expressive enough.

  • I've used SQL Reporting Services for this purpose. You can design a report template in Visual Studio or generate the XML for the report on the fly in code. You can then have SSRS export the report to about 10 different formats and send to the client including pdf, excel, html, etc. You can also write your own plugin to export to your own format.

    Crystal Reports has a similar product thats more expensive but has a better report designer.

  • I've always had the most success using PDFs to accomplish this. I can't think of a more universally acceptable format that does what you are trying to do. Rather than looking for another format, perhaps it would be better to try to understand how to overcome the problems that you are experiencing with Acrobat on the client side. Can you provide some more information on the types of problems that you are experiencing with Acrobat?

    From Joe Barone
  • Please keep in mind this is a business intranet application. We require that PDFs be opened in the browser so we know when they navigate away to ask them if the report printed correctly or if they would like to save the report on the server for later viewing/printing. This is a client setting that we have no control over. Also several different version combinations of IE and Reader do not play well together. Sometimes the pdf won't even open depending on client settings. But if you copy the pdf out of the temporary internet files and open directly, it works fine. Maybe this is more of an IE thing, but we are stuck with that problem.

  • I does know only 3(4) possible viewer(formats) for reporting in browser.

    • PDF
    • Flash
    • Java
    • (Silverlihgt)

    For all 3 there are reporting solutions. Silverlight are to new and I does not know a solution. You can test how flash and Java in your intranet work and then search a reporting solution. I think PDF should be made the few problems if you use the newest readers. The old readers has many bad bugs.

    From Horcrux7