
ASP.NET要如何调用数据库的存储过程?
3个回答
展开全部
其实调用存储过程和调用普通的sql语句的过程是一样的。
1.创建连接字符串string SqlConnectionString="";
2.实例化连接SqlConnection cn = new SqlConnectio(SqlConnectionString);
3.SqlCommand cmd = new SqlCommand("存储过程的名字", cn);
4.执行命令cmd.cmd.ExecuteNonQuery();(这个方法根据你的存储过程而定)
应该这样就OK啦!
1.创建连接字符串string SqlConnectionString="";
2.实例化连接SqlConnection cn = new SqlConnectio(SqlConnectionString);
3.SqlCommand cmd = new SqlCommand("存储过程的名字", cn);
4.执行命令cmd.cmd.ExecuteNonQuery();(这个方法根据你的存储过程而定)
应该这样就OK啦!
展开全部
private void LoadData1()
{
string Strconn=ConfigurationSettings.AppSettings["StrConn"].ToString();
SqlConnection nwConn=new SqlConnection(Strconn);
SqlCommand SearchData=new SqlCommand("ProductData1",nwConn);
SearchData.CommandType=CommandType.StoredProcedure;
SearchData.Parameters.Add("@ProductName1",SqlDbType.VarChar,50);
SearchData.Parameters["@ProductName1"].Direction=ParameterDirection.Output;
SearchData.Parameters.Add("@ProductType1",SqlDbType.VarChar,50);
SearchData.Parameters["@ProductType1"].Direction=ParameterDirection.Output;
SearchData.Parameters.Add("@ProductPrice1",SqlDbType.VarChar,50);
SearchData.Parameters["@ProductPrice1"].Direction=ParameterDirection.Output;
nwConn.Open();
SearchData.ExecuteNonQuery();
SearchData.Dispose();
nwConn.Close();
Product1_Name.Text=SearchData.Parameters["@ProductName1"].Value.ToString();
Product1_Type.Text=SearchData.Parameters["@ProductType1"].Value.ToString();
Product1_Price.Text=SearchData.Parameters["@ProductPrice1"].Value.ToString();
}
存储过程函数部分
CREATE PROCEDURE ProductData1
@ProductName1 varchar(50) output,
@ProductType1 varchar(50) output,
@ProductPrice1 varchar(50) output
Begin
set @ProductName1=(Select Top 1 ProductName From Products1 order by CreateTime DESC);
set @ProductType1=(Select Top 1 ProductType From Products1 order by CreateTime DESC);
set @ProductPrice1=(Select Top 1 ProductPrice From Products1 order by CreateTime DESC);
End
{
string Strconn=ConfigurationSettings.AppSettings["StrConn"].ToString();
SqlConnection nwConn=new SqlConnection(Strconn);
SqlCommand SearchData=new SqlCommand("ProductData1",nwConn);
SearchData.CommandType=CommandType.StoredProcedure;
SearchData.Parameters.Add("@ProductName1",SqlDbType.VarChar,50);
SearchData.Parameters["@ProductName1"].Direction=ParameterDirection.Output;
SearchData.Parameters.Add("@ProductType1",SqlDbType.VarChar,50);
SearchData.Parameters["@ProductType1"].Direction=ParameterDirection.Output;
SearchData.Parameters.Add("@ProductPrice1",SqlDbType.VarChar,50);
SearchData.Parameters["@ProductPrice1"].Direction=ParameterDirection.Output;
nwConn.Open();
SearchData.ExecuteNonQuery();
SearchData.Dispose();
nwConn.Close();
Product1_Name.Text=SearchData.Parameters["@ProductName1"].Value.ToString();
Product1_Type.Text=SearchData.Parameters["@ProductType1"].Value.ToString();
Product1_Price.Text=SearchData.Parameters["@ProductPrice1"].Value.ToString();
}
存储过程函数部分
CREATE PROCEDURE ProductData1
@ProductName1 varchar(50) output,
@ProductType1 varchar(50) output,
@ProductPrice1 varchar(50) output
Begin
set @ProductName1=(Select Top 1 ProductName From Products1 order by CreateTime DESC);
set @ProductType1=(Select Top 1 ProductType From Products1 order by CreateTime DESC);
set @ProductPrice1=(Select Top 1 ProductPrice From Products1 order by CreateTime DESC);
End
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection conn=new SqlConnection(connstr);
SqlCommand cmd=new SqlCommand("存储过程名",conn);
cmd.CommandType=CommandType.StoreProcedure;
SqlCommand cmd=new SqlCommand("存储过程名",conn);
cmd.CommandType=CommandType.StoreProcedure;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询