C# でストアド プロシージャを使用したい。SQL Server でストアド プロシージャを作成し、プログラムで呼び出します。しかし、ブレークポイント機能を使用すると、ブレークポイントがループをスキップするため、データベースからデータが取得されないことがわかります..
.aspx コード:
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="store" />
<asp:Label ID="Label9" runat="server" Text="Label"></asp:Label>
c# コード:
public void store(object sender, EventArgs ser)
{
try
{
// c reate and open a connection object
SqlConnection conn = Class3.GetConnection();
// 1. create a command object identifying the stored procedure
SqlCommand cmd = new SqlCommand("storeprocedure3", conn);
// 2. set the command object so it knows to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
// 3. add parameter to command, which will be execute the command
SqlDataReader rdr = cmd.ExecuteReader();
// iterate through results, printing each to console
while (rdr.Read())
{
Label9.Text = rdr["menuename"].ToString();
}
}
catch (Exception sa)
{
Console.WriteLine(sa);
}
}
ストアド プロシージャ:
CREATE PROCEDURE procedure3
AS
BEGIN
select menuename from menue;
END
GO