如何在C#程序中执行存储过程
use master go create procedure dbo.test asDECLARE @command as varchar(1000), @i intSET @i = 0WHILE @i < 5BEGINPrint 'I VALUE ' +CONVERT(varchar(20),@i) EXEC(@command)SET @i = @i + 1END
编辑:
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;namespace AutomationApp{ class Program { public void RunStoredProc() { SqlConnection conn = null; SqlDataReader rdr = null; Console.WriteLine("\nTop 10 Most Expensive Products:\n"); try { conn = new SqlConnection("Server=(local);DataBase=master;Integrated Security=SSPI"); conn.Open(); SqlCommand cmd = new SqlCommand("dbo.test", conn); cmd.CommandType = CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); /*while (rdr.Read()) { Console.WriteLine( "Product: {0,-25} Price: ${1,6:####.00}", rdr["TenMostExpensiveProducts"], rdr["UnitPrice"]); }*/ } finally { if (conn != null) { conn.Close(); } if (rdr != null) { rdr.Close(); } } } static void Main(string[] args) { Console.WriteLine("Hello World"); Program p= new Program(); p.RunStoredProc(); Console.Read(); } }}
Cannot find the stored procedure dbo.test
温温酱
心有法竹
相关分类