2015-10-23 34 views
0

我有一個asp.net應用程序,它是一個隨機的地方生成器。使用SQL Server數據庫中的值的隨機生成器

目前我是在我的代碼背後的價值觀,但我想將這些到我的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; 
      } 
     } 
    } 
} 
+0

多少值有大致? –

+0

@Alec在我的文章中的所有人加上其他任何人建議,所以這個列表可能會增長。我還有另外8個也添加到我的列表,但還沒有 – murday1983

+0

我已經添加了一個答案來創建一個表併爲您插入這個東西。 –

回答

2

您可以通過在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

+0

不,我已經設置了表中的值。我想知道是否有可能使用這張表作爲我的隨機生成器來使用,而不是我必須不斷添加Eat.Add(「XX」);'每次一個新的地方決定。目前,點擊按鈕後,它會在我的代碼中使用我的列表,但我希望將其移出,因爲隨着時間的推移,可能會有很多條目。 – murday1983

+0

請參閱編輯,這是非常簡單的做法,也是很好的做法。 –

+0

我會如何去做這個sproc,然後在點擊我的按鈕時獲得價值 – murday1983

0

隨着感謝亞歷克,我manged得到它的工作最終使用下列內容:

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; 
    } 
} 
+0

隨意接受我的答案,如果它幫助:) –

+0

@Alec已完成 – murday1983