开发者

fetch data from DB and display it using stored procedure (asp.net)

In asp.net(C#) i want to fetch data from a database using a stored procedure and display them in controls. For example...

"Name" in textbox1

"contact" num i开发者_开发技巧n textbox2

Any help as I am new with this?

Thanks.


Check out some of those beginner's video series (some topics would be intermediate to advanced - find those of interest to you):

  • Data Access Videos
  • Data Access Tutorials

Marc


If you are using 3.5 sp1, then I suggest you try the entity framework. You can map a stored procedure over and it will behave in code just like any other C# function (in a very broad sense).

tutorial here: http://msdn.microsoft.com/en-us/library/bb386876.aspx


Stored Procedure:

USE [MyDB]
GO
/******Object: StoredProcedure [dbo].[GetNameByID] Script Date: 11/03/2009 07:10:10******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetNameByID] 
-- Add the parameters for the stored procedure here
@NameID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT dbo.Names.Name
    FROM dbo.MyDB
    WHERE dbo.Names.NameID=@NameID
END

DALC Class:

public static string GetNameByID(int nameID)
        {
            SqlParameter[] parameters = new SqlParameter[1];

            parameters[0] = new SqlParameter("@NameID", 
                System.Data.SqlDbType.Int, 8, "nameID");
                    parameters[0].Value = nameID;

            try
            {
                return (SqlHelper.ExecuteNonQuery(dbConnection, 
                    CommandType.StoredProcedure,
                        "GetNameByID", parameters));
            }
            catch
            {
                throw;
            }
            finally
            {
                dbConnection.Close();
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜