Delphi and SQL Server Stored Procedure Programming

Source: Internet
Author: User
Many beginners often ask how to call the stored procedure of SQL Server in Delphi? The problem is actually a good solution, but I don't want to answer it if I ask more. The following describes how to use an instance, from creating a stored procedure in SQL Server to calling a complete instance.

First, open the SQL Server Manager and create a test table in the pubs database. The table name is "test" and the fields include ID, name, and desc. All the tables are in the metadata type. If you do not know how to create a table, then open the SQL query analyzer and paste the followingCodeAnd then, the test table is automatically generated.

Use pubs

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [test] ') and objectproperty (ID, n'isusertable') = 1)

Drop table [DBO]. [test]

Go

Create Table [DBO]. [test] (

[ID] [char] (10) Collate chinese_prc_ci_as not null,

[Name] [char] (12) Collate chinese_prc_ci_as null,

[Descrip] [char] (30) Collate chinese_prc_ci_as null

) On [primary]

Go

Create a stored procedure to insert a new record to test. the code for creating a stored procedure is as follows. Similarly, you can copy it to the query analyzer and execute it directly:

Create procedure myinsert

@ ID char (10 ),

@ Name varchar (12 ),

@ Descrip varchar (30)

As

Begin

Insert into test (ID, name, descrip) values (@ ID, @ name, @ descrip)

If @ rowcount = 0

Begin

Raiserror ('error', 16, 1)

Rollback transaction

End

End

Go

Next, create a new project file, place the following controls on form1, and set properties (in parentheses ):

One adoconnection1: tadoconnection;

(Loginprompt: = false;

Connectionstring: = provider = sqloledb.1; persist Security info = true; user id = sa; initial catalog = pubs; Data Source = (local );)

An adostoredproc1: tadostoredproc; attribute:

(Connection: = adoconnection1;

Procedurename: = myinsert; // The one we created above)

An adotable1: tadotable; attribute:

(Connection: = adoconnection1;

Tablename: = test; // The one we created above)

A performance1 property:

(Dataset: = tadotable ;)

One dbgrid1: TDBGrid; attribute:

(Datasource: = performance1 ;)

A button1, written in its onclick:

With adostoredproc1 do

Begin

Parameters. parambyname ('@ id'). Value: = '2 ';

Parameters. parambyname ('@ name'). Value: = 'myname ';

Parameters. parambyname ('@ descrip'). Value: = 'nosubobject ';

Execproc;

End;

Adotable1.close;

Adotable1.open;

Related Article

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.