Skip to content

Instantly share code, notes, and snippets.

@swkwon
Created December 31, 2013 07:56
Show Gist options
  • Save swkwon/8193846 to your computer and use it in GitHub Desktop.
Save swkwon/8193846 to your computer and use it in GitHub Desktop.
SQL Server custom function, custom stored procedure
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
namespace SqlServerProject5
{
public class Class1
{
[SqlFunction]
public static SqlInt32 GetSquare(SqlInt32 param)
{
return param * param;
}
[SqlProcedure]
public static void InsertUser(SqlString name, SqlInt32 score)
{
string insertQuery = "INSERT INTO myUser(user_name, user_score) VALUES(@name, @score)";
using (SqlConnection conn = new SqlConnection(@"context connection=true"))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(insertQuery, conn))
{
cmd.Parameters.Add("@name", System.Data.SqlDbType.VarChar, 50);
cmd.Parameters.Add("@score", System.Data.SqlDbType.Int);
cmd.Parameters[0].Value = name;
cmd.Parameters[1].Value = score;
cmd.ExecuteNonQuery();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment