如何在C#程序中執(zhí)行存儲(chǔ)過(guò)程我想從C#程序中執(zhí)行這個(gè)存儲(chǔ)過(guò)程。我在SQLSERVER查詢窗口中編寫了以下存儲(chǔ)過(guò)程,并將其保存為Store 1: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..我需要提供這條路嗎?如果是,應(yīng)將存儲(chǔ)過(guò)程存儲(chǔ)在哪個(gè)位置?
如何在C#程序中執(zhí)行存儲(chǔ)過(guò)程
呼啦一陣風(fēng)
2019-06-14 10:29:04