Database Reference
In-Depth Information
parmCurrencyCode.Value = currencyCode;
parmCurrencyName.Value = currencyName;
cmdInsertCurrency.Parameters.Add(parmCurrencyCode);
cmdInsertCurrency.Parameters.Add(parmCurrencyName);
cmdInsertCurrency.CommandText =
"INSERT Sales.Currency (CurrencyCode, CurrencyName, ModifiedCurrencyDate)" +
" VALUES(@CCode, @Name, GetDate())";
conn.Open();
cmdInsertCurrency.ExecuteNonQuery();
}
catch (SqlException ex)
{
SqlContext.Pipe.Send("An error occured" + ex.Message + ex.StackTrace);
}
finally
{
conn.Close();
}
}
6. Save the project, and build the solution. After a successful build, it will produce
a Chapter20.dll file under the project's \bin\debug folder.
How It Works
Since this is a stored procedure coded in C#, it will be inserting currency data into the
AdventureWorks.Sales.Currency table, which has three columns. Of those, you will be passing values for
two as input parameters.
[Microsoft.SqlServer.Server.SqlProcedure()]
public static void InsertCurrency_CS(SqlString currencyCode, SqlString currencyName)
The most important part of any database application is creating a connection and a command.
SqlConnection conn = null;
conn = new SqlConnection(@"server = .\sql2012;integrated security = true;
database = AdventureWorks");
SqlCommand cmdInsertCurrency = new SqlCommand();
cmdInsertCurrency.Connection = conn;
Once you have connection and command objects, you need to set the parameters that this stored
procedure will be accepting.
 
Search WWH ::




Custom Search