验证中...
Languages: ASP
Categories: 数据库相关
Latest update 2018-12-25 08:55
调用数据库中的存储过程
Raw Copy
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sqlconn = "Data Source=YF616057\\LHB;Initial Catalog=SuperMarket;Integrated Security=True";
SqlConnection myConnection = new SqlConnection(sqlconn);
//打开数据库连接
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandType = CommandType.StoredProcedure;
//up_InsertData为存储过程的名字
myCommand.CommandText = "byType";
//创建要传递给存储过程的参数
SqlParameter parInput = myCommand.Parameters.Add("@type", SqlDbType.SmallMoney);
parInput.Direction = ParameterDirection.Input;
parInput.Value = 2;
//把存储过程执行的结果保存到SqlDataReader
SqlDataReader myReader = myCommand.ExecuteReader();
//获取数据之前,必须不断的调用Read方法,它负责前进到下一条记录.
Response.Write("<h3>使用存储过程查询数据</h3><hr>");
Response.Write("<table border=1 cellspacing=0 cellpadding=2>");
//显示列名字
Response.Write("<tr bgcolor=#DAB4B4>");
for (int i = 0; i < myReader.FieldCount; i++)
{
Response.Write("<td>" + myReader.GetName(i) + "</td>");
}
Response.Write("</tr>");
//输出所有的字段值
while (myReader.Read())
{
Response.Write("<tr>");
//打印取到的数据
for (int i = 0; i < myReader.FieldCount; i++)
{
Response.Write("<td>" + myReader[i].ToString() + "</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
//关闭SqlDataReader
myReader.Close();
//关闭与数据库的连接
myConnection.Close();
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search

Gitee_you_jiang_zheng_wen Zheng_wen_close