Asp.net calls the Stored Procedure

Source: Internet
Author: User

The stored procedure is as follows:
Create procedure pr_addfile
@ Filename varchar (100 ),
@ Fileurl varchar (200 ),
@ Filekind varchar (100)
As
Declare @ recordcount as int
Set @ recordcount = (select count (*) from file1 where filename = @ filename)
If @ recordcount <1
Begin
Insert into file1 ([filename], fileurl, filekind) values (@ filename, @ fileurl, @ filekind)
Return @ identity
End
Go

Configuration File
<Deleetask>
<Add key = "connectionstring" value = "Server = wcj; database = files; uid = sa; Pwd ="/>
</Appsettings>

specific implementation process
string strconn = system. configuration. configurationmanager. appsettings ["connectionstring"]. tostring ();
try
{< br> sqlconnection conn = new sqlconnection (strconn);
Conn. open ();
sqlcommand cmd = new sqlcommand ();
cmd. commandtype = commandtype. storedprocedure; // The Stored Procedure of the database
cmd. commandtext = "pr_addfile"; // name of the stored procedure
cmmd. connection = conn;

// set of parameters
sqlparameter [] parameters = {
New sqlparameter ("@ FILENAME", sqldbtype. varchar, 100),
New sqlparameter ("@ fileurl", sqldbtype. varchar, 200),
New sqlparameter ("@ filekind", sqldbtype. varchar, 200),
New sqlparameter ("@ returnvalue", sqldbtype. int, 4)
};

// assign a value to each parameter
parameters [0]. value = sfilename. tostring ();
parameters [1]. value = sfileurl. tostring ();
parameters [2]. value = sfilekind. tostring ();
parameters [3]. direction = parameterdirection. returnvalue;
foreach (sqlparameter parameter in parameters)
{< br> cmmd. parameters. add (parameter);
}

// Execute the Stored Procedure
Cmmd. executenonquery ();
Int parameterfileid = int. parse (cmmd. Parameters ["@ returnvalue"]. value. tostring (); // return Parameter
Return parameterfileid;
}
Catch (exception ex)
{
Throw new exception ("database connection failed! ", Ex );
}
Finally
{
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.