我有一個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;
}
}
}
}
多少值有大致? –
@Alec在我的文章中的所有人加上其他任何人建議,所以這個列表可能會增長。我還有另外8個也添加到我的列表,但還沒有 – murday1983
我已經添加了一個答案來創建一個表併爲您插入這個東西。 –