VS13/VS15 - can't import SQL snippet

旧时模样 提交于 2019-12-11 13:57:57

问题


I would like to create sql snippet in VS2013 and VS2015. I don't know why, but I've got an error while importing (in both of VS): "C:\sql.snippet: Missing or unspecified Language attribute"

My snippet:

<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>
            My Snippet
        </Title>
    </Header>
    <Snippet>
        <Code Language="SQL">
            <![CDATA[select * from MyTable]]>
        </Code>
    </Snippet>
</CodeSnippet>

What's wrong? The language "SQL" should be ok https://msdn.microsoft.com/en-US/library/ms171418.aspx#code

I tried to add this snippet to Language "Miscrosoft SQL Server Data Tools, T-SQl..." If I've changed the language attribute to e.g. "CSharp" it works.


回答1:


Instead of:

<Code Language="SQL">

Write:

<Code Language="SQL_SSDT">

Visual Studio supports only MSSQL Server Data Tools & T-SQL Language Scripts, not SQL like SSMS does.




回答2:


Try this.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>
                My Snippet
            </Title>
        </Header>
        <Snippet>
            <Code Language="SQL">
                <![CDATA[select * from MyTable]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

You could also check this out if it works but still gives a warning.

http://providersweb.blogspot.se/2016/01/visual-studio-snippets-missing-or.html

I have been getting a list of messages in the Snippets output window when I load a project that a number of snippets had a 'missing or unspecified language attribute' and could not figure out why these messages appeared. I had checked the Language attribute on the snippets and they were correct, but still got the message. I had set up folders for each language type, but still got the messages. I finally figured out that I had specified they all were VB snippets in the Snippet manager and those that were not VB would cause the messages in the output window, ie when loading a VB project, the CSharp snippets would generate the error message. I fixed this by going into the snippet manager and specifying the Language, VB or CSharp, and adding the snippets to the correct language and removing them from the invalid language. And now these messages of invalid language attributes do not appear on my output screen.




回答3:


The language attribute value you used, SQL, is correct. From the VS2015 Code Snippets Schema Reference:

Errors such as you're seeing can happen if you're editing a snippet file directly in one of the folders known by the Code Snippets Manager. Evidently Visual Studio holds its own metadata in addition to what's in the snippet XML itself. For this reason, it can be best to do the edits in a temporary folder and then use the Import button.

For example, I kept getting the error "Missing or unspecified Language attribute" when saving any edit to my new C# snippet in the My Code Snippets folder—or so I thought. Turned out I was in the Visual Basic\My Code Snippets folder, and there's also a Visual C#\My Code Snippets folder which is where I should have been. This was a silly mistake but it illustrates that the "Language" XML attribute isn't enough, as Visual Studio has its own internal conventions to do with different folders for different snippet types (and who knows what else) which all happens when the Import button is used.

I removed my snippet file to a temporary location, and then added it from there using the Import button. As soon as I did this, the problem went away and the snippet started working normally.



来源:https://stackoverflow.com/questions/38657900/vs13-vs15-cant-import-sql-snippet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!