如何编写一个存储过程,其输入参数是从下拉列表中进行选择并将结果传递到空白文本框中?
How to write a stored procedure whose input parameter is a selection from a drop down list & pass the result into a blank text box?
这是我在 SQL Management studio 中成功执行的程序,但我无法在 visual studio 中执行到 运行。当我 运行 它在 visual studio 它不会让我 select 输入参数所以我想 运行 它输入参数是用户 selects 来自应用程序中的下拉列表。
程序如下:
Create PROCEDURE [dbo].[GetLastCustomerID_CustomerTypeSelect]
(@CustomerType varchar(20)= Null)
-- Add the parameters for the stored procedure here
--@CustomerType varchar = 'Norcross'
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Select case @CustomerType
When 'Norcross'
Then (SELECT MAX(custnum)
from TSarCustomer
where dbo.ParseAlphaChars(CustNum) between 25000 and 79999 and OpenedDate >
dateadd(d,-120,GETDATE()))
When 'Gaffey'
Then(SELECT MAX(custnum)
from TSarCustomer
where (dbo.ParseAlphaChars(CustNum) between 80000 and 150000) and OpenedDate > dateadd(d,-120,GETDATE()))
When 'CranePro'
then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%K' and OpenedDate > '1/1/2012')
When 'CranePro'
then(SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%C' and OpenedDate > '1/1/2005')
When 'Internet'
Then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%W' and OpenedDate > '1/1/2012')
When 'M'
Then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%M' and OpenedDate > '1/1/2012')
end
end
这是我的下拉代码,该下拉菜单的选项是从 table 中提取的。
<tr>
<td class="auto-style3">Customer Type:</td>
<td colspan="1" class="auto-style4">
<asp:DropDownList ID="DropDownListCustomerType" AutoPostBack="true" runat="server" DataSourceID="SqlDataSourceCustomerType" DataTextField="CustomerType" DataValueField="CustomerType" Height="22px" Width="113px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCustomerType" runat="server" ConnectionString="<%$ ConnectionStrings:AceWOMConnectionString %>" SelectCommand="SELECT * FROM [CustomeryType]"></asp:SqlDataSource>
</td>
<td class="auto-style3" >
Assign Take-Stock#:</td>
<td class="auto-style3">
<asp:TextBox ID="TSCustomerNumber" runat="server">
</asp:TextBox>
</td>
</tr>
所以我需要帮助弄清楚如何在用户选择下拉菜单时 运行 存储过程,从下拉菜单中取出 selection,将其传递给存储过程作为输入参数,然后 post 将过程的结果放入由 ID "TSCustomerNumber"
标记的空文本框中
我们将不胜感激任何帮助!
在您的下拉列表中使用 onselectedindexchanged 事件处理程序。
onselectedindexchanged="itemSelected"
然后在后面的代码中创建一个方法来处理事件并将下拉列表值作为参数获取,调用 SP 传入参数,完成后更新文本框:
protected void itemSelected(object sender, EventArgs e)
{
var val = DropDownListCustomerType.SelectedItem.Value;
using (SqlConnection con = new SqlConnection(myDataConnection)) {
using (SqlCommand cmd = new SqlCommand("GetLastCustomerID_CustomerTypeSelect", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerType", SqlDbType.VarChar).Value = val;
con.Open();
TSCustomerNumber.Text = (String) cmd.ExecuteScalar();
}
}
}
那至少应该让你接近...
string query = "select Fechacumplimiento from TTareasIngreso where descripcion='" + dprTarea.Text + "' and descripcion='" + lblagencia.Text + "' ";
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TxtFechaPago.Text = dr["Fechacumplimiento"].ToString();
}
}
这是我在 SQL Management studio 中成功执行的程序,但我无法在 visual studio 中执行到 运行。当我 运行 它在 visual studio 它不会让我 select 输入参数所以我想 运行 它输入参数是用户 selects 来自应用程序中的下拉列表。
程序如下:
Create PROCEDURE [dbo].[GetLastCustomerID_CustomerTypeSelect]
(@CustomerType varchar(20)= Null)
-- Add the parameters for the stored procedure here
--@CustomerType varchar = 'Norcross'
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Select case @CustomerType
When 'Norcross'
Then (SELECT MAX(custnum)
from TSarCustomer
where dbo.ParseAlphaChars(CustNum) between 25000 and 79999 and OpenedDate >
dateadd(d,-120,GETDATE()))
When 'Gaffey'
Then(SELECT MAX(custnum)
from TSarCustomer
where (dbo.ParseAlphaChars(CustNum) between 80000 and 150000) and OpenedDate > dateadd(d,-120,GETDATE()))
When 'CranePro'
then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%K' and OpenedDate > '1/1/2012')
When 'CranePro'
then(SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%C' and OpenedDate > '1/1/2005')
When 'Internet'
Then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%W' and OpenedDate > '1/1/2012')
When 'M'
Then (SELECT MAX(dbo.ParseAlphaChars(CustNum))
from TSarCustomer
where CustNum like '%M' and OpenedDate > '1/1/2012')
end
end
这是我的下拉代码,该下拉菜单的选项是从 table 中提取的。
<tr>
<td class="auto-style3">Customer Type:</td>
<td colspan="1" class="auto-style4">
<asp:DropDownList ID="DropDownListCustomerType" AutoPostBack="true" runat="server" DataSourceID="SqlDataSourceCustomerType" DataTextField="CustomerType" DataValueField="CustomerType" Height="22px" Width="113px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCustomerType" runat="server" ConnectionString="<%$ ConnectionStrings:AceWOMConnectionString %>" SelectCommand="SELECT * FROM [CustomeryType]"></asp:SqlDataSource>
</td>
<td class="auto-style3" >
Assign Take-Stock#:</td>
<td class="auto-style3">
<asp:TextBox ID="TSCustomerNumber" runat="server">
</asp:TextBox>
</td>
</tr>
所以我需要帮助弄清楚如何在用户选择下拉菜单时 运行 存储过程,从下拉菜单中取出 selection,将其传递给存储过程作为输入参数,然后 post 将过程的结果放入由 ID "TSCustomerNumber"
标记的空文本框中我们将不胜感激任何帮助!
在您的下拉列表中使用 onselectedindexchanged 事件处理程序。
onselectedindexchanged="itemSelected"
然后在后面的代码中创建一个方法来处理事件并将下拉列表值作为参数获取,调用 SP 传入参数,完成后更新文本框:
protected void itemSelected(object sender, EventArgs e)
{
var val = DropDownListCustomerType.SelectedItem.Value;
using (SqlConnection con = new SqlConnection(myDataConnection)) {
using (SqlCommand cmd = new SqlCommand("GetLastCustomerID_CustomerTypeSelect", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@CustomerType", SqlDbType.VarChar).Value = val;
con.Open();
TSCustomerNumber.Text = (String) cmd.ExecuteScalar();
}
}
}
那至少应该让你接近...
string query = "select Fechacumplimiento from TTareasIngreso where descripcion='" + dprTarea.Text + "' and descripcion='" + lblagencia.Text + "' "; SqlCommand cmd = new SqlCommand(query, conn); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { TxtFechaPago.Text = dr["Fechacumplimiento"].ToString(); } }