Call/Use a SQL Function in C#

Step 1> Create A function

CREATE function [dbo].[function_xyz](@username varchar(200))
returns table
as
return (select [password] from [User] where username =@username)


Step 2> use function in your C# code..

SqlConnection conn=new SqlConnection(@"SQLCONNECTION STRING");
SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.function_xyz(@username)", conn);
       
       // cmd.CommandType=CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@username","username");
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        string str = dt.Rows[0][0].ToString();
        Response.Write(str.ToString());



Note--THis is only a process ,You have to modify as per your requirement 


Ebook Download
View all
Learn
View all