Dynamic SQL C # implementation.

Source: Internet
Author: User

Set ansi_nulls on

Set quoted_identifier on

Go

 

Alter proc [DBO]. [GB] @ table_name
Varchar (8)

 

Declare @ SQL nvarchar (1000)

Set @ SQL = 'update' + @ table_name + 'set
Id = '+ '000000' + 'where id =' + '000000'

-- Set @ SQL = 'update' + 'aa' + 'set
Id = '+ '000000' + 'where id =' + '000000'

Exec sp_executesql @ SQL with recompile

The above is the use of dynamic SQL statements for Stored Procedures

 

 

Private void form1_load (Object sender,
Eventargs E)

{


Sqlconnection conn = new
Sqlconnection ("Server = localhost; database = example; uid = example; Pwd = example ");


Try


{


Conn. open ();


Label1.text = "connection successful! ";


}


Catch (exception ex)


{


Throw (Ex );

 


}


String SQL = "Exec select_table ";


Dataset DS = new dataset ();


Sqldataadapter da = new sqldataadapter (SQL, Conn );


Da. Fill (DS );


Datagridview1.datasource = Ds. Tables [0];

 


Textbox1.text = (string) datagridview1.currentcell. value;


Int ROW = This. datagridview1.currentcell. rowindex;



For (INT I = row; I <maid-1; I ++)


{


Conn. Close ();


Row = 0;


This. Maid = This. Maid [0, I];

 



Listbox1.items. Add (this. datagridview1.currentcell. value );


String mysql1 = "Exec GB", mysql2;


Mysql2 = (string) This. datagridview1.currentcell. value;


Mysql1 + = mysql2;


MessageBox. Show (mysql1 );


Sqlcommand cmd = new sqlcommand ("GB", Conn );


Cmd. commandtype = commandtype. storedprocedure;


Sqlparameter sp = new sqlparameter ("@ table_name ",
Sqldbtype. varchar, 8 );


Sp. value = mysql2;


Cmd. Parameters. Add (SP );


Conn. open ();


Cmd. executenonquery ();



Mysql2 = "";


}


Conn. Close ();

 

}

 

Executes the Stored Procedure cyclically. I finally finished it. Haha. In the future, use C # to develop the database. Haha ....

 

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.