Created
March 25, 2017 12:47
-
-
Save tayyebi/28148f692f515712e5f1f9969ba93924 to your computer and use it in GitHub Desktop.
SQL Execute and SQL Read commands snippet for Visual Studio C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<CodeSnippets | |
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>Transact SQL Execute</Title> | |
<Author>MohammadReza Tayyebi github.com/tayyebi</Author> | |
<Description>Creates SQL Execute</Description> | |
<Shortcut>tsqlexec</Shortcut> | |
</Header> | |
<Snippet> | |
<Imports> | |
<Import> | |
<Namespace>System.Data.SqlClient</Namespace> | |
</Import> | |
<Import> | |
<Namespace>System.Configuration</Namespace> | |
</Import> | |
</Imports> | |
<Declarations> | |
<Literal> | |
<ID>theCON</ID> | |
<ToolTip>Replace with your connection name</ToolTip> | |
<Default>con</Default> | |
</Literal> | |
<Literal> | |
<ID>theCOM</ID> | |
<ToolTip>Replace with your command name</ToolTip> | |
<Default>com</Default> | |
</Literal> | |
<Literal> | |
<ID>theCS</ID> | |
<ToolTip>Replace with your connection string name</ToolTip> | |
<Default>defaultConnectionString</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="CSharp"> | |
<![CDATA[ | |
SqlConnection $theCON$ = new SqlConnection(); | |
$theCON$.ConnectionString = ConfigurationManager.ConnectionStrings["$theCS$"].ConnectionString; | |
$theCON$.Open(); | |
SqlCommand $theCOM$ = new SqlCommand(); | |
$theCOM$.Connection = $theCON$; | |
$theCOM$.CommandText = "INSERT INTO theTABLE (Field1) VALUES (@Value1)"; | |
$theCOM$.Parameters.AddWithValue("Value1", theValue1); | |
$theCOM$.ExecuteNonQuery(); | |
]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<CodeSnippets | |
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>Transact SQL Read</Title> | |
<Author>MohammadReza Tayyebi github.com/tayyebi</Author> | |
<Description>Creates an SQL read</Description> | |
<Shortcut>tsqlread</Shortcut> | |
</Header> | |
<Snippet> | |
<Imports> | |
<Import> | |
<Namespace>System.Data.SqlClient</Namespace> | |
</Import> | |
<Import> | |
<Namespace>System.Configuration</Namespace> | |
</Import> | |
</Imports> | |
<Declarations> | |
<Literal> | |
<ID>theCON</ID> | |
<ToolTip>Replace with your connection name</ToolTip> | |
<Default>con</Default> | |
</Literal> | |
<Literal> | |
<ID>theCOM</ID> | |
<ToolTip>Replace with your command name</ToolTip> | |
<Default>com</Default> | |
</Literal> | |
<Literal> | |
<ID>theCS</ID> | |
<ToolTip>Replace with your connection string name</ToolTip> | |
<Default>defaultConnectionString</Default> | |
</Literal> | |
<Literal> | |
<ID>theDATA</ID> | |
<ToolTip>Replace with your data reader name</ToolTip> | |
<Default>data</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="CSharp"> | |
<![CDATA[ | |
SqlConnection $theCON$ = new SqlConnection(); | |
$theCON$.ConnectionString = ConfigurationManager.ConnectionStrings["$theCS$"].ConnectionString; | |
$theCON$.Open(); | |
SqlCommand $theCOM$ = new SqlCommand(); | |
$theCOM$.Connection = $theCON$; | |
$theCOM$.CommandText = "SELECT 'Hello world' as MyField1 WHERE @Value1 = 1"; | |
$theCOM$.Parameters.AddWithValue("Value1", theValue1); | |
SqlDataReader $theDATA$ = com.ExecuteReader(); | |
while($theDATA$.Read()) | |
{ | |
string Requested = $theDATA$["MyField1"].ToString(); | |
} | |
]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment