Saturday, February 25, 2012

Access SQL functions through .net?

I usually access stored procedures using SQL data source. But now I need a string returned from the database. If I write a function in SQL how do I access it from an aspx.vb file?

Put it in a proc and run an EXEC query, same way as any other procedure.

Jeff

|||

If the SQL function returns a string, why not use SqlCommand.ExecuteScalar method:

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ToString()))
{
conn.Open();

string qstring = "SELECT dbo.fn_test('IORI')";

SqlCommand cmd = new SqlCommand(qstring, conn);
string s=cmd.ExecuteScalar().ToString();

Response.Write("The new string is:" + s);
}

|||Cool! Thanks guys!

No comments:

Post a Comment