这例子很好,慢慢研究!
<%@Page Language="C#"%><%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Using Explicit Parameters with a Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Using Explicit Parameters with a Stored Procedure</span><hr />
<!---------------------------------------------------------------------------><%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/><div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>Command Text: <b><span id="outCommandText" runat="server"></span></b></div>
<div>Input Parameter: <b><span id="outInParams" runat="server"></span></b></div>
<div>Output Parameters: <b><span id="outOutParams" runat="server"></span></b></div>
<div id="outError" runat="server"> </div><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e)
{ // get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
outConnect.InnerText = strConnect; // and display it // specify the name of the stored procedure to use as the CommandText
string strCommandText = "FindFromISBN";
outCommandText.InnerText = strCommandText; // and display it // create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect); // create a new Command object and set the CommandType
OleDbCommand objCommand = new OleDbCommand(strCommandText, objConnect);
objCommand.CommandType = CommandType.StoredProcedure; // create a variable to hold a Parameter object
OleDbParameter objParam; // create a new Parameter object named 'ISBN' with the correct data
// type to match a SQL database 'varchar' field of 12 characters
objParam = objCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12); // specify that it// s an Input parameter and set the value
objParam.Direction = ParameterDirection.Input;
objParam.Value = "1861002882"; // create a new Parameter object named 'Title' with the correct data
// type to match a SQL database 'varchar' field of 50 characters
// and specify that it's an output parameter (so no value required)
objParam = objCommand.Parameters.Add("Title", OleDbType.VarChar, 50);
objParam.Direction = ParameterDirection.Output; // create another output Parameter object named 'Date' with the correct
// data type to match a SQL database 'datetime' field
objParam = objCommand.Parameters.Add("Date", OleDbType.DBDate);
objParam.Direction = ParameterDirection.Output; // display the value of the input parameter
outInParams.InnerText = "ISBN='" + objCommand.Parameters["ISBN"].Value + "'"; try
{
// open the connection and execute the command
objConnect.Open();
objCommand.ExecuteNonQuery();
objConnect.Close(); // then close the connection
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
} // collect the values of the output parameters - note the use of
// the ToString method as they will contain DBNull if there was no
// match for the ISBN and this will cause an error if displayed
string strTitle = objCommand.Parameters["Title"].Value.ToString();
string strDate = objCommand.Parameters["Date"].Value.ToString();
outOutParams.InnerHtml = "Title='" + strTitle + "' Date=" + strDate; }
</script><!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
<%@Page Language="C#"%><%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Using Explicit Parameters with a Stored Procedure</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Using Explicit Parameters with a Stored Procedure</span><hr />
<!---------------------------------------------------------------------------><%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/><div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>Command Text: <b><span id="outCommandText" runat="server"></span></b></div>
<div>Input Parameter: <b><span id="outInParams" runat="server"></span></b></div>
<div>Output Parameters: <b><span id="outOutParams" runat="server"></span></b></div>
<div id="outError" runat="server"> </div><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e)
{ // get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
outConnect.InnerText = strConnect; // and display it // specify the name of the stored procedure to use as the CommandText
string strCommandText = "FindFromISBN";
outCommandText.InnerText = strCommandText; // and display it // create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect); // create a new Command object and set the CommandType
OleDbCommand objCommand = new OleDbCommand(strCommandText, objConnect);
objCommand.CommandType = CommandType.StoredProcedure; // create a variable to hold a Parameter object
OleDbParameter objParam; // create a new Parameter object named 'ISBN' with the correct data
// type to match a SQL database 'varchar' field of 12 characters
objParam = objCommand.Parameters.Add("ISBN", OleDbType.VarChar, 12); // specify that it// s an Input parameter and set the value
objParam.Direction = ParameterDirection.Input;
objParam.Value = "1861002882"; // create a new Parameter object named 'Title' with the correct data
// type to match a SQL database 'varchar' field of 50 characters
// and specify that it's an output parameter (so no value required)
objParam = objCommand.Parameters.Add("Title", OleDbType.VarChar, 50);
objParam.Direction = ParameterDirection.Output; // create another output Parameter object named 'Date' with the correct
// data type to match a SQL database 'datetime' field
objParam = objCommand.Parameters.Add("Date", OleDbType.DBDate);
objParam.Direction = ParameterDirection.Output; // display the value of the input parameter
outInParams.InnerText = "ISBN='" + objCommand.Parameters["ISBN"].Value + "'"; try
{
// open the connection and execute the command
objConnect.Open();
objCommand.ExecuteNonQuery();
objConnect.Close(); // then close the connection
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
} // collect the values of the output parameters - note the use of
// the ToString method as they will contain DBNull if there was no
// match for the ISBN and this will cause an error if displayed
string strTitle = objCommand.Parameters["Title"].Value.ToString();
string strDate = objCommand.Parameters["Date"].Value.ToString();
outOutParams.InnerHtml = "Title='" + strTitle + "' Date=" + strDate; }
</script><!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
解决方案 »
- ASP.NET 的GridView,如何证“编辑”列放到后面去?
- 对于搜索出来的结果不能翻页
- ajax(js)判断文本框输入的用户是否存在
- 有关Visual Web Developer 2005的问题!!!
- 请高手回答数据库连接问题
- 百分求助 AspnetMenu菜单项为何不能用中文呢?
- datagird问题,请高手指教!!!
- 新手求助 “ASP.index_aspx”不包含“AdCreated_Event”的定义
- 为什么得到模板列控件的值都是一样的....急急急...
- vs2017安装成功可以用,过来2天打不开,只能用命令devenv/Safemode打开,
- 如何设置 COOKIE 过期的时间呢? TimeSpan(0,0,720,0)干什么用的。
- datalist怎么new一个新行?
Dim myCommand As SqlClient.SqlCommand
Dim myReader As SqlClient.SqlDataReader
Dim myOutput As ParameterDirection
Dim Sql As String
Dim ConnStr As String = Application("Connt_Login") '数据库连接串
myConn = New SqlClient.SqlConnection(ConnStr)
myConn.Open()
myCommand = New SqlClient.SqlCommand("online", myConn)
myCommand.CommandType = System.Data.CommandType.StoredProcedure
myCommand.Parameters.Add("@S_Id", SqlDbType.Char).Value = "111"
myCommand.Parameters.Add("@P_Id", SqlDbType.Char).Value = "-100"
myCommand.Parameters.Add("@Lb", SqlDbType.Char).Value = "T"
myCommand.Parameters.Add("@CountUser", SqlDbType.Int).Value = 0 myReader = myCommand.ExecuteReader
myCommand = New SqlClient.SqlCommand("online", myConn)
这个online是不是sql server中的存储过程?