【问题标题】:Random Generator To Use Values From SQL Server DB随机生成器使用 SQL Server DB 中的值
【发布时间】:2015-10-23 12:56:10
【问题描述】:

我有一个asp.net 应用程序,它是地点的随机生成器。

目前我的值位于我的代码后面,但我想将这些值移动到我的 SQL Server 数据库中,但我不知道如何执行此操作。作为参考,我使用的是 SQL Server Management Studio。

这是可能的还是我把它复杂化了?

代码背后

protected void BtnDecideForMe_Click(object sender, EventArgs e)
{
    List<string> Eat = new List<string>();
    Eat.Add("Chinese Buffet");
    Eat.Add("Harvester");
    Eat.Add("Frankie & Benny's");
    Eat.Add("Hungry Horse");
    Eat.Add("Blaize");
    Eat.Add("Chiquito");
    Eat.Add("Cafe Football");
    Eat.Add("Nando's");

    Random Place = new Random();
    int Places = Place.Next(0, Eat.Count);
    txtDestination.Text = Eat[Places];
}

查看

<div class="row">
    <div class="col-sm-3">
        <asp:TextBox class="form-control" ID="txtDestination" runat="server" disabled></asp:TextBox>
    </div>
    <div class="col-sm-2">
        <asp:Button class="btn btn-success" ID="BtnDecideForMe" runat="server" Text="Decide For Me" OnClick="BtnDecideForMe_Click" />
    </div>
</div>

提供建议但无法正常工作的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace PaydayLunchGenerator
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnDecideForMe_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString =
            "Data Source=DEV-116\\ONLINE;" +
            "Initial Catalog=PaydayLunch;" +
            "Integrated Security=True;";
            conn.Open();

            //using (SqlConnection conn = new SqlConnection(PaydayLunchConnectionString1))
            using (SqlCommand cmd = new SqlCommand("dbo.GetRandomPlace", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                // set up the parameters
                cmd.Parameters.Add("@OutputVar", SqlDbType.VarChar).Direction = ParameterDirection.Output;

                // open connection and execute stored procedure
                conn.Open();
                cmd.ExecuteNonQuery();

                // read output value from @OutputVar
                string place = Convert.ToString(cmd.Parameters["@OutputVar"].Value);
                conn.Close();

                txtDestination.Text = place;
            }
        }
    }
}

【问题讨论】:

  • 大概有多少个值?
  • @Alec 我帖子中的所有内容以及其他任何人建议的内容,因此此列表可能会增加。我确实还有另外 8 个要添加到我的列表中,但还没有
  • 我添加了一个答案来创建一个表格并为你插入这些东西。

标签: c# asp.net sql-server


【解决方案1】:

您可以通过在 SQL Server 中创建视图并将该视图加载到数据集中来实现此目的。这样您就可以在需要时从数据集中进行选择并刷新数据。

Populating Dataset

注意 - 您甚至可以更进一步,创建一个存储过程,它只会根据需要从表中为您提供一个随机值 :)

创建一个带有输出变量的存储过程,然后在里面创建一个这样的选择

 CREATE PROC sp_RandomPlace
 @OutputVar nvarchar(100) OUTPUT
 AS

SET @OutputVar =  (select top 1 percent Place from [PlaceTable] order by newid())

然后在你的c#中

using(SqlConnection conn = new SqlConnection(ConnectionString ))
using(SqlCommand cmd = new SqlCommand("dbo.sp_RandomPlace", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;

    // set up the parameters

    cmd.Parameters.Add("@OutputVar", SqlDbType.Nvarchar).Direction = ParameterDirection.Output;



    // open connection and execute stored procedure
    conn.Open();
    cmd.ExecuteNonQuery();

    // read output value from @OutputVar
    string place= Convert.ToString(cmd.Parameters["@OutputVar"].Value);
    conn.Close();
}

上面的代码未经测试,但你得到了 jist

【讨论】:

  • 不,我已经设置了包含值的表。我想知道是否可以将此表用于我的随机生成器,而不必每次都添加Eat.Add("XX");新的地方决定了。在单击按钮时,它会在我的代码后面使用我的列表,但我想将其移出,因为随着时间的推移,其中可能会有很多条目
  • 查看修改,这很简单,也是很好的做法。
  • 我将如何进行存储过程,然后在单击按钮时获取值
  • 查看编辑,我没有测试过这些东西,但它就是这样工作的
  • 你需要添加一个连接字符串,这样它就知道如何连接sql server了,看这个:stackoverflow.com/questions/15631602/…
【解决方案2】:

感谢 Alec,我最终使用以下方法让它工作:

protected void BtnDecideForMe_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=DEV-116\\ONLINE;Initial Catalog=PaydayLunch;Integrated Security=True";

    using (SqlCommand cmd = new SqlCommand("dbo.GetRandomPlace", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        // set up the parameters
        cmd.Parameters.Add("@OutputVar", SqlDbType.VarChar, 25).Direction = ParameterDirection.Output;

        // open connection and execute stored procedure
        conn.Open();
        cmd.ExecuteNonQuery();

        // read output value from @OutputVar
        string place = Convert.ToString(cmd.Parameters["@OutputVar"].Value);
        conn.Close();

        txtDestination.Text = place;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2015-01-10
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多